固定效果plm软件包R-每年/id多次观察 [英] Fixed Effects plm package R - multiple observations per year/id

查看:615
本文介绍了固定效果plm软件包R-每年/id多次观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究状态和年份固定效应回归,该州/年份组合基于该行的种族(白色,黑色,其他)具有3个观察值-请参阅下面的链接.
到目前为止,我一直在使用基本的lm函数来估计考虑所有三个种族的固定效应回归.我通过使用状态,年份和种族作为因子变量来做到这一点.我还为每个种族运行单独的回归.问题是我更喜欢使用plm包,这样我就可以在所有种族下获得模型的r平方内,但是它给了我错误.

I'm working on a state and year fixed effects regression, which has 3 observations per state/year combo based on the race for that row (white, black, other) - See link below.
So far, I've been using the base lm function to estimate a fixed effects regression that accounts for all three races. I do this by using state, year and race all as factor variables. I am also running separate regressions for each individual race. The problem is that I would prefer to use the plm package so that i can get the within r-squared for the model with all races, however it is giving me errors.

我在此处包括了我的数据图片 数据是一个平衡的面板,有34个州,12年(2003-2014年),每个州/年组合有3个种族,因此共有1244个观测值.

I included a picture of my data here the data is a balanced panel, there are 34 states, 12 years (2003-2014) and 3 races for each state/year combo so a total of 1244 observations.

这是我用来运行plm回归的代码:

Here is the code I'm using to run the plm regression:

#plm regression
plm.reg <- plm(drugcrime_ar ~ decrim_dummy + median_income + factor(race),
               data = my.data, index=c("st_name","year"), model = "within",
               effect = "twoways")

我得到的错误回报:

Error in pdim.default(index[[1]], index[[2]]): 
   duplicate couples (id-time) 
In addition: Warning messages: 
1: In pdata.frame(data, index) :
   duplicate couples (id-time) in resulting pdata.frame
   to find out which, use e.g. table(index(your_pdataframe), useNA = "ifany"
2: In is.pbalanced.default(index[[1]], index[[2]]) :
   duplicate couples (id-time)
 3: In is.pbalanced.default(index[[1]], index[[2]]) :
   duplicate couples (id-time)  ` 

这是否有解决方法?还是我不走运?

Is there a workaround for this or am I out of luck?

推荐答案

plm函数仅需要一对id/时间.对于您提供的每个ID,您都有超过一年的时间.

The plm function needs just one pair of id/time. For each id you supplied you have more than one year.

如果每个st_namerace对组成一个个人"(或您在面板的该尺寸中使用的任何名称),那么您可以这样做:

If each st_name and race pairs form an "individual" (or whatever the name you give to this dimension of the panel), then you could do:

library(dplyr)

my.data$id <- group_indices(my.data, st_name, race)    
#which would be the same as my.data <- my.data %>% mutate(id = group_indices(st_name, race)), if this function supported mutate. 

plm.reg <- plm(drugcrime_ar ~ decrim_dummy + median_income + factor(race),
           data = my.data, index=c("id","year"), model = "within",
           effect = "twoways")

但是,请注意,在这种情况下,您没有使用@ Helix123建议的一种嵌套面板结构.您只是在重新定义面板的第一个尺寸.

See, however, that in this situation you are not using a kind of nested panel structure as @Helix123 suggested. You are only redefining the first dimension of the panel.

这篇关于固定效果plm软件包R-每年/id多次观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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