用R模型预测结果

用R模型预测结果,r,linear-regression,R,Linear Regression,我试图用线性回归做一个简单的预测 我有一个data.frame,其中一些项目缺少价格(因此注明NA)。 这显然不起作用: #Simple LR fit <- lm(Price ~ Par1 + Par2 + Par3, data=combi[!is.na(combi$Price),]) Prediction <- predict(fit, data=combi[is.na(combi$Price),]), OOB=TRUE, type = "response") #简单LR 调整

我试图用线性回归做一个简单的预测 我有一个data.frame,其中一些项目缺少价格(因此注明NA)。 这显然不起作用:

#Simple LR
fit <- lm(Price ~ Par1 + Par2 + Par3, data=combi[!is.na(combi$Price),])
Prediction <- predict(fit,  data=combi[is.na(combi$Price),]), OOB=TRUE, type = "response")
#简单LR

调整将
数据
更改为
新数据
。查看
?predict.lm
以了解
predict
可以接受哪些参数。其他参数将被忽略。因此,在您的情况下,
data
(和
OOB
)将被忽略,默认情况是返回对训练数据的预测

Prediction <- predict(fit, newdata = combi[is.na(combi$Price),])

identical(predict(fit), predict(fit, data = combi[is.na(combi$Price),]))
## [1] TRUE

Prediction您也不必在原始拟合中明确排除
NA
,默认的
NA.action
是to
NA.ommit
,它会忽略具有任何
NA
值的行。另外,请注意,对于R中的几乎每个建模包都有一个
predict(…)
方法,其中一些方法使用
type=…'参数
predict.glm(…)`是一个很重要的例子,您确实需要指定
type=“response”