请注意:我试图获取代码一起工作既时间与个人固定效应和不平衡的数据集.下面的示例代码适用于平衡数据集.
请参阅下面的编辑
我试图使用包手动计算固定效果模型的拟合值(具有个人和时间效应)plm.这更像是一个确认我理解模型和包的机制的练习,我知道我可以从plm对象中获取拟合值,来自两个相关问题(这里和这里).
从小plm插图(第2页),基础模型是:
y _it = alpha + beta _transposed*x _it +(mu _i + lambda _t + epsilon _it)
其中mu_i是错误术语的单独组成部分(又名"个人效应"),lambda_t是"时间效应".
固定效果可以通过使用提取fixef(),我认为我可以使用它们(连同自变量)来计算模型的拟合值,使用(使用两个独立变量)这样:
fit _it = alpha + beta _1*x1 + beta _2*x2 + mu _i + lambda _t
这是我失败的地方 - 我得到的值远不及拟合值(我得到的是实际值和模型对象中残差之间的差异).首先,我没有看到alpha任何地方.我尝试使用固定效果显示为与第一个,平均值等不同,但没有成功.
我错过了什么?它很可能是对模型的误解,或代码中的错误,我担心...提前谢谢.
PS:其中一个相关的问题提示pmodel.response()应该与我的问题有关(以及没有plm.fit功能的原因),但是它的帮助页面并没有帮助我理解这个功能实际上做了什么,我找不到任何例子如何解释它产生的结果.
谢谢!
我所做的示例代码:
library(data.table); library(plm)
set.seed(100)
DT <- data.table(CJ(id=c("a","b","c","d"), time=c(1:10)))
DT[, x1:=rnorm(40)]
DT[, x2:=rnorm(40)]
DT[, y:=x1 + 2*x2 + rnorm(40)/10]
DT <- DT[!(id=="a" & time==4)] # just to make it an unbalanced panel
setkey(DT, id, time)
summary(plmFEit <- plm(data=DT, id=c("id","time"), formula=y ~ x1 + x2, model="within", effect="twoways"))
# Extract the fitted values from the plm object
FV <- data.table(plmFEit$model, residuals=as.numeric(plmFEit$residuals))
FV[, y := as.numeric(y)]
FV[, x1 := as.numeric(x1)]
FV[, x2 := as.numeric(x2)]
DT <- merge(x=DT, y=FV, by=c("y","x1","x2"), all=TRUE)
DT[, fitted.plm := as.numeric(y) - as.numeric(residuals)]
FEI <- data.table(as.matrix(fixef(object=plmFEit, effect="individual", type="level")), keep.rownames=TRUE) # as.matrix needed to preserve the names?
setnames(FEI, c("id","fei"))
setkey(FEI, id)
setkey(DT, id)
DT <- DT[FEI] # merge the fei into the data, each id gets a single number for every row
FET <- data.table(as.matrix(fixef(object=plmFEit, effect="time", type="level")), keep.rownames=TRUE) # as.matrix needed to preserve the names?
setnames(FET, c("time","fet"))
FET[, time := as.integer(time)] # fixef returns time as character
setkey(FET, time)
setkey(DT, time)
DT <- DT[FET] # merge the fet into the data, each time gets a single number for every row
# calculate the fitted values (called calc to distinguish from those from plm)
DT[, fitted.calc := as.numeric(coef(plmFEit)[1] * x1 + coef(plmFEit)[2]*x2 + fei + fet)]
DT[, diff := as.numeric(fitted.plm - fitted.calc)]
all.equal(DT$fitted.plm, DT$fitted.calc)
Run Code Online (Sandbox Code Playgroud)
我的会议如下:
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] plm_1.4-0 Formula_1.2-1 RJSONIO_1.3-0 jsonlite_0.9.17 readxl_0.1.0.9000 data.table_1.9.7 bit64_0.9-5 bit_1.1-12 RevoUtilsMath_3.2.2
loaded via a namespace (and not attached):
[1] bdsmatrix_1.3-2 Rcpp_0.12.1 lattice_0.20-33 zoo_1.7-12 MASS_7.3-44 grid_3.2.2 chron_2.3-47 nlme_3.1-122 curl_0.9.3 rstudioapi_0.3.1 sandwich_2.3-4
[12] tools_3.2.2
Run Code Online (Sandbox Code Playgroud)
编辑:(2015-02-22) 由于这引起了一些兴趣,我将进一步澄清.我试图拟合一个"固定效果"模型(又名"内部"或"最小二乘虚拟变量",因为plm包装小插图在p.3,顶部段落上调用它) - 相同的斜率,不同的截距.
这与在为time和添加假人后运行普通OLS回归相同id.使用下面的代码,我可以plm使用base 复制包中的拟合值lm().使用假人,明确的是id和time的第一个元素是要比较的组.我仍然不能做的是如何使用plm包的设施来做我可以轻松完成的工作lm().
# fit the same with lm() and match the fitted values to those from plm()
lmF <- lm(data = DT, formula = y ~ x1 + x2 + factor(time) + factor(id))
time.lm <- coef(lmF)[grep(x = names(coef(lmF)), pattern = "time", fixed = TRUE)]
time.lm <- c(0, unname(time.lm)) # no need for names, the position index corresponds to time
id.lm <- coef(lmF)[grep(x = names(coef(lmF)), pattern = "id", fixed = TRUE)]
id.lm <- c(0, unname(id.lm))
names(id.lm) <- c("a","b","c","d") # set names so that individual values can be looked up below when generating the fit
DT[, by=list(id, time), fitted.lm := coef(lmF)[["(Intercept)"]] + coef(lmF)[["x1"]] * x1 + coef(lmF)[["x2"]] * x2 + time.lm[[time]] + id.lm[[id]]]
all.equal(DT$fitted.plm, DT$fitted.lm)
Run Code Online (Sandbox Code Playgroud)
希望这对可能感兴趣的其他人有用.该问题可能是一些关于如何plm和fixef与我故意创建的缺失值处理.我尝试使用type=参数fixef但没有效果.
编辑:适应双向不平衡模型,需要plm版本> = 2.4-0
这是你想要的吗?通过 提取固定效应fixef。以下是不平衡双向模型上的 Grunfeld 数据示例(平衡双向模型的工作原理相同):
gtw_u <- plm(inv ~ value + capital, data = Grunfeld[-200, ], effect = "twoways")
yhat <- as.numeric(gtw_u$model[ , 1] - gtw_u$residuals) # reference
pred_beta <- as.numeric(tcrossprod(coef(gtw_u), as.matrix(gtw_u$model[ , -1])))
pred_effs <- as.numeric(fixef(gtw_u, "twoways")) # sum of ind and time effects
all.equal(pred_effs + pred_beta, yhat) # TRUE -> matches fitted values (yhat)
Run Code Online (Sandbox Code Playgroud)
如果您想将个体效应和时间效应的总和(由 给出effect = "twoways")拆分为各个分量,您将需要选择一个参考,并且自然会想到两个参考,如下所示:
# Splits of summed up individual and time effects:
# use one "level" and one "dfirst"
ii <- index(gtw_u)[[1L]]; it <- index(gtw_u)[[2L]]
eff_id_dfirst <- c(0, as.numeric(fixef(gtw_u, "individual", "dfirst")))[ii]
eff_ti_dfirst <- c(0, as.numeric(fixef(gtw_u, "time", "dfirst")))[it]
eff_id_level <- as.numeric(fixef(gtw_u, "individual"))[ii]
eff_ti_level <- as.numeric(fixef(gtw_u, "time"))[it]
all.equal(pred_effs, eff_id_level + eff_ti_dfirst) # TRUE
all.equal(pred_effs, eff_id_dfirst + eff_ti_level) # TRUE
Run Code Online (Sandbox Code Playgroud)
(这是基于 fixef 的手册页。?fixef其中还显示了如何处理(平衡和不平衡)单向模型)。
| 归档时间: |
|
| 查看次数: |
3405 次 |
| 最近记录: |