Rebecca Stirnemann
2013-Oct-09 21:35 UTC
[R] Help required graphing factors with predicted model settings
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") -- www.samoanbirds.com [[alternative HTML version deleted]]
Jim Lemon
2013-Oct-10 04:33 UTC
[R] Help required graphing factors with predicted model settings
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
2013-Oct-11 12:31 UTC
[R] Help required graphing factors with predicted model settings
On 10/10/2013 11:33 PM, Rebecca Stirnemann wrote:> Hi Michael, > Thanks! That worked. Which is so brilliant! > A couple of questions. In regards to display. > Do you know how to add labels on to the graph? The code below doesn't > work.Not surprising, since your data, mao1, is not in the lme4 package, and you didn't provide it.> Also is it possible some how to add all three lines onto one graph and > some how tell it to display things without colours?For the example I gave you, multiline=TRUE will plot all curves in a single graph. You have to learn how to read the help pages to find answers to such questions. plot(Effect(c("recipe", "temperature"), fm1), colors=rep("black",3), multiline=TRUE) # put the legend inside the plot plot(Effect(c("recipe", "temperature"), fm1), colors=rep("black",3), multiline=TRUE, key.args=list(x=.10, y=.80))> > Thanks soooooo much! > Rebecca > > library(effects) > > ?effect > > > library(lme4) > > data(mao1,package="lme4") > > fm1<-glm(frat~flandusenumb+ ground.cover_lo+(1|fsite),mao1, > > REML=FALSE) > > plot(effect("frat:ground.cover_lo",fm1, xlab="Low ground cover (%) ", > > ylab="Proportion of nests predated",),grid=TRUE) > > plot(Effect(c("flandusenumb","ground.cover_lo"),fm1))# equivalent > > > > > On Fri, Oct 11, 2013 at 1:41 AM, Michael Friendly <friendly@yorku.ca > <mailto:friendly@yorku.ca>> wrote: > > 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@bitwrit.com.au > <mailto:jim@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 > <tel:416%20736-2100%20x66249> Fax: 416 736-5814 <tel:416%20736-5814> > 4700 Keele Street Web: http://www.datavis.ca > Toronto, ONT M3J 1P3 CANADA > > > > > -- > www.samoanbirds.com <http://www.samoanbirds.com>-- 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 [[alternative HTML version deleted]]