thecheech
thecheech

Reputation: 2211

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). 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),]) ?

Upvotes: 0

Views: 230

Answers (1)

Jake Burkhead
Jake Burkhead

Reputation: 6545

Change data to newdata. Look at ?predict.lm to see what arguments predict can take. Additional arguments are ignored. So in your case data (and OOB) is ignored and the default is to return predictions on the training data.

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

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

Upvotes: 1

Related Questions