stat_smooth for a logistic model with more than one predictor

1,328 views
Skip to first unread message

Manuel Spínola

unread,
Oct 5, 2010, 7:52:27 AM10/5/10
to ggplot2
Dear list members,

How can I plot a logistic model with more than one predictor in ggplot2:

Using the function glm looks like this:

model <- glm(tortugas ~ profundidad + fondo + temperatura, family=binomial(logit), data=variables)

I tried this but this only make a model with one predictor without taking in consideration the other variables:

p = ggplot(variables, aes(temperatura, tortugas))
p + stat_smooth(method="glm", family="binomial", colour="red", size = 1.2, fill = "red")

Best,

Manuel
--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

Brian Diggs

unread,
Oct 5, 2010, 11:49:16 AM10/5/10
to ggplot2


On Oct 5, 4:52 am, Manuel Spínola <mspinol...@gmail.com> wrote:
>   Dear list members,
>
> How can I plot a logistic model with more than one predictor in ggplot2:
>
> Using the function glm looks like this:
>
> model <- glm(tortugas ~ profundidad + fondo + temperatura,
> family=binomial(logit), data=variables)
>
> I tried this but this only make a model with one predictor without
> taking in consideration the other variables:
>
> p = ggplot(variables, aes(temperatura, tortugas))
> p + stat_smooth(method="glm", family="binomial", colour="red", size =
> 1.2, fill = "red")

stat_smooth has a formula argument, as well as passing ... to the
smoothing function, so something like (untested):

p + stat_smooth(method="glm", formula=tortugas ~ profundidad + fondo +
temperatura, family="binomial", colour="red", size=1.2, fill="red")

might work.

> Best,
>
> Manuel
> --
> *Manuel Sp�nola, Ph.D.*
> Instituto Internacional en Conservaci�n y Manejo de Vida Silvestre
> Universidad Nacional
> Apartado 1350-3000
> Heredia
> COSTA RICA
> mspin...@una.ac.cr
> mspinol...@gmail.com
> Tel�fono: (506) 2277-3598
> Fax: (506) 2237-7036
> Personal website: Lobito de r�o
> <https://sites.google.com/site/lobitoderio/>
> Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Hadley Wickham

unread,
Oct 5, 2010, 12:07:01 PM10/5/10
to Brian Diggs, ggplot2
>> I tried this but this only make a model with one predictor without
>> taking in consideration the other variables:
>>
>> p = ggplot(variables, aes(temperatura, tortugas))
>> p + stat_smooth(method="glm", family="binomial", colour="red", size =
>> 1.2, fill = "red")
>
> stat_smooth has a formula argument, as well as passing ... to the
> smoothing function, so something like (untested):
>
> p + stat_smooth(method="glm", formula=tortugas ~ profundidad + fondo +
> temperatura, family="binomial", colour="red", size=1.2, fill="red")
>
> might work.

But you're better of fitting the model outside of ggplot2, and the
generating exactly what you want to plot. expand.grid is often
helpful in this situation.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Manuel Spínola

unread,
Oct 5, 2010, 12:20:44 PM10/5/10
to ggp...@googlegroups.com
Thank you very much Hadley.
Can you point me to any reference how to do this?
Is the example in the stat_smooth for a logistic regression only for a simple logistic regression, not multiple?
Best,

Manuel


On 05/10/2010 10:07 a.m., Hadley Wickham wrote:
I tried this but this only make a model with one predictor without
taking in consideration the other variables:

p = ggplot(variables, aes(temperatura, tortugas))
p + stat_smooth(method="glm", family="binomial", colour="red", size =
1.2, fill = "red")
stat_smooth has a formula argument, as well as passing ... to the
smoothing function, so something like (untested):

p + stat_smooth(method="glm", formula=tortugas ~ profundidad + fondo +
temperatura, family="binomial", colour="red", size=1.2, fill="red")

might work.
But you're better of fitting the model outside of ggplot2, and the
generating exactly what you want to plot.  expand.grid is often
helpful in this situation.

Hadley



--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

Manuel Spínola

unread,
Oct 5, 2010, 12:21:03 PM10/5/10
to ggp...@googlegroups.com
Thank you very much Brian.
I tried but it didn't work I guess that the plot need to be for 1 covariate at a time.
Best,

Manuel
--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

Dennis Murphy

unread,
Oct 5, 2010, 1:58:43 PM10/5/10
to Manuel Spínola, ggplot2
HI:

On Tue, Oct 5, 2010 at 9:17 AM, Manuel Spínola <mspin...@gmail.com> wrote:
Thank you very much Dennis.

Suppose that I have 4 covariates

model = glm(response ~ x1 + x2 + x3 + x4, family=binomail(logit), data = mydata)

Now I would like a plot for each covariate but according to the fitted model using stat_smooth, so I need to select one variable at the time, but how I do this using stat_smooth?

Yes, but...a plot of what? Predictions, residuals (which type?) or partial residuals are all possibilities for the vertical axis.

Perhaps you want something analogous to termplot() in base graphics, which can be applied to both lm() and glm() fitted objects. One possibility is to return the information you need into a data frame, e.g.,  res   x1   x2   x3   x4 (obtained from a glm model fit), melt it using res as the id variable and then use facet_wrap() in conjunction with ggplot() + ...  to get the final product. With facet_wrap(),  you should be able to get smooths for each variable individually. This would be consistent with Hadley's advice.

I'm reposting this to the list because others may have useful advice on this front, particularly those who have extensive experience with logistic regression. My inclination would be to plot appropriate forms of residuals by covariate - is there a useful context in which predicted values by covariate makes sense in logistic models?

Dennis

The example in stat_smooth for logistic regression appear to be for a simple model (only 1 covariate), right?
Best,

Manuel




On 05/10/2010 09:45 a.m., Dennis Murphy wrote:
Hi:

On Tue, Oct 5, 2010 at 4:52 AM, Manuel Spínola <mspin...@gmail.com> wrote:
Dear list members,

How can I plot a logistic model with more than one predictor in ggplot2:

Using the function glm looks like this:

model <- glm(tortugas ~ profundidad + fondo + temperatura, family=binomial(logit), data=variables)

I tried this but this only make a model with one predictor without taking in consideration the other variables:

p = ggplot(variables, aes(temperatura, tortugas))
p + stat_smooth(method="glm", family="binomial", colour="red", size = 1.2, fill = "red")

Looks like a scatterplot to me; if tortugas is the response in a glm model, it must be binary, so your plot should have varying x with (I imagine) clusters of y values at zero and one. Since you gave ggplot only one predictor, that's what it plotted :)

You haven't specified what it is that you really want. Do you want plots of predicted responses vs. each individual covariate, a surface plot of the predicted response over pairs of covariates, or something else entirely?  Your data may have to be reshaped depending on your needs.

The closest ggplot2 gets to a 3D plot is geom_contour or geom_tile. If you want pseudo-3D rendering (e.g., cloud() or wireframe() in lattice), you'll have to consult other packages such as lattice, scatterplot3d, rgl and maybe iplots.

HTH,
Dennis

Best,

Manuel
--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Manuel Spínola

unread,
Oct 5, 2010, 4:09:59 PM10/5/10
to Dennis Murphy, ggplot2
Thank you very much Dennis.
I will look at termplot.
What I want to do is the stat_smooth example for logistic regression but for each covariate in a multiple logistic regression.  I think the plot is for the predictions.
I am trying to get the same plots from package "effects" but in ggplot2.
Best,

Manuel


2010/10/5 Dennis Murphy <djm...@gmail.com>
Reply all
Reply to author
Forward
0 new messages