[R] Plotting a cubic line from a multiple regression

Ashish Ranpura a.ranpura at ucl.ac.uk
Thu May 29 15:20:25 CEST 2008


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



More information about the R-help mailing list