21
votes
$\begingroup$

I've created a linear model in R: mod = lm(train_y ~ train_x). I want to pass it a list of X's and get its predicted/estimateed/forecasted Y. I looked at predict(), but I think that is for something else, or I just don't know how to use it.

I'm guessing by taking the coefficients of my model, I could manually plugin the test_x variables one-by-one, and get a predicted Y, but I'm guessing there is a more efficient way to do this.

$\endgroup$
20
votes
$\begingroup$

If you want the predicted values for train_x = 1, 2, and 3, use predict(mod, data.frame(train_x = c(1, 2, 3))).

$\endgroup$
  • $\begingroup$ I was leaving out the "train_x =", and adding that solved my issue! thank you kindly $\endgroup$ – Dolan Antenucci Aug 5 '11 at 2:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.