简体   繁体   English

用R中的模型预测结果

[英]predicting outcome with a model in R

I am trying to do a simple prediction, using linear regression I have a data.frame where some of the items are missing price (and therefor noted NA). 我正在尝试使用线性回归做一个简单的预测,我有一个data.frame,其中某些项目缺少价格(因此标注为NA)。 This apperantely doesn't work: 这显然是行不通的:

#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")

What should I put instead of data=combi[is.na(combi$Price),]) ? 我应该代替data=combi[is.na(combi$Price),])放置什么?

Change data to newdata . data更改为newdata Look at ?predict.lm to see what arguments predict can take. 查看?predict.lm以查看predict可以接受的参数。 Additional arguments are ignored. 其他参数将被忽略。 So in your case data (and OOB ) is ignored and the default is to return predictions on the training data. 因此,在您的情况下, data (和OOB )将被忽略,默认值是对训练数据返回预测。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM