在多变量回归(多个响应)的情况下,predict.lm
函数似乎不起作用。它只是预测第一个回应。
示例代码:
df = data_frame(x1 = c(1:20),
y1 = x1 + rnorm(20,5,sd = 5),
y2 = 2*x1 + rnorm(20,-5,sd = 3))
m1 = lm(cbind(y1, y2) ~ x1, data = df)
m1
## Call:
## lm(formula = cbind(y1, y2) ~ x1, data = df)
## Coefficients:
## y1 y2
## (Intercept) 7.5934 -5.9436
## x1 0.8704 2.0913
如果我尝试预测使用此模型对象(对于训练数据或newdata
),它仅预测第一个响应。
# predicts only for y1
length(predict.lm(m1))
## [1] 20
# predicts only for y1
predict.lm(m1, newdata = data_frame(x1 = 50))
## 1
## 51.11093
如何让此功能适用于多个响应?
答案 0 :(得分:2)
而不是使用predict.lm
使用predict
。
> predict.lm(m1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
4.210964 5.260564 6.310164 7.359764 8.409365 9.458965 10.508565 11.558166 12.607766 13.657366 14.706967 15.756567 16.806167 17.855767
15 16 17 18 19 20
18.905368 19.954968 21.004568 22.054169 23.103769 24.153369
> predict(m1)
y1 y2
1 4.210964 -3.117932
2 5.260564 -1.024133
3 6.310164 1.069666
4 7.359764 3.163466
5 8.409365 5.257265
6 9.458965 7.351064
7 10.508565 9.444863
8 11.558166 11.538663
9 12.607766 13.632462
10 13.657366 15.726261
11 14.706967 17.820061
12 15.756567 19.913860
13 16.806167 22.007659
14 17.855767 24.101459
15 18.905368 26.195258
16 19.954968 28.289057
17 21.004568 30.382857
18 22.054169 32.476656
19 23.103769 34.570455
20 24.153369 36.664255