您是否嘗試過直接做到,因爲在
> y
[1] 10 19 30 42 51 59 72 78
> X
[,1] [,2]
[1,] 1 1.0
[2,] 2 3.0
[3,] 3 5.5
[4,] 4 7.0
[5,] 5 9.0
[6,] 6 11.0
[7,] 7 13.0
[8,] 8 16.0
> summary(lm(y ~ X))
Call:
lm(formula = y ~ X)
Residuals:
1 2 3 4 5 6 7 8
-0.1396 -1.2774 0.9094 1.4472 0.3094 -1.8283 1.0340 -0.4547
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.647 2.004 -1.321 0.24366
X1 15.436 3.177 4.859 0.00464 **
X2 -2.649 1.535 -1.726 0.14490
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.363 on 5 degrees of freedom
Multiple R-squared: 0.9978, Adjusted R-squared: 0.9969
F-statistic: 1124 on 2 and 5 DF, p-value: 2.32e-07
來源
2016-03-19 22:02:39
wsw
也許感興趣:我昨天才知道,這招打造公式爲一個字符串,然後轉換爲公式也適用,如果你包括轉換。例如在上例中,將'colnames(HHdata)[20:43]'替換爲sapply(colnames(HHdata)[20:43],function(cc){gsub(「X」,cc,「I(X^2 )「,fixed = TRUE)})'。 – 2011-05-20 07:57:12
除了使用「I()」會產生令人誤解的統計輸出。學習如何使用'poly'。 – 2016-05-24 22:27:42
'poly'反過來,在缺少值時不起作用,除非您使用'raw = TRUE' - 但對於我來說,結果(或者您輸出_是什麼意思)是相同的。 – Daniel 2016-05-25 06:25:02