# [R] Plotting a cubic line from a multiple regression

David Winsemius dwinsemius at comcast.net
Sun Jun 1 16:16:41 CEST 2008

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