R - Plm 和 lm - 固定效果 [英] R - Plm and lm - Fixed effects

查看:138
本文介绍了R - Plm 和 lm - 固定效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个平衡的面板数据集,df,它基本上由三个变量组成,ABY,对于一堆唯一标识的区域,它们会随着时间的推移而变化.我想运行一个回归,其中包括区域(下面等式中的区域)和时间(年)固定效应.如果我没记错的话,我可以通过不同的方式实现这一点:

I have a balanced panel data set, df, that essentially consists in three variables, A, B and Y, that vary over time for a bunch of uniquely identified regions. I would like to run a regression that includes both regional (region in the equation below) and time (year) fixed effects. If I'm not mistaken, I can achieve this in different ways:

lm(Y ~ A + B + factor(region) + factor(year), data = df)

library(plm)
plm(Y ~ A + B, 
    data = df, index = c('region', 'year'), model = 'within',
    effect = 'twoways')

在第二个等式中,我指定了索引(regionyear)、模型类型('within'、FE)和 FE 的性质('twoways',这意味着我包括区域和时间 FE).

In the second equation I specify indices (region and year), the model type ('within', FE), and the nature of FE ('twoways', meaning that I'm including both region and time FE).

尽管我似乎做对了,但得到的结果却截然不同.当我考虑时间固定效应并使用参数effect = 'individual'时,问题就消失了.这里有什么交易?我错过了什么吗?是否还有其他 R 包可以运行相同的分析?

Despite I seem to be doing things correctly, I get extremely different results. The problem disappears when I do not consider time fixed effects - and use the argument effect = 'individual'. What's the deal here? Am I missing something? Are there any other R packages that allow to run the same analysis?

推荐答案

也许发布一个数据示例有助于回答这个问题.对于一些合成数据,我得到了相同的系数.你也可以使用 lfe 包中的 felm 来做同样的事情:

Perhaps posting an example of your data would help answer the question. I am getting the same coefficients for some made up data. You can also use felm from the package lfe to do the same thing:

N <- 10000
df <- data.frame(a = rnorm(N), b = rnorm(N),
                 region = rep(1:100, each = 100), year = rep(1:100, 100))
df$y <- 2 * df$a - 1.5 * df$b + rnorm(N)


model.a <- lm(y ~ a + b + factor(year) + factor(region), data = df)
summary(model.a)
#  (Intercept)       -0.0522691  0.1422052   -0.368   0.7132    
#  a                  1.9982165  0.0101501  196.866   <2e-16 ***
#  b                 -1.4787359  0.0101666 -145.450   <2e-16 ***

library(plm)
pdf <- pdata.frame(df, index = c("region", "year"))

model.b <- plm(y ~ a + b, data = pdf, model = "within", effect = "twoways")
summary(model.b)

# Coefficients :
#    Estimate Std. Error t-value  Pr(>|t|)    
# a  1.998217   0.010150  196.87 < 2.2e-16 ***
# b -1.478736   0.010167 -145.45 < 2.2e-16 ***

library(lfe)

model.c <- felm(y ~ a + b | factor(region) + factor(year), data = df)
summary(model.c)

# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)    
# a  1.99822    0.01015   196.9   <2e-16 ***
# b -1.47874    0.01017  -145.4   <2e-16 ***

这篇关于R - Plm 和 lm - 固定效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆