I'm very new to R and modeling but need some help with visualization of glms. I'd like to make a graph of my glms to visualize the different effects of different parameters. I've got a binary response variable (bird sightings) and use binomial glms. The 'main' response variable is a measure of distance to a track and the parameters I'm testing for are vegetation parameters that effect the response in terms of distance. My glm is: glm(Response~NEdist+I(NEdist^2)+Distance+I(Distance^2) which is the basic model and where I add interactions to, like for exampls Visibility as an interaction to Distance (glm(Response~NEdist+I(NEdist^2)+Distance*Visibility+I(Distance^2))) I'd now like to make a graph which has the response variable on the y-axis (obviously). But the x-axis should have distance on it. The NEdist is a vector that is just co-influencing the curve and has to stay in the model but doesn't have any interactions with any other vectors. I'd then like to put in curves/lines for the different models to see if for example visibility effects the distance of the track to the first bird sighting. Is there a way to produce a graph in R that has these features? -- View this message in context: http://r.789695.n4.nabble.com/How-to-produce-glm-graph-tp3051367p3051367.html Sent from the R help mailing list archive at Nabble.com.
On Nov 20, 2010, at 4:27 AM, Sonja Klein wrote:> > I'm very new to R and modeling but need some help with visualization > of glms. > > I'd like to make a graph of my glms to visualize the different > effects of > different parameters. > I've got a binary response variable (bird sightings) and use > binomial glms. > The 'main' response variable is a measure of distance to a track and > the > parameters I'm testing for are vegetation parameters that effect the > response in terms of distance. > My glm is: glm(Response~NEdist+I(NEdist^2)+Distance+I(Distance^2) > which is > the basic model and where I add interactions to, like for exampls > Visibility > as an interaction to Distance > (glm(Response~NEdist+I(NEdist^2)+Distance*Visibility+I(Distance^2))) > > I'd now like to make a graph which has the response variable on the > y-axis > (obviously). But the x-axis should have distance on it. The NEdist > is a > vector that is just co-influencing the curve and has to stay in the > model > but doesn't have any interactions with any other vectors. > I'd then like to put in curves/lines for the different models to see > if for > example visibility effects the distance of the track to the first bird > sighting. > > Is there a way to produce a graph in R that has these features?Of course. Modeling would be of little value without such capability. In R, regression functions produce an object with a particular class ("glm" in this case) and there is generally have predict method for each class. There is also a vector of fitted values within the object that may be accessed with the fitted or fitted values functions. The predict.glm help page has a worked example. -- David Winsemius, MD West Hartford, CT
As others have noted, you can use predict() to get the fitted values and then plot them 'manually' using basic plotting functions in R. However, you will probably find it easier to use the effects package, which is designed for exactly this task. e.g., install.packages("effects") # if necessary library(effects) mod.cowles <- glm(volunteer ~ sex + neuroticism*extraversion, data=Cowles, family=binomial) eff.cowles <- allEffects(mod.cowles, xlevels=list(neuroticism=0:24, extraversion=seq(0, 24, 6)), given.values=c(sexmale=0.5)) eff.cowles In addition, you may find things simpler if you use poly(NEdist,2) rather than NEdist+I(NEdist^2), but effects should be able to handle either. HTH -Michael On 11/20/2010 4:27 AM, Sonja Klein wrote:> > I'm very new to R and modeling but need some help with visualization of glms. > > I'd like to make a graph of my glms to visualize the different effects of > different parameters. > I've got a binary response variable (bird sightings) and use binomial glms. > The 'main' response variable is a measure of distance to a track and the > parameters I'm testing for are vegetation parameters that effect the > response in terms of distance. > My glm is: glm(Response~NEdist+I(NEdist^2)+Distance+I(Distance^2) which is > the basic model and where I add interactions to, like for exampls Visibility > as an interaction to Distance > (glm(Response~NEdist+I(NEdist^2)+Distance*Visibility+I(Distance^2))) > > I'd now like to make a graph which has the response variable on the y-axis > (obviously). But the x-axis should have distance on it. The NEdist is a > vector that is just co-influencing the curve and has to stay in the model > but doesn't have any interactions with any other vectors. > I'd then like to put in curves/lines for the different models to see if for > example visibility effects the distance of the track to the first bird > sighting. > > Is there a way to produce a graph in R that has these features?