【问题标题】:Prediction using Fixed Effects使用固定效应进行预测
【发布时间】:2017-12-30 09:22:32
【问题描述】:

我有一个简单的数据集,我为其应用了一个简单的线性回归模型。现在我想使用固定效应对模型做出更好的预测。我知道我也可以考虑制作虚拟变量,但我的真实数据集包含更多年份并且有更多变量,所以我想避免制作虚拟变量。

我的数据和代码是这样的:

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

对于我的真实数据集,我将根据我的测试集进行预测,但为简单起见,我也在这里使用训练集。

我想借助我发现的固定效应进行预测。但是好像和固定效果不匹配吧,有谁知道这个fe$effects怎么用?

prediction_fe<- predict(lm.1, data) + fe$effect

【问题讨论】:

  • predict 函数应该使用模型中的固定效果。当您问“但它似乎与固定效果不匹配”时,您是什么意思?怎么不匹配?
  • 可能不是您问题的核心,但我收到此错误:找不到函数“postResample”
  • 虽然这可能会改变您所说的“固定效果”,但您可以在 lm 中添加 Year 吗?即:lm(ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 + Year, data=data)。你认为使用 lfe 你会得到一个非常重要的预测吗?
  • fe 发现了 2000 年到 2002 年的固定效应。但我不知道如何在我的预测中使用这些固定效应。因此,“匹配”实际上是指如何在预测中使用固定效应,以便将 2000 年的固定效应考虑到 2000 年的数据中。
  • @dca,我认为 postResample() 来自图书馆(插入符号)。

标签: r statistics prediction lm


【解决方案1】:

这里有一些关于您的设置和正在运行的模型的额外 cmets。

您要拟合的主要模型是

lm.1<-lm(ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2, data=data) 

产生

> lm.1
Call:
lm(formula = ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2, 
    data = data)

Coefficients:
         (Intercept)  ExplanatoryVariable1  ExplanatoryVariable2  
              0.8901                0.7857                0.1923  

当你在这个模型上运行 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 

现在这对应于“更正”或调整Year,因此如果适合,您会得到相同的结果

> 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 提取的固定效应中获取预测(因为您缺少截距和全年效应) - 您只能看到效应大小。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 2016-06-11
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多