Dear all, I'm attempting to plot a cubic relationship between two variables controlling for the effects of a third variable. In this short example, I'm trying to use AGE to predict CORTEX while controlling for the effects of TIV (total intracranial volume): ######################## cortex = rnorm(100, mean=0.5, sd=0.5) age = rnorm(100, mean=10, sd=2) tiv = rnorm(100, mean=1000, sd=100) ## ## simple regression (ignoring TIV) works ## cortex.lm = lm(cortex ~ poly(age, degree=3)) plot(age, cortex) pseudo.x = seq(min(age)-2, max(age)+2, len=200) lines(pseudo.x, predict(cortex.lm, data.frame(age=pseudo.x))) ## ## multiple regression (accounting for TIV) fails ## cortex.lm = lm(cortex ~ poly(age, degree=3) + tiv) plot(age, cortex) pseudo.x = seq(min(age)-2, max(age)+2, len=200) lines(pseudo.x, predict(cortex.lm, data.frame(age=pseudo.x))) ######################## Although the last 'lines' command fails, summary(cortex.lm) does give me reasonable coefficients for the multiple regression. Can anyone advise me if (1) this is at all a legitimate procedure, and (2) how to draw the cubic function that fits the multiple regression? Thanks! Ash. ----- Ashish Ranpura Institute of Cognitive Neuroscience University College London 17 Queen Square London WC1N 3AR tel: +44 (20) 7679 1126 web: http://www.icn.ucl.ac.uk
Richard.Cotton at hsl.gov.uk
2008-May-29 16:30 UTC
[R] Plotting a cubic line from a multiple regression
> I'm attempting to plot a cubic relationship between two variables > controlling for the effects of a third variable. In this short > example, I'm trying to use AGE to predict CORTEX while controlling for > the effects of TIV (total intracranial volume): > > ######################## > cortex = rnorm(100, mean=0.5, sd=0.5) > age = rnorm(100, mean=10, sd=2) > tiv = rnorm(100, mean=1000, sd=100) > > ## > ## simple regression (ignoring TIV) works > ## > cortex.lm = lm(cortex ~ poly(age, degree=3)) > plot(age, cortex) > pseudo.x = seq(min(age)-2, max(age)+2, len=200) > lines(pseudo.x, predict(cortex.lm, data.frame(age=pseudo.x))) > > ## > ## multiple regression (accounting for TIV) fails > ## > cortex.lm = lm(cortex ~ poly(age, degree=3) + tiv) > plot(age, cortex) > pseudo.x = seq(min(age)-2, max(age)+2, len=200) > lines(pseudo.x, predict(cortex.lm, data.frame(age=pseudo.x))) > ######################## > > Although the last 'lines' command fails, summary(cortex.lm) does give > me reasonable coefficients for the multiple regression. Can anyone > advise me if (1) this is at all a legitimate procedure, and (2) how to > draw the cubic function that fits the multiple regression?The last line fails because you haven't specified any values for TIV in the data frame. There are a couple of things you can plot: 1. The effect on cortex of varying x, at different levels of tiv (which is what you were doing) lines(pseudo.x, predict(cortex.lm, data.frame(age=pseudo.x, tiv=1000))) lines(pseudo.x, predict(cortex.lm, data.frame(age=pseudo.x, tiv=min(tiv))), col="red") lines(pseudo.x, predict(cortex.lm, data.frame(age=pseudo.x, tiv=max(tiv))), col="blue") 2. The effect on cortex of varying tiv, at different levels of age. pseudo.tiv = seq(min(tiv)-20, max(tiv)+20, len=200) plot(tiv, cortex) lines(pseudo.tiv, predict(cortex.lm, data.frame(age=8, tiv=pseudo.tiv))) lines(pseudo.tiv, predict(cortex.lm, data.frame(age=min(age), tiv=pseudo.tiv)), col="red") lines(pseudo.tiv, predict(cortex.lm, data.frame(age=max(age), tiv=pseudo.tiv)), col="blue") Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
Stephan Kolassa
2008-May-29 19:52 UTC
[R] Plotting a cubic line from a multiple regression
Hi Ashish, I am rather more concerned about whether what you outlined is legitimate (your question 1 below). If you are looking at children, higher AGE will be associated with higher TIV, so both variables would essentially measure the same thing (see Miller & Chapman, Misunderstanding Analysis of Covariance), which I really would not like to see. Of course, if you are looking at adults, this will probably not apply. Best wishes, Stephan Ashish Ranpura schrieb:> > Dear all, > > I'm attempting to plot a cubic relationship between two variables > controlling for the effects of a third variable. In this short example, > I'm trying to use AGE to predict CORTEX while controlling for the > effects of TIV (total intracranial volume):(snip)> Although the last 'lines' command fails, summary(cortex.lm) does give me > reasonable coefficients for the multiple regression. Can anyone advise > me if (1) this is at all a legitimate procedure, and (2) how to draw the > cubic function that fits the multiple regression?