Re: [R] Plotting a cubic line from a multiple regression

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sun, 01 Jun 2008 14:16:41 +0000 (UTC)

Richard.Cotton_at_hsl.gov.uk wrote in
news:OF4505A545.494741B2-ON80257458.0059E49A-80257458.005AB622_at_hsl.gov. uk:

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

I do not think that is the reason for the error. The 2nd regression succeeded. Only the plotting failed. The error message says that the lengths of the vectors are the problem. In fact, I see no dataframes. This is being done on individual vectors. If instead you use:

pseudo.x = seq(min(age)-2, max(age)+2, len=100)

#                                          ^
... you get no error, albeit a rather jagged plot.
-- 
David Winsemius

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}}
>
> ______________________________________________
> R-help_at_r-project.org 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.
>
> ______________________________________________ R-help_at_r-project.org 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.
Received on Mon 02 Jun 2008 - 04:06:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 02 Jun 2008 - 04:30:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive