【问题标题】:time to event for panel data面板数据的事件时间
【发布时间】:2012-06-18 20:17:57
【问题描述】:

我有一个国家年份的面板数据集。我想计算事件发生后的时间,以及每个国家/地区的事件总数,我可以随着时间的推移而衰减。我正在使用doBy 包中的timeSinceEvent 函数,该函数返回一个具有我想要的值的数据框,但我无法将其应用于我的主df。

structure(list(ccode.a = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 
41L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 
42L, 42L, 42L, 42L, 42L), year = c(1975, 1976, 1977, 1978, 1979, 
1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 
1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 
2002, 2003, 2004, 2005, 2006, 2007, 2008, 1975, 1976, 1977, 1978, 
1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 
1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 1975, 1976, 1977, 
1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 
1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 1975, 1976, 
1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 
1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 
1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 1975, 
1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 
1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 
1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 
1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004), onset.a = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("ccode.a", "year", 
"onset.a"), row.names = c(NA, 200L), class = "data.frame")

我试过用这个:

last.step <- function(x) {
  temp <- timeSinceEvent(x$onset.a, x$year)
  cbind(x[,1],temp) #timeSinceEvent cuts off the country ID
}
result <- do.call("rbind", by(data, data$ccode.a, last.step))

还有

test <- by(data, data$ccode.a, function(x) timeSinceEvent(data$onset.a, data$year))

收效甚微。我单步执行了这个函数,它似乎在做我想做的事,但我猜我调用它的方式有问题?

【问题讨论】:

    标签: r data-manipulation


    【解决方案1】:

    在我看来,问题只是ccode.a==20 没有事件,因此timeSinceEvent 在应用于该子集时返回NULL。这意味着last.step 为两个ccode.as 返回不同维度的数据帧,因此rbind 失败。

    不完全是一个解决方案,但也许更好地了解问题所在已经有所帮助。

    【讨论】:

    • 因此,有些横截面单元没有任何起始点,但当然有很多。我不明白为什么返回的 df 会有不同的尺寸。它们应该与我传递给last.step 的df 中的年数相匹配,只是缺少timeSinceEvent 返回的一些变量的值。除非我误会了..
    • 不同的不是行数,而是列数。对于示例中的第一个国家 last.step 返回一个包含 9 列的数据框(因为函数内部的 timeSinceEvent 返回一个包含 8 列的数据框),而对于第二个国家/地区 last.step 返回一个只有 1 列的数据框(因为函数内部的timeSinceEvent 返回NULL)。并且由于列数不匹配rbind将不起作用。
    • 啊好吧,这更有意义。我只是在 ID 代码上测试该功能,该代码恰好至少有 1 次发作。谢谢。
    【解决方案2】:

    由于有空列,您应该在plyr 中使用rbind.fill()。它将用 na 填充空的列

    last.step <- function(x) {
      temp <- timeSinceEvent(x$onset.a, x$year)
      cbind(x[,1],temp) #timeSinceEvent cuts off the country ID
    }
    result <- do.call(rbind.fill, by(data, data$ccode.a, last.step))
    

    但是,这不会返回“空”lists,即只有 x[,1] 的那个。它只会rbind 那些lists 里面有data.frame。我不知道这是否是预期的行为和/或您想要的。

    【讨论】:

    • 这看起来像我想要做的,但是if 声明是怎么回事?虽然这是期望的行为。
    • 很抱歉我要添加一个 if 但后来我认为它不需要但忘记删除它。已编辑
    • 我收到了这个错误Error in sss[ii, 2]:sss[ii, 3] : NA/NaN argument
    • 我也在cbind上方的行中尝试了if(temp != NULL)并得到了错误Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : length of 'dimnames' [2] not equal to array extent
    • @Zach 使用您提供的相同数据吗?
    【解决方案3】:

    最终不得不稍微修改doBy 包中的timeSinceEvent。这是有效的最终代码。感谢 lselzer 在 plyr 中指出 rbind.fill 和 RoyalTS 在 yvar 参数全为零时指出 timeSinceEvent 返回 null

    panel.tse <- function(yvar, tvar = seq_along(yvar)){
       if (!(is.numeric(yvar) | is.logical(yvar))){
            stop("yvar must be either numeric or logical")
        }
       yvar[is.na(yvar)] <- 0
       event.idx <- which(yvar == 1)
       run <- cumsum(yvar)
       un <- unique(run)
       tlist <- list()
       for (i in 1:length(un)){
         v <- un[[i]]
         y <- yvar[run == v]
         t <- tvar[run == v]
         t <- t - t[1]
         tlist[[i]] <- t
       }
       timeAfterEvent <- unlist(tlist)
       timeAfterEvent[run == 0] <- NA
       run[run == 0] <- NA
       ans <- cbind(data.frame(yvar = yvar, tvar = tvar), run, tae = timeAfterEvent)
       return(ans)
     }
    
    last.step <- function(x) {
      temp <- panel.tse(x$onset.a, x$year)
      cbind(x[,1],temp) 
    }
    
    result <- do.call(rbind.fill, by(data, data$ccode.a, last.step))
    

    【讨论】:

      猜你喜欢
      • 2016-06-29
      • 2021-12-18
      • 1970-01-01
      • 2018-10-10
      • 2013-06-06
      • 2012-02-13
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      相关资源
      最近更新 更多