I have a simple data set for which I applied a simple linear regression model. Now I would like to use fixed effects to make a better prediction on the model. I know that I could also consider making dummy variables, but my real dataset consist of more years and has more variables so I would like to avoid making dummies.
My data and code is similar to this:
data <- read.table(header = TRUE,
stringsAsFactors = FALSE,
text="CompanyNumber ResponseVariable Year ExplanatoryVariable1 ExplanatoryVariable2
1 2.5 2000 1 2
1 4 2001 3 1
1 3 2002 5 7
2 1 2000 3 2
2 2.4 2001 0 4
2 6 2002 2 9
3 10 2000 8 3")
library(lfe)
library(caret)
fe <- getfe(felm(data = data, ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 | Year))
fe
lm.1<-lm(ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2, data=data)
prediction<- predict(lm.1, data)
prediction
check_model=postResample(pred = prediction, obs = data$ResponseVariable)
check_model
For my real dataset I will make a prediction based on my test set but for simplicity I just use the trainingset here as well.
I would like to make a prediction with the help of the fixed effects that I found. But it does not seem to match the fixed effect right, anyone who knows how to use this fe$effects
?
prediction_fe<- predict(lm.1, data) + fe$effect