我有一些基本的计算,我想应用在一个plm模型的剩余,但我被困在如何自动化的步骤,为大量的数据。
让我们假设输入是一个具有以下数据的data.frame (df):
Id Year Population Y X1 X2 X3
country A 2009 977612 212451.009 19482.7995 0.346657979 0.001023221
country A 2010 985332 221431.632 18989.3 0.345142551 0.001015205
country A 2011 998211 219939.296 18277.79286 0.344020453 0.001002106
country A 2012 1010001 218487.503 17916.2765 0.342434314 0.000990409
country B 2009 150291 177665.268 18444.04522 0.330864789 0.001940218
country B 2010 150841 183819.407 18042 0.327563461 0.001933143
country B 2011 152210 183761.566 17817.3515 0.32539255 0.001915756
country B 2012 153105 182825.112 17626.62261 0.321315437 0.001904557
country c 2009 83129 132328.034 17113.64268 0.359525557 0.005862866
country c 2010 83752 137413.878 16872.5 0.357854141 0.005819254
country c 2011 84493 136002.537 16576.17856 0.356479235 0.005768219
country c 2012 84958 133064.911 16443.3057 0.355246122 0.005736648应用了一个模型,并存储了残差:
fixed <- plm(Y ~ Y1 + X2 + X3,
data=df, drop.unused.levels = TRUE, index=c("Id", "Year"), model="within")
residuals <- resid(fixed)在接下来的步骤中,我想用以下方法计算残差的“加权平均值”:

以nit代表国家I的人口,t和nt是t的总人口。
到目前为止,我的做法是:
首先,我计算了每年的新台币总人口:
year_range <- seq(from=2009,to=2012,by=1)
tot_pop = NULL
for (n in year_range)
{
tot_pop[n] = with(df, sum(Population[Year == n]))
}在取“加权”残差之和之前,我的下一步将是自动计算“新”残差:
res1 <- df$Population[1]/tot_pop[2009] * residuals[1]
res2 <- df$Population[2]/tot_pop[2010] * residuals[2]
res3 <- df$Population[3]/tot_pop[2011] * residuals[3]
...
res12 <- df$Population[12]/tot_pop[2011] * residuals[12]编辑:将JTT的解决方案应用于我的问题,最后一步是:
year_range1 <- rep(year_range, 3)
df_res <- data.frame(year = year_range1, res=as.vector(res))
aggr_res <- aggregate(df_res$res, list(df_res$year), sum)
colnames(aggr_res) <- c("Year", "Aggregated residual")对吗?
我试过了lapply函数和双“换循环”,但没有成功。我不知道该怎么做。你的帮助将不胜感激。如果我的问题不清楚,请评论,我将努力改进它。
发布于 2015-03-30 10:12:43
首先,您可能需要使用聚合函数来计算总人口,而不是for-循环,例如:
a<-aggregate(df$Population, list(df$Year), sum)注意(Group.1和x)的列名。
然后,您可以使用a -function将df中的结果匹配到df中的数据。它给出了匹配的行号,在与残差相乘之前,行号可以用于从df到除法的数据子集。例如:
res<-df$Population/a$x[match(df$Year, a$Group.1)]*residuals现在,您应该在对象res中有一个“新”残差的向量。
https://stackoverflow.com/questions/29342993
复制相似问题