用R模型预测结果
我试图用线性回归做一个简单的预测 我有一个data.frame,其中一些项目缺少价格(因此注明NA)。 这显然不起作用:用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 调整
#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
是toNA.ommit
,它会忽略具有任何NA
值的行。另外,请注意,对于R中的几乎每个建模包都有一个predict(…)
方法,其中一些方法使用type=…'参数
predict.glm(…)`是一个很重要的例子,您确实需要指定type=“response”
。