Simon Pickett
2007-Apr-19 11:33 UTC
[R] general question about plotting multiple regression results
Hi all, I have been bumbling around with r for years now and still havent come up with a solution for plotting reliable graphs of relationships from a linear regression. Here is an example illustrating my problem 1.I do a linear regression as follows summary(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma,data=surv)) which gives some nice sig. results Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.73917 0.43742 -1.690 0.093069 . n.day1 1.00460 0.05369 18.711 < 2e-16 *** ffemale.yell 0.22419 0.06251 3.586 0.000449 *** fmale.yell 0.25874 0.06925 3.736 0.000262 *** fmale.chroma 0.23525 0.11633 2.022 0.044868 * 2. I want to plot the effect of "ffemale.yell", "fmale.yell" and "fmale.chroma" on my response variable. So, I either plot the raw values (which is fine when there is a very strong relationship) but what if I want to plot the effects from the model? In this case I would usually plot the fitted values values against the raw values of x... Is this the right approach? fit<-fitted(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma,data=fsurv1)) plot(fit~ffemale.yell) #make a dummy variable across the range of x x<-seq(from=min(fsurv1$ffemale.yell),to=max(fsurv1$ffemale.yell), length=100) #get the coefficients and draw the line co<-coef(lm(fit~ffemale.yell,data=fsurv1)) y<-(co[2]*x)+co[1] lines(x,y, lwd=2) This often does the trick but for some reason, especially when my model has many terms in it or when one of the independent variables is only significant when the other independent variables are in the equation, it gives me strange lines. Please can someone show me the light? Thanks in advance, Simon. Simon Pickett PhD student Centre For Ecology and Conservation Tremough Campus University of Exeter in Cornwall TR109EZ Tel 01326371852
Chuck Cleland
2007-Apr-19 12:08 UTC
[R] general question about plotting multiple regression results
Simon Pickett wrote:> Hi all, > > I have been bumbling around with r for years now and still havent come up > with a solution for plotting reliable graphs of relationships from a > linear regression. > > Here is an example illustrating my problem > > 1.I do a linear regression as follows > > summary(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma,data=surv)) > > which gives some nice sig. results > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) -0.73917 0.43742 -1.690 0.093069 . > n.day1 1.00460 0.05369 18.711 < 2e-16 *** > ffemale.yell 0.22419 0.06251 3.586 0.000449 *** > fmale.yell 0.25874 0.06925 3.736 0.000262 *** > fmale.chroma 0.23525 0.11633 2.022 0.044868 * > > 2. I want to plot the effect of "ffemale.yell", "fmale.yell" and > "fmale.chroma" on my response variable. > > So, I either plot the raw values (which is fine when there is a very > strong relationship) but what if I want to plot the effects from the > model? > > In this case I would usually plot the fitted values values against the raw > values of x... Is this the right approach? > > fit<-fitted(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma,data=fsurv1)) > > plot(fit~ffemale.yell) > > #make a dummy variable across the range of x > x<-seq(from=min(fsurv1$ffemale.yell),to=max(fsurv1$ffemale.yell), length=100) > > #get the coefficients and draw the line > co<-coef(lm(fit~ffemale.yell,data=fsurv1)) > y<-(co[2]*x)+co[1] > lines(x,y, lwd=2) > > This often does the trick but for some reason, especially when my model > has many terms in it or when one of the independent variables is only > significant when the other independent variables are in the equation, it > gives me strange lines. > > Please can someone show me the light?Have a close look at John Fox's effects package. For example, the following seems to be the kind of thing you want: fm <- lm(n.day13 ~ n.day1 + ffemale.yell + fmale.yell + fmale.chroma, data=fsurv1) library(effects) eff.fm <- effect("ffemale.yell", fm) plot(eff.fm)> Thanks in advance, > > Simon. > > Simon Pickett > PhD student > Centre For Ecology and Conservation > Tremough Campus > University of Exeter in Cornwall > TR109EZ > Tel 01326371852 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Liaw, Andy
2007-Apr-19 12:32 UTC
[R] general question about plotting multiple regression results [Broadcast]
I suspect you'll greatly benefit a read of Prof. Fox's book(s) on regression models, as well as making use of his "car" package. You may want to read up on partial residual plots and partial regression plots. Andy From: Simon Pickett> > Hi all, > > I have been bumbling around with r for years now and still > havent come up with a solution for plotting reliable graphs > of relationships from a linear regression. > > Here is an example illustrating my problem > > 1.I do a linear regression as follows > > summary(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma > ,data=surv)) > > which gives some nice sig. results > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) -0.73917 0.43742 -1.690 0.093069 . > n.day1 1.00460 0.05369 18.711 < 2e-16 *** > ffemale.yell 0.22419 0.06251 3.586 0.000449 *** > fmale.yell 0.25874 0.06925 3.736 0.000262 *** > fmale.chroma 0.23525 0.11633 2.022 0.044868 * > > 2. I want to plot the effect of "ffemale.yell", "fmale.yell" > and "fmale.chroma" on my response variable. > > So, I either plot the raw values (which is fine when there is > a very strong relationship) but what if I want to plot the > effects from the model? > > In this case I would usually plot the fitted values values > against the raw values of x... Is this the right approach? > > fit<-fitted(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.ch > roma,data=fsurv1)) > > plot(fit~ffemale.yell) > > #make a dummy variable across the range of x > x<-seq(from=min(fsurv1$ffemale.yell),to=max(fsurv1$ffemale.yel > l), length=100) > > #get the coefficients and draw the line > co<-coef(lm(fit~ffemale.yell,data=fsurv1)) > y<-(co[2]*x)+co[1] > lines(x,y, lwd=2) > > This often does the trick but for some reason, especially > when my model has many terms in it or when one of the > independent variables is only significant when the other > independent variables are in the equation, it gives me strange lines. > > Please can someone show me the light? > > Thanks in advance, > > Simon. > > > > > > > Simon Pickett > PhD student > Centre For Ecology and Conservation > Tremough Campus > University of Exeter in Cornwall > TR109EZ > Tel 01326371852 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
Thomas Lumley
2007-Apr-19 14:37 UTC
[R] general question about plotting multiple regression results
On Thu, 19 Apr 2007, Simon Pickett wrote:> Hi all, > > I have been bumbling around with r for years now and still havent come up > with a solution for plotting reliable graphs of relationships from a > linear regression.termplot() does this for a range of regression models (without interaction terms). The "effects" package does it better for linear regression models. -thomas> Here is an example illustrating my problem > > 1.I do a linear regression as follows > > summary(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma,data=surv)) > > which gives some nice sig. results > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) -0.73917 0.43742 -1.690 0.093069 . > n.day1 1.00460 0.05369 18.711 < 2e-16 *** > ffemale.yell 0.22419 0.06251 3.586 0.000449 *** > fmale.yell 0.25874 0.06925 3.736 0.000262 *** > fmale.chroma 0.23525 0.11633 2.022 0.044868 * > > 2. I want to plot the effect of "ffemale.yell", "fmale.yell" and > "fmale.chroma" on my response variable. > > So, I either plot the raw values (which is fine when there is a very > strong relationship) but what if I want to plot the effects from the > model? > > In this case I would usually plot the fitted values values against the raw > values of x... Is this the right approach? > > fit<-fitted(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma,data=fsurv1)) > > plot(fit~ffemale.yell) > > #make a dummy variable across the range of x > x<-seq(from=min(fsurv1$ffemale.yell),to=max(fsurv1$ffemale.yell), length=100) > > #get the coefficients and draw the line > co<-coef(lm(fit~ffemale.yell,data=fsurv1)) > y<-(co[2]*x)+co[1] > lines(x,y, lwd=2) > > This often does the trick but for some reason, especially when my model > has many terms in it or when one of the independent variables is only > significant when the other independent variables are in the equation, it > gives me strange lines. > > Please can someone show me the light? > > Thanks in advance, > > Simon. > > > > > > > Simon Pickett > PhD student > Centre For Ecology and Conservation > Tremough Campus > University of Exeter in Cornwall > TR109EZ > Tel 01326371852 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
John Fox
2007-Apr-19 21:20 UTC
[R] general question about plotting multiple regression results
Dear Thomas and Simon, On Thu, 19 Apr 2007 07:37:28 -0700 (PDT) Thomas Lumley <tlumley at u.washington.edu> wrote:> On Thu, 19 Apr 2007, Simon Pickett wrote: > > > Hi all, > > > > I have been bumbling around with r for years now and still havent > come up > > with a solution for plotting reliable graphs of relationships from > a > > linear regression. > > termplot() does this for a range of regression models (without > interaction > terms). The "effects" package does it better for linear regression > models. > > -thomas >The effects package also works for generalized linear models (which, I suppose, are arguably linear regression models). Regards, John> > > Here is an example illustrating my problem > > > > 1.I do a linear regression as follows > > > > >summary(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma,data=surv))> > > > which gives some nice sig. results > > > > Coefficients: > > Estimate Std. Error t value Pr(>|t|) > > (Intercept) -0.73917 0.43742 -1.690 0.093069 . > > n.day1 1.00460 0.05369 18.711 < 2e-16 *** > > ffemale.yell 0.22419 0.06251 3.586 0.000449 *** > > fmale.yell 0.25874 0.06925 3.736 0.000262 *** > > fmale.chroma 0.23525 0.11633 2.022 0.044868 * > > > > 2. I want to plot the effect of "ffemale.yell", "fmale.yell" and > > "fmale.chroma" on my response variable. > > > > So, I either plot the raw values (which is fine when there is a > very > > strong relationship) but what if I want to plot the effects from > the > > model? > > > > In this case I would usually plot the fitted values values against > the raw > > values of x... Is this the right approach? > > > > >fit<-fitted(lm(n.day13~n.day1+ffemale.yell+fmale.yell+fmale.chroma,data=fsurv1))> > > > plot(fit~ffemale.yell) > > > > #make a dummy variable across the range of x > > x<-seq(from=min(fsurv1$ffemale.yell),to=max(fsurv1$ffemale.yell), > length=100) > > > > #get the coefficients and draw the line > > co<-coef(lm(fit~ffemale.yell,data=fsurv1)) > > y<-(co[2]*x)+co[1] > > lines(x,y, lwd=2) > > > > This often does the trick but for some reason, especially when my > model > > has many terms in it or when one of the independent variables is > only > > significant when the other independent variables are in the > equation, it > > gives me strange lines. > > > > Please can someone show me the light? > > > > Thanks in advance, > > > > Simon. > > > > > > > > > > > > > > Simon Pickett > > PhD student > > Centre For Ecology and Conservation > > Tremough Campus > > University of Exeter in Cornwall > > TR109EZ > > Tel 01326371852 > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > Thomas Lumley Assoc. Professor, Biostatistics > tlumley at u.washington.edu University of Washington, Seattle > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/