Using ggplot2 to plot predicted probabilities

356 views
Skip to first unread message

Manuel Spínola

unread,
Oct 10, 2010, 7:13:23 PM10/10/10
to ggp...@googlegroups.com
Dear list members,

I want to plot the the results (predicted probabilities) of a logistic regression model with 5 categorical predictors (factors).  Using grid:expand I create the following object (see below) with the combinations (32) of each level of the factors.  Each factor has 2 levels.

Is there a way to represent this graphically in ggplot2?
Thank you very much in advance.

Manuel

> grid
   iluminacion animales cielo.raso piso paredes condicion       ucl        lcl
1            0        0          0    0       0 0.3955409 0.5282769 0.26280477
2            1        0          0    0       0 0.2881366 0.4219901 0.15428300
3            0        1          0    0       0 0.5093029 0.6329608 0.38564512
4            1        1          0    0       0 0.3909893 0.5308140 0.25116467
5            0        0          1    0       0 0.3629202 0.5072199 0.21862040
6            1        0          1    0       0 0.2605554 0.3967200 0.12439083
7            0        1          1    0       0 0.4746677 0.6159702 0.33336522
8            1        1          1    0       0 0.3585214 0.5063956 0.21064711
9            0        0          0    1       0 0.4289568 0.5914158 0.26649775
10           1        0          0    1       0 0.3172410 0.4807612 0.15372074
11           0        1          0    1       0 0.5436852 0.6945761 0.39279424
12           1        1          0    1       0 0.4242906 0.5939351 0.25464619
13           0        0          1    1       0 0.3953829 0.5740095 0.21675624
14           1        0          1    1       0 0.2880010 0.4571859 0.11881614
15           0        1          1    1       0 0.5091378 0.6828918 0.33538380
16           1        1          1    1       0 0.3908320 0.5734134 0.20825054
17           0        0          0    0       1 0.3651755 0.5230834 0.20726754
18           1        0          0    0       1 0.2624367 0.3946978 0.13017550
19           0        1          0    0       1 0.4770974 0.6421186 0.31207620
20           1        1          0    0       1 0.3607648 0.5107178 0.21081188
21           0        0          1    0       1 0.3336766 0.5141625 0.15319072
22           1        0          1    0       1 0.2364983 0.3825463 0.09045025
23           0        1          1    0       1 0.4426765 0.6381145 0.24723848
24           1        1          1    0       1 0.3294490 0.5012936 0.15760432
25           0        0          0    1       1 0.3977140 0.5106134 0.28481456
26           1        0          0    1       1 0.2900027 0.3903442 0.18966116
27           0        1          0    1       1 0.5115721 0.6202424 0.40290176
28           1        1          0    1       1 0.3931537 0.4962814 0.29002601
29           0        0          1    1       1 0.3650223 0.5212488 0.20879575
30           1        0          1    1       1 0.2623088 0.3938309 0.13078659
31           0        1          1    1       1 0.4769325 0.6378933 0.31597173
32           1        1          1    1       1 0.3606124 0.5075854 0.21363949



--
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

Andrie de Vries

unread,
Oct 11, 2010, 3:39:04 AM10/11/10
to ggplot2
Manuel

There is always a way, but first you have to describe in words what
you want the plot to look like.

Here is a starting point. First, I put your data into a data.frame,
and then plotted some of the variables using a combination of colour
and facets.

You can build on this by labelling your variables using descriptive
names (1 and 0 are good for modelling, but not descriptive for
graphing). You can build on this even more by combining some of your
predictors into super-categories. For example, you can combine 'piso'
and 'paredes' into a single variable 'piso.paredes' with four
different levels, and use this combined variable as a faceting
variable.



library(ggplot2)
library(reshape)

d <- structure(list(n = 1:32, iluminacion = c(0L, 1L, 0L, 1L, 0L,
1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), animales = c(0L,
0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L,
0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L),
cielo.raso = c(0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L,
0L, 0L, 1L, 1L, 1L, 1L), piso = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), paredes = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), condicion = c(0.3955409, 0.2881366, 0.5093029, 0.3909893,
0.3629202, 0.2605554, 0.4746677, 0.3585214, 0.4289568, 0.317241,
0.5436852, 0.4242906, 0.3953829, 0.288001, 0.5091378, 0.390832,
0.3651755, 0.2624367, 0.4770974, 0.3607648, 0.3336766, 0.2364983,
0.4426765, 0.329449, 0.397714, 0.2900027, 0.5115721, 0.3931537,
0.3650223, 0.2623088, 0.4769325, 0.3606124), ucl = c(0.5282769,
0.4219901, 0.6329608, 0.530814, 0.5072199, 0.39672, 0.6159702,
0.5063956, 0.5914158, 0.4807612, 0.6945761, 0.5939351, 0.5740095,
0.4571859, 0.6828918, 0.5734134, 0.5230834, 0.3946978, 0.6421186,
0.5107178, 0.5141625, 0.3825463, 0.6381145, 0.5012936, 0.5106134,
0.3903442, 0.6202424, 0.4962814, 0.5212488, 0.3938309, 0.6378933,
0.5075854), lcl = c(0.26280477, 0.154283, 0.38564512, 0.25116467,
0.2186204, 0.12439083, 0.33336522, 0.21064711, 0.26649775,
0.15372074, 0.39279424, 0.25464619, 0.21675624, 0.11881614,
0.3353838, 0.20825054, 0.20726754, 0.1301755, 0.3120762,
0.21081188, 0.15319072, 0.09045025, 0.24723848, 0.15760432,
0.28481456, 0.18966116, 0.40290176, 0.29002601, 0.20879575,
0.13078659, 0.31597173, 0.21363949)), .Names = c("n",
"iluminacion",
"animales", "cielo.raso", "piso", "paredes", "condicion", "ucl",
"lcl"), class = "data.frame", row.names = c(NA, -32L)) #

d$iluminacion <- as.factor(d$iluminacion)
d$animales <- as.factor(d$animales)
d$cielo.raso <- as.factor(d$cielo.raso)
d$piso <- as.factor(d$piso)
d$paredes <- as.factor(d$paredes)


meltd <- melt(d, id.vars=1:6)
str(meltd)

p <- ggplot(meltd, aes(x=iluminacion, y=value, colour=variable)) +
facet_grid(animales~cielo.raso) +
geom_point()
print(p)


Andrie
> *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/>

Manuel Spínola

unread,
Oct 11, 2010, 8:33:02 AM10/11/10
to Andrie de Vries, ggplot2
Thank you very much Andrie,

Something like you have done with facet will be very good.  I was thinking also on a plot like the result from a cluster analysis or a classification tree where the end will be a point range display showing the point estimate and the 95% CI (the predicted probabilities).
Yes, I agree with you, I used number to facilitate the process but the 1 and 0 are basically presence - absence for each categorical predictor.
I am trying to plot the result of a logistic model when your predictors are categorical. I have plotting without any problem the result of a model when variables are continuous using stat_smooth, but I have trouble to express graphically the model for categorical predictors.

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

Rob James

unread,
Oct 11, 2010, 12:24:37 PM10/11/10
to Andrie de Vries, ggplot2

You might want to look at Prof Harrell's rms package which is a remarkable collection of regression tools reflecting his philosophy, including many graphical displays.

It now (as of V3.0?) produces data frames from the Predict command and these df slide nicely into ggplot2.

Harrell and Hadley are clearly friends and while Harrell's current rms package does not yet make (extensive use) of ggplot, I suspect that plans are afoot.

I will say that have had less success (probably reflecting lack of effort) in converting Harrell's elegant bplot (heat-map) outputs into something equally elegant in ggplot2. But the fautl lies not in the software, but in the operator.  For displaying categorical predictors, etc, you can start with the rms standard outputs and then use the dataframes which get generated by Predict to feed ggplot. That said, rms has a very particular and specific take on things like bootstrapping, calibration, and to use it well really requires engaging with Harrell's larger methodological and statistical perspective.



---------------------------
Robert C James, PhD

On Mon 11/10/10 05:33 , Manuel Spínola mspin...@gmail.com sent:

Institutional website: ICOMVIS 


--
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 11, 2010, 1:10:03 PM10/11/10
to r...@aetiologic.ca, Andrie de Vries, ggplot2
Thank you very much Rob.

I will take a look at the rms.
I am following the idea of Andrew Gelman of plotting the model. instead of presenting a table with the coefficients, something that I do not see very often, at least in ecology.  Now, I found that having several categorical predictors, and even some of them with several levels, is challenging for showing the results, and even more challenging to make the plots.  I tried the effects package from John Fox, but the plot for categorical predictor is not what I would like for this type of variables. 
Best,

Manuel

2010/10/11 Rob James <r...@aetiologic.ca>

Dennis Murphy

unread,
Oct 11, 2010, 4:17:10 PM10/11/10
to Manuel Spínola, ggplot2
Hi:

The problem is that with five factors each at two levels, there are 32 factor combinations. I suppose you could combine the levels of two factors twice to produce a 4 x 4 plot, and then make one plot with all 16 combinations at level 0 of the fifth factor and another at level 1 of the fifth factor. You could then use the 'paste two graphs on one page' trick using the ideas from section 8.4 of Hadley's book. I suspect that plot will be a bit cramped, but it's one approach to the problem...

HTH,
Dennis

Manuel Spínola

unread,
Oct 12, 2010, 6:48:53 AM10/12/10
to Dennis Murphy, ggplot2
Thank you very much Dennis.
Yes, that is the problem, the 32 combinations.  This was my best model according to AIC and I am trying to plot the results.
I will try your suggestion.
Best,

Manuel

James Howison

unread,
Oct 12, 2010, 11:32:35 AM10/12/10
to ggplot2
I haven't taken a close look at your data, but the discussion makes me think of fuzzy set analysis. Might be relevant if you can convert your factor levels into set memberships (your dependent is already binary, right?) It gives results in terms of necessary and sufficient conditions which can really help interpretation of different routes to your outcome. Of course if no one knows it in your literature then it's a hard sell. This paper is a good intro:

Fiss PC (2007) A Set-theoretic Approach to Organizational Configurations. Academy of Management Review. 32:1180-1198.

--J

On Oct 12, 2010, at 06:48, Manuel Spínola wrote:

> Thank you very much Dennis.
> Yes, that is the problem, the 32 combinations. This was my best model according to AIC and I am trying to plot the results.
> I will try your suggestion.
> Best,
>
> Manuel
>
>
> On 11/10/2010 02:17 p.m., Dennis Murphy wrote:
>> Hi:
>>
>> The problem is that with five factors each at two levels, there are 32 factor combinations. I suppose you could combine the levels of two factors twice to produce a 4 x 4 plot, and then make one plot with all 16 combinations at level 0 of the fifth factor and another at level 1 of the fifth factor. You could then use the 'paste two graphs on one page' trick using the ideas from section 8.4 of Hadley's book. I suspect that plot will be a bit cramped, but it's one approach to the problem...
>>
>> HTH,
>> Dennis
>>

>> On Mon, Oct 11, 2010 at 10:10 AM, Manuel Spínola <mspin...@gmail.com <mailto:mspin...@gmail.com>> wrote:
>>
>> Thank you very much Rob.
>>
>> I will take a look at the rms.
>> I am following the idea of Andrew Gelman of plotting the model.
>> instead of presenting a table with the coefficients, something
>> that I do not see very often, at least in ecology. Now, I found
>> that having several categorical predictors, and even some of them
>> with several levels, is challenging for showing the results, and
>> even more challenging to make the plots. I tried the effects
>> package from John Fox, but the plot for categorical predictor is
>> not what I would like for this type of variables.
>> Best,
>>
>> Manuel
>>

>> 2010/10/11 Rob James <r...@aetiologic.ca <mailto:r...@aetiologic.ca>>


>>
>> You might want to look at Prof Harrell's rms package which is
>> a remarkable collection of regression tools reflecting his
>> philosophy, including many graphical displays.
>>
>> It now (as of V3.0?) produces data frames from the Predict
>> command and these df slide nicely into ggplot2.
>>
>> Harrell and Hadley are clearly friends and while Harrell's
>> current rms package does not yet make (extensive use) of
>> ggplot, I suspect that plans are afoot.
>>
>> I will say that have had less success (probably reflecting
>> lack of effort) in converting Harrell's elegant bplot
>> (heat-map) outputs into something equally elegant in ggplot2.
>> But the fautl lies not in the software, but in the operator. For displaying categorical predictors, etc, you can start with
>> the rms standard outputs and then use the dataframes which get
>> generated by Predict to feed ggplot. That said, rms has a very
>> particular and specific take on things like bootstrapping,
>> calibration, and to use it well really requires engaging with
>> Harrell's larger methodological and statistical perspective.
>>
>>
>>
>> ---------------------------
>> Robert C James, PhD
>>

>> *On Mon 11/10/10 05:33 , Manuel Spínola mspin...@gmail.com
>> <mailto:mspin...@gmail.com> sent:
>> *

>> -- *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

>> -- 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

>> <mailto:ggp...@googlegroups.com>
>> To unsubscribe: email ggplot2+u...@googlegroups.com
>> <mailto:ggplot2%2Bunsu...@googlegroups.com>


>> More options: http://groups.google.com/group/ggplot2
>>
>> -- 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

>> <mailto:ggp...@googlegroups.com>
>> To unsubscribe: email ggplot2+u...@googlegroups.com
>> <mailto:ggplot2%2Bunsu...@googlegroups.com>


>> More options: http://groups.google.com/group/ggplot2
>>
>>
>> -- 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

>> <mailto:ggp...@googlegroups.com>
>> To unsubscribe: email ggplot2+u...@googlegroups.com
>> <mailto:ggplot2%2Bunsu...@googlegroups.com>


>> More options: http://groups.google.com/group/ggplot2
>>
>>
>
>
> --

> *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 <https://sites.google.com/site/lobitoderio/>


> Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
>

Andrie de Vries

unread,
Oct 13, 2010, 11:25:44 AM10/13/10
to ggplot2
I have done some more work on your graph. Dennis's suggestion can in
fact be simplified a little bit. The following code combines two sets
of factors to display four variables in facets, and uses the fifth as
a categorical variable in the plot itself.

It helps, of course, to understand that the data really is one
variable, with upper and lower confidence limits.

library(ggplot2)
lilu <- paste("iluminacion", 0:1)
lani <- paste("animales", 0:1)
lcie <- paste("cielo.raso", 0:1)
lpis <- paste("piso", 0:1)
lpar <- paste("paredes", 0:1)

d$iluminacion <- as.factor(lilu[d$iluminacion + 1])
d$animales <- as.factor(lani[d$animales + 1])
d$cielo.raso <- as.factor(lcie[d$cielo.raso +1])
d$piso <- as.factor(lpis[d$piso + 1])
d$paredes <- as.factor(lpar[d$paredes + 1])

d$factor1 <- paste(d$animales, d$cielo.raso)
d$factor2 <- paste(d$piso, d$paredes)

p <- ggplot(d, aes(x=iluminacion)) +
facet_grid(factor1~factor2) +
geom_point(aes(y=condicion)) +
geom_linerange(aes(ymin=lcl, ymax=ucl))
print(p)

---

Andrie

On Oct 12, 4:32 pm, James Howison <ja...@freelancepropaganda.com>
wrote:
> I haven't taken a close look at your data, but the discussion makes me think of fuzzy set analysis.  Might be relevant if you can convert your factor levels into set memberships (your dependent is already binary, right?)  It gives results in terms of necessary and sufficient conditions which can really help interpretation of different routes to your outcome.  Of course if no one knows it in your literature then it's a hard sell.  This paper is a good intro:
>
> Fiss PC (2007) A Set-theoretic Approach to Organizational Configurations. Academy of Management Review. 32:1180-1198.
>
> --J
>
> On Oct 12, 2010, at 06:48, Manuel Spínola wrote:
>
> > Thank you very much Dennis.
> > Yes, that is the problem, the 32 combinations.  This was my best model according to AIC and I am trying to plot the results.
> > I will try your suggestion.
> > Best,
>
> > Manuel
>
> > On 11/10/2010 02:17 p.m., Dennis Murphy wrote:
> >> Hi:
>
> >> The problem is that with five factors each at two levels, there are 32 factor combinations. I suppose you could combine the levels of two factors twice to produce a 4 x 4 plot, and then make one plot with all 16 combinations at level 0 of the fifth factor and another at level 1 of the fifth factor. You could then use the 'paste two graphs on one page' trick using the ideas from section 8.4 of Hadley's book. I suspect that plot will be a bit cramped, but it's one approach to the problem...
>
> >> HTH,
> >> Dennis
>
> >>        *On Mon 11/10/10 05:33 , Manuel Spínola mspinol...@gmail.com
> >>        <mailto:mspinol...@gmail.com> sent:
> ...
>
> read more »

Manuel Spínola

unread,
Oct 14, 2010, 8:19:40 AM10/14/10
to Andrie de Vries, ggplot2
That's great Andrie.  Thank you very much.
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
Reply all
Reply to author
Forward
0 new messages