[R] Help required graphing factors with predicted model settings

Michael Friendly friendly at yorku.ca
Thu Oct 10 14:41:23 CEST 2013


Perhaps you are looking for the effects package, which can plot effects
(predicted values) for terms in mer objects from lme4?

library(effects)
?effect

library(lme4)
data(cake, package="lme4")
fm1 <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake,
                    REML = FALSE)
plot(effect("recipe:temperature", fm1), grid=TRUE)
plot(Effect(c("recipe", "temperature"), fm1)) # equivalent


On 10/10/2013 12:52 AM, Rebecca Stirnemann wrote:
> Thanks Jim for helping,
>
> Your sample data actually looks like my dataset. The one I put up looks
> strange for some reason so please ignore that.
> I have three landusenumb variables 1 2 and 3. is rep (1,2,3) correct?
>
> When I run the following code I am getting:
>
>> mod1 <- glmer(frat ~ flandusenumb + ground.cover_lo + (1|fsite) ,family =
> binomial, data= mao1)
>>
>> #Calculate predicted values
>> newdata1 <- data.frame(ground.cover_lo = c(25,50,100), flandusenumb =
> rep(1,2,3))
>> pred34 <- predict(mod1,newdata=newdata1,type="response")
>
> Error in UseMethod("predict") :
>    no applicable method for 'predict' applied to an object of class "mer"
>
> Can you see what I am doing wrong?
> What I am aiming for is a graph which looks like this.
>
> Thanks
> Rebecca
>
>
>
>
>
>
> On Thu, Oct 10, 2013 at 5:33 PM, Jim Lemon <jim at bitwrit.com.au> wrote:
>
>> On 10/10/2013 08:35 AM, Rebecca Stirnemann wrote:
>>
>>> Dear R wizards,
>>>
>>> Though I hate to do it after weeks of my code not working I need some help
>>> since I cant find an example which seems to work.
>>> I am trying to create a graph which show the probability of predation of a
>>> nest on one side (either 1 to 0) or (0% to 100%) on one side
>>> and grass height at the bottom. I want to then add my predicted lines from
>>> my glmr onto the graph for three habitat types.
>>>
>>> I would like to repeat this procedure 3 times for three different grass
>>> heights 25- 50- 100 to see the effect size.
>>>
>>> My data:
>>>      landusenumb landuse sitename rat ground.cover_lo  1  plantation
>>> far.leftroad_LHS 0 60  1 plantation far.leftroad_LHS 1 70  1 plantation
>>> far.leftroad_LHS 1 10  1 plantation far.leftroad_LHS 1 30  1 plantation
>>> far.leftroad_LHS 1 50  1 plantation far.leftroad_LHS 0 20  1 plantation
>>> far.leftroad_LHS 0 70  1 plantation far.leftroad_LHS 0 100  1 plantation
>>> far.leftroad_LHS 0 90
>>>
>>> #Graph
>>>
>>>
>>> #Fit model
>>>
>>> mod1<- glmer(frat ~ flandusenumb + ground.cover_lo + (1|fsite) ,family =
>>> binomial, data= mao1)
>>>
>>>
>>> #Calculate predicted values
>>>
>>> newdata1<- data.frame(ground.cover_lo = seq(0,10,length=100), flandusenumb
>>> = rep(1,2,3))
>>>
>>> pred34<- predict(mod1,newdata=newdata1,**type="response")
>>>
>>>
>>>
>>> #Plot model predicted curves
>>>
>>> plot(c(0,100),c(0,1),type="n",**xlab="grasscover",ylab="**Probability of
>>> predation")
>>>
>>> lines(newdata1$frat,pred34,**lwd=3,col="blue")
>>>
>>>
>>>   Hi Rebecca,
>> First, your sample data are a bit mangled, and should look like this:
>>
>> mao1
>>
>> landusenumb landuse    sitename rat ground.cover_lo
>> 1           plantation far.leftroad_LHS   0      60
>> 1           plantation far.leftroad_LHS   1      70
>> 1           plantation far.leftroad_LHS   1      10
>> 1           plantation far.leftroad_LHS   1      30
>> 1           plantation far.leftroad_LHS   1      50
>> 1           plantation far.leftroad_LHS   0      20
>> 1           plantation far.leftroad_LHS   0      70
>> 1           plantation far.leftroad_LHS   0     100
>> 1           plantation far.leftroad_LHS   0      90
>>
>> If you want the predicted values with ground cover as above, then:
>>
>> ground.cover_lo = c(25,50,100)
>>
>> The variable names in the first model don't match those in the data frame,
>> but I assume these were typos. What does "pred34" look like? This will tell
>> you what function you should be using to plot it.
>>
>> Jim
>>
>
>
>


-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list