If you read the documentation for predict.lm
, you will see the following. So, use the newdata
argument to pass the newmodel
data you imported to get predictions.
predict(object, newdata, se.fit = FALSE, scale = NULL, df = Inf,
interval = c("none", "confidence", "prediction"),
level = 0.95, type = c("response", "terms"),
terms = NULL, na.action = na.pass,
pred.var = res.var/weights, weights = 1, ...)
Arguments
object
Object of class inheriting from "lm"
newdata
An optional data frame in which to look for variables with which to predict.
If omitted, the fitted values are used.
UPDATE. On the question of exporting data with predictions, here is how you can do it.
predictions = cbind(newmodel, pred = predict(bigmodel, newdata = newmodel))
write.csv(predictions, 'predictions.csv', row.names = F)
UPDATE 2. A full minimally reproducible solution
bigmodel <- lm(mpg ~ wt, data = mtcars)
newdata = data.frame(wt = runif(20, min = 1.5, max = 6))
cbind(
newdata,
mpg = predict(bigmodel, newdata = newdata)
)