R 利用固定效应进行预测
我有一个简单的数据集,我应用了一个简单的线性回归模型。现在我想使用固定效应对模型进行更好的预测。我知道我也可以考虑做哑变量,但是我的真实数据集包含了更多的年份,并且有更多的变量,所以我不想做假人。 我的数据和代码与此类似:R 利用固定效应进行预测,r,statistics,prediction,lm,R,Statistics,Prediction,Lm,我有一个简单的数据集,我应用了一个简单的线性回归模型。现在我想使用固定效应对模型进行更好的预测。我知道我也可以考虑做哑变量,但是我的真实数据集包含了更多的年份,并且有更多的变量,所以我不想做假人。 我的数据和代码与此类似: data <- read.table(header = TRUE, stringsAsFactors = FALSE, text="CompanyNumber ResponseVariabl
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
这里有一些关于您的设置和正在运行的模型的额外注释 正在拟合的主要模型是
lm.1<-lm(ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2, data=data)
在这个模型上运行predict
函数时,您会得到
> predict(lm.1)
1 2 3 4 5 6 7
2.060385 3.439410 6.164590 3.631718 1.659333 4.192205 7.752359
这对应于计算(观察1):0.8901+1*0.7857+2*0.1923,因此预测中使用了估计的固定效应。felm
模型稍微复杂一些,因为它“排除”了年份成分。模型拟合如图所示
> felm(data = data, ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 | Year)
ExplanatoryVariable1 ExplanatoryVariable2
0.9726 1.3262
现在,这对应于年的“校正”或条件反射,因此,如果适合,您将得到相同的结果
> lm(data = data, ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 + factor(Year))
Call:
lm(formula = ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 +
factor(Year), data = data)
Coefficients:
(Intercept) ExplanatoryVariable1 ExplanatoryVariable2 factor(Year)2001
-2.4848 0.9726 1.3262 0.9105
factor(Year)2002
-7.0286
然后扔掉所有的解释变量的系数。因此,您无法从felm
中提取固定效果并获得预测(因为您缺少截距和全年效果)-您只能看到效果大小
希望这有帮助。预测功能应该使用模型的固定效果。你问“但它似乎与固定效果不匹配”是什么意思?它怎么不匹配?可能不是你问题的核心,但我得到了一个错误:找不到函数“postResample”,虽然这可能会改变你所说的“固定效果”,但你可以只在lm中添加年份?即:lm(ResponseVariable~ExplanatoryVariable1+ExplanatoryVariable2+Year,data=data)。你认为使用lfe你会得到一个有实际意义的预测吗?fe发现了2000年到2002年的固定效应。但我不知道如何在我的预测中使用这些固定效应。所以“匹配”实际上是指如何在预测中使用固定效应,以便在2000年的数据中考虑2000年的固定效应。@dca,我认为postResample()来自库(插入符号)。
> felm(data = data, ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 | Year)
ExplanatoryVariable1 ExplanatoryVariable2
0.9726 1.3262
> lm(data = data, ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 + factor(Year))
Call:
lm(formula = ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 +
factor(Year), data = data)
Coefficients:
(Intercept) ExplanatoryVariable1 ExplanatoryVariable2 factor(Year)2001
-2.4848 0.9726 1.3262 0.9105
factor(Year)2002
-7.0286