Hi everyone,
this should be pretty basic but I need asking for help as I got stuck.
I am running simple linear regression models on R with k regressors where k
> 1. In order to automate my code I packed all the regressors in a matrix X
so that lm(y~X) will always produce the results I want regardless of the
variables in X. I am new to R but I found this advice somewhere so I guess
it is relatively standard practice. This works very well until I need to
forecast using the estimate model.
I cannot pass a matrix to predict - when I pass a data frame I get the
fitted valuie which leads me to think that R doesnt see the data.frame I
pass to predict
Thanks in advance,
Paolo
# REPRODUCIBLE CODE
x <- matrix(rnorm(30), ncol =2)
y <- 1 + 3*x[, 1] + 2*x[, 1] + rnorm(15)
new_x <- matrix(rnorm(2), ncol =2)
new_x.d <- data.frame(new_x)
# fitted values
predict(lm(y ~ x))
# same as fitted values
predict(lm(y ~ x), new_x.d)
# error
predict(lm(y ~ x), new_x)
[[alternative HTML version deleted]]