计算面板差异 [英] Compute panel differences

查看:100
本文介绍了计算面板差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些面板数据。对于这些数据,如果该组在两年中都有观察,我想计算每个组每年的差异。

I have some panel data. For this data, I want to compute the differences for every year for every group, if the group had observations in both years.

以下是数据:

> head(statistics)
     persnr year NOBS     value
1: 61961225 1993    1 0.5777598
2: 62037645 1993    1 0.5777598
3: 62181514 1993    1 0.5777598
4: 62499451 1993    1 0.5777598
5: 62649247 1993    1 0.5777598
6: 62744472 1993    1 0.5777598

其中 persnr 是面板的groupid。我现在的方法是沿着

Where persnr is the groupid for the panel. And my current approach was something along the lines of

dataTable = data.table(cast(statistics, persnr ~ year, value='totalWage'))
# y is the second year. Iterate over that
for (y in tail(unique(statistics[, jahr]), n=-1)):
  # get the first year
  x <- y - 1  
  dataTable[!is.na(`x`) & !is.na(`y`), `y`-`x`]
}

但是,我不能使用 \ x``方案来访问列。

However, I cant use the \x`` scheme to access the columns. What'd be an "R-ish" way of solving this?

推荐答案

我想要使用data.table这里:

I think that you want to use data.table here:

statistics[ by=persnr, order(year), list( year=year[-1], diff = diff(value) ) ]

这将给你一个data.table函数列: persnr year diff 。您可以将 -1 更改为 -N 以移除哪些差异被省略,第一个还是最后一个。

This would give you a data.table wit columns: persnr, year and diff. You can change the -1 to -N to shift which of the differences get omitted, the first or the last.

这篇关于计算面板差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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