[R] Plotting a cubic line from a multiple regression

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Thu May 29 18:30:48 CEST 2008


> 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}}



More information about the R-help mailing list