[R] Polynomial regression problem

David Winsemius
Tue, 15 Mar 2011 19:06:55 -0000

On Mar 15, 2011, at 10:41 AM, Matthew McKinney wrote:

> I have just started using R so forgive me if this question is very
> simple. I have a data set (in a data frame called dm) that looks like
> this
>
> x (Cells)    y(males)
> 1	0
> 2	2
> 3	7
> 4	12
> 5	12
> 6	19
> 7	22
> 8	23
> 9	25
> 10	23
> 11	23
> 12	11
> 13	8
> 14	3
> 15	0
> 16	0
>
>
> I centered the dependent and predictor variables and then squared the
> predictor to make a quadratic variable.
> This left me with 3 variables:
> MalesC
> CellsC
> CellsC2
>
> I then used: >quadraticModel <- lm(MalesC ~ CellsC + CellsC2, data =
> dm)
>
> This has given me R^2= 0.8821, F= 48.63, and p<0.001
>
> I ran the exact same data in sigma plot and got identical results. My
> problem comes from the estimated coefficients I am getting in R when
> using >summary(quadraticModel). My coefficient estimates in sigma plot
> fit my data set well and agree with Microsoft excels estimates.

Around these parts agreement with Excel may be evidence _against_
correctness.

> The
> results from R appear to be the coefficients of a curve fitting the
> residuals. If I >plot(quadraticModel) it does not draw a curve fitting
> my data, but rather the residuals.

You give no basis for your conclusions regarding "fitting residuals",
nor code, nor specific results. This would be a more "standard" way of
using the facilities of R for polynomial regression:

 > sec.degr.mod <- lm(y ~ poly(x, 2), data=dm)
 > sec.degr.mod

Call:
lm(formula = y ~ poly(x, 2), data = dm)

Coefficients:
(Intercept)  poly(x, 2)1  poly(x, 2)2
      11.875       -2.386      -34.243

 > summary(sec.degr.mod)

Call:
lm(formula = y ~ poly(x, 2), data = dm)

Residuals:
     Min      1Q  Median      3Q     Max
-4.4998 -2.0758 -0.4526  2.7860  4.9534

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept)  11.8750     0.8701  13.647 4.40e-09 ***
poly(x, 2)1  -2.3862     3.4805  -0.686    0.505
poly(x, 2)2 -34.2429     3.4805  -9.838 2.17e-07 ***
---
Signif. codes:  0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1

Residual standard error: 3.481 on 13 degrees of freedom
Multiple R-squared: 0.8821,	Adjusted R-squared: 0.864
F-statistic: 48.63 on 2 and 13 DF,  p-value: 9.221e-07

>
> What I would like to know is how I can get the coefficient estimates
> for the data rather than the residuals, and how to plot that in R.

As far as I know those regression coefficients reflect operations the
data.

This produced sensible output:

 > plot(dm$x, dm$y)
 > lines(dm$x, predict(sec.degr.mod))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Poly2.plot.predict.pdf
Type: application/pdf
Size: 70438 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20110315/cb048b9b/attachment.pdf>
-------------- next part --------------


--

David Winsemius, MD
West Hartford, CT