【问题标题】:How do I Difference Panel Data in R如何在 R 中区分面板数据
【发布时间】:2014-04-28 18:43:56
【问题描述】:

我想知道是否有任何简单的 R 命令或包都可以让我轻松地将变量添加到 data.frames 中,这些变量是这些变量的“差异”或随时间的变化。

如果我的数据如下所示:

set.seed(1)
MyData <- data.frame(Day=0:9 %% 5+1, 
                 Price=rpois(10,10),
                 Good=rep(c("apples","oranges"), each=5))
MyData

   Day Price    Good
1    1     8  apples
2    2    10  apples
3    3     7  apples
4    4    11  apples
5    5    14  apples
6    1    12 oranges
7    2    11 oranges
8    3     9 oranges
9    4    14 oranges
10   5    11 oranges

然后在对价格变量进行“一阶差分”之后,我的数据将如下所示。

   Day Price    Good P1d
1    1     8  apples  NA
2    2    10  apples   2
3    3     7  apples  -3
4    4    11  apples   4
5    5    14  apples   3
6    1    12 oranges  NA
7    2    11 oranges  -1
8    3     9 oranges  -2
9    4    14 oranges   5
10   5    11 oranges  -3

【问题讨论】:

    标签: r time-series panel-data


    【解决方案1】:

    collapse::fdiff 是您要查找的函数:

    library(collapse)
    # This means compute difference of Price lagged once, iterated once, by Good, ordered by Day
    settransform(MyData, P1d = fdiff(Price, 1, 1, Good, Day))
    

    【讨论】:

      【解决方案2】:

      就我而言,我必须为面板生成第一个差异。为了使差分向量具有相同的长度,我使用了带有 NA 的 diff。

      library(dplyr)
      mydata %>%
      group_by(id) %>%
      mutate(price_diff = c(NA, diff(price)))%>%
      ungroup()
      

      【讨论】:

        【解决方案3】:

        已经

        transform(MyData, P1d = ave(Price, Good, FUN = function(x) c(NA, diff(x))))
        

        ave/gsubfn

        使用 gsubfn 包中的fn$ 可以稍微缩短最后一个解决方案:

        library(gsubfn)
        transform(MyData, P1d = fn$ave(Price, Good, FUN = ~ c(NA, diff(x))))
        

        dplyr

        library(dplyr)
        
        MyData %>% 
          group_by(Good) %>% 
          mutate(P1d = Price - lag(Price)) %>% 
          ungroup
        

        data.table

        library(data.table)
        
        dt <- data.table(MyData)
        dt[, P1d := c(NA, diff(Price)), by = Good]
        

        更新

        dplyr 现在使用 %&gt;% 而不是 %.%

        【讨论】:

          【解决方案4】:

          我在网上的一些 dpylr 教程之后想出了这段代码: 我的目标是添加新列,即公司(标识符 GVKEY)的研发(变量 xrd)的 5 年增长率。 RandD2015 是原始数据文件。

          通过使用管道函数 (%>%),您可以在 dplyr 中组合多个调用。希望这很有用(我在 Stack Overflow 中的第一个代码贡献)

          library(dplyr)
          
          RandDtest<- RandDec2015 %>% 
              group_by(GVKEY) %>%
              mutate(xrd5yr=xrd/lag(xrd,4)-1)
          

          【讨论】:

            【解决方案5】:

            这样可以轻松做到:

            library(reshape2)
            library(dplyr)
            
            MyNewData <- 
             MyData %.%
             melt(id = c("Good", "Day")) %.%
             dcast(Day ~ Good) %.%
             mutate(apples  = apples - lag(apples),
                 oranges = oranges - lag(oranges)) %.%
             melt(id = "Day", variable.name = "Good", value.name = "P1d") %.%
             merge(MyData) %.%
             arrange(Good, Day)
            

            问候

            【讨论】:

            • 感谢您的解决方案,但我不确定我是否会说“容易”。在我看来,这相当复杂。尤其是在设置面板和时间序列标识符后,Stata 会执行相同的命令(gen P1d = Price-L1.Price)。
            【解决方案6】:

            这就是我想出的。但它似乎至少没有效率:

            MyData$P1d <- c(NA, MyData$Price[-1]-MyData$Price[-nrow(MyData)])
            MyData$P1d[c(F,MyData$Good[-1]!=MyData$Good[-nrow(MyData)])] <- NA
            
            MyData
            
               Day Price    Good P1d
            1    1     8  apples  NA
            2    2    10  apples   2
            3    3     7  apples  -3
            4    4    11  apples   4
            5    5    14  apples   3
            6    1    12 oranges  NA
            7    2    11 oranges  -1
            8    3     9 oranges  -2
            9    4    14 oranges   5
            10   5    11 oranges  -3
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-04-05
              • 1970-01-01
              • 2019-01-19
              • 2017-09-01
              • 1970-01-01
              相关资源
              最近更新 更多