【问题标题】:Automate basic calculations with residuals in R使用 R 中的残差自动进行基本计算
【发布时间】:2015-03-30 20:16:14
【问题描述】:

我想对 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 时刻的总人口。

到目前为止,我的方法是:

首先我计算每年的总人口 nt:

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 函数和双“for-loop”,但没有成功。我不知道该怎么做。您的帮助将不胜感激。如果我的问题不清楚,请发表评论,我会努力改进。

【问题讨论】:

  • 在您的原始公式中,您指定 i 是国家,然后对国家进行求和。在这里您已经汇总了多年,但您不应该为国家/地区汇总吗?如果是这样,那么只需使用例如df_res &lt;- data.frame(country = df$Id, res=as.vector(res)); aggr_res &lt;- aggregate(df_res$res, list(df_res$country), sum)应该工作。
  • @JTT 嗯,我认为我的总结是正确的。结果应该是每年的一个值。每个值都是该特定年份所有国家/地区的总和。
  • 是的,我今天的洞察力不是很强……在那种情况下应该是这样。这解决了你的问题吗?
  • @JTT 是的,确实如此!谢谢 :) 也许你也知道这个(相关)问题的答案:stackoverflow.com/questions/29275633/…

标签: r


【解决方案1】:

首先,您可能希望使用聚合函数来计算总人口,而不是 for 循环,例如:

a<-aggregate(df$Population, list(df$Year), sum)

注意 a(Group.1 和 x)的列名。

然后您可以使用match() 函数将a 中的结果与df 中的数据相匹配。它给出了匹配的行号,可用于在与残差相乘之前将来自df 的数据子集到除法。例如:

res<-df$Population/a$x[match(df$Year, a$Group.1)]*residuals

现在您应该在对象res 中有一个“新”残差向量。

【讨论】:

  • 太棒了!谢谢。聚合函数确实是这里更好的方法。我在我的问题中添加了最后一步。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
相关资源
最近更新 更多