我的详细答案如下,但是对这种问题的一般(即真实)答案是:1)实验,拧紧螺丝,查看数据,无论做什么都无法中断计算机,因此。。。实验; 或2)RTFM。
这是一些R代码,或多或少地复制了此问题中确定的问题:
# This program written in response to a Cross Validated question
# http://stats.stackexchange.com/questions/95939/
# 
# It is an exploration of why the result from lm(y_x+I(x^2))
# looks so different from the result from lm(y~poly(x,2))
library(ggplot2)
epsilon <- 0.25*rnorm(100)
x       <- seq(from=1, to=5, length.out=100)
y       <- 4 - 0.6*x + 0.1*x^2 + epsilon
# Minimum is at x=3, the expected y value there is
4 - 0.6*3 + 0.1*3^2
ggplot(data=NULL,aes(x, y)) + geom_point() + 
       geom_smooth(method = "lm", formula = y ~ poly(x, 2))
summary(lm(y~x+I(x^2)))       # Looks right
summary(lm(y ~ poly(x, 2)))   # Looks like garbage
# What happened?
# What do x and x^2 look like:
head(cbind(x,x^2))
#What does poly(x,2) look like:
head(poly(x,2))
第一个lm返回预期的答案:
Call:
lm(formula = y ~ x + I(x^2))
Residuals:
     Min       1Q   Median       3Q      Max 
-0.53815 -0.13465 -0.01262  0.15369  0.61645 
Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  3.92734    0.15376  25.542  < 2e-16 ***
x           -0.53929    0.11221  -4.806 5.62e-06 ***
I(x^2)       0.09029    0.01843   4.900 3.84e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.2241 on 97 degrees of freedom
Multiple R-squared:  0.1985,    Adjusted R-squared:  0.182 
F-statistic: 12.01 on 2 and 97 DF,  p-value: 2.181e-05
第二个lm返回奇怪的东西:
Call:
lm(formula = y ~ poly(x, 2))
Residuals:
     Min       1Q   Median       3Q      Max 
-0.53815 -0.13465 -0.01262  0.15369  0.61645 
Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  3.24489    0.02241 144.765  < 2e-16 ***
poly(x, 2)1  0.02853    0.22415   0.127    0.899    
poly(x, 2)2  1.09835    0.22415   4.900 3.84e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.2241 on 97 degrees of freedom
Multiple R-squared:  0.1985,    Adjusted R-squared:  0.182 
F-statistic: 12.01 on 2 and 97 DF,  p-value: 2.181e-05
由于lm两个调用中的相同,因此它的参数lm必须不同。因此,让我们看一下参数。显然y是一样的。这是其他部分。让我们看一下第一次调用中对右侧变量的前几个观察结果lm。返回的head(cbind(x,x^2))样子如下:
            x         
[1,] 1.000000 1.000000
[2,] 1.040404 1.082441
[3,] 1.080808 1.168146
[4,] 1.121212 1.257117
[5,] 1.161616 1.349352
[6,] 1.202020 1.444853
这是预期的。第一列为x,第二列为x^2。第二次调用lm,带有poly的调用怎么样?返回的head(poly(x,2))样子如下:
              1         2
[1,] -0.1714816 0.2169976
[2,] -0.1680173 0.2038462
[3,] -0.1645531 0.1909632
[4,] -0.1610888 0.1783486
[5,] -0.1576245 0.1660025
[6,] -0.1541602 0.1539247
好的,那是完全不同的。第一列不是x,第二列不是x^2。因此,无论poly(x,2)做什么,它都不会返回x和x^2。如果我们想知道poly它的作用,我们可以先阅读其帮助文件。所以我们说help(poly)。描述说:
  返回或评估在指定点x上度为1到度的正交多项式。这些都与次数为0的常数多项式正交。或者,评估原始多项式。
现在,您要么知道“正交多项式”,要么不知道。如果您不这样做,请使用Wikipedia或Bing(当然,不是Google,因为Google很邪恶-当然不如Apple糟糕,但仍然很糟糕)。或者,您可能决定不关心什么是正交多项式。您可能会注意到短语“原始多项式”,并且可能会在帮助文件中稍稍向下一些,该文件中poly的选项raw默认为FALSE。这两个注意事项可能会激发您尝试head(poly(x, 2, raw=TRUE))返回哪些结果:
            1        2
[1,] 1.000000 1.000000
[2,] 1.040404 1.082441
[3,] 1.080808 1.168146
[4,] 1.121212 1.257117
[5,] 1.161616 1.349352
[6,] 1.202020 1.444853
这项发现令人兴奋(现在看起来不错,是吗?),您可以继续尝试以下操作summary(lm(y ~ poly(x, 2, raw=TRUE)))  :返回:
Call:
lm(formula = y ~ poly(x, 2, raw = TRUE))
Residuals:
     Min       1Q   Median       3Q      Max 
-0.53815 -0.13465 -0.01262  0.15369  0.61645 
Coefficients:
                        Estimate Std. Error t value Pr(>|t|)    
(Intercept)              3.92734    0.15376  25.542  < 2e-16 ***
poly(x, 2, raw = TRUE)1 -0.53929    0.11221  -4.806 5.62e-06 ***
poly(x, 2, raw = TRUE)2  0.09029    0.01843   4.900 3.84e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.2241 on 97 degrees of freedom
Multiple R-squared:  0.1985,    Adjusted R-squared:  0.182 
F-statistic: 12.01 on 2 and 97 DF,  p-value: 2.181e-05
以上答案至少有两个层次。首先,我回答了你的问题。其次,更重要的是,我说明了您应该如何自己回答这样的问题。每个“知道如何编程”的人都经历了超过六千万次的序列。就连像我一样沮丧的人,也一直都在经历这个过程。代码不起作用是正常的。误解什么功能是正常的。处理它的方法是拧紧,试验,查看数据和RTFM。让您摆脱“专心遵循食谱”模式,进入“侦探”模式。