0

我正在尝试根据当前仅定期观察到的内容创建每日时间序列数据集。我可以成功地为单个案例执行所需的操作,但无法确定如何扩展到整个数据集。例如:

        UNIT <- c(100,100, 200, 200, 200, 200, 200, 300, 300, 300,300)
        STATUS <- c('ACTIVE','INACTIVE','ACTIVE','ACTIVE','INACTIVE','ACTIVE','INACTIVE','ACTIVE','ACTIVE',
                    'ACTIVE','INACTIVE') 
        TERMINATED <- as.Date(c('1999-07-06' , '2008-12-05' , '2000-08-18' , '2000-08-18' ,'2000-08-18' ,'2008-08-18',
                        '2008-08-18','2006-09-19','2006-09-19' ,'2006-09-19' ,'1999-03-15')) 
        START <- as.Date(c('2007-04-23','2008-12-06','2004-06-01','2007-02-01','2008-04-19','2010-11-29','2010-12-30',
                   '2007-10-29','2008-02-05','2008-06-30','2009-02-07'))
        STOP <- as.Date(c('2008-12-05','2012-12-31','2007-01-31','2008-04-18','2010-11-28','2010-12-29','2012-12-31',
                  '2008-02-04','2008-06-29','2009-02-06','2012-12-31'))
        TEST <- data.frame(UNIT,STATUS,TERMINATED,START,STOP)
        TEST                   

这是对间隔单位的观察:

   UNIT   STATUS TERMINATED      START       STOP
1   100   ACTIVE 1999-07-06 2007-04-23 2008-12-05
2   100 INACTIVE 2008-12-05 2008-12-06 2012-12-31
3   200   ACTIVE 2000-08-18 2004-06-01 2007-01-31
4   200   ACTIVE 2000-08-18 2007-02-01 2008-04-18
5   200 INACTIVE 2000-08-18 2008-04-19 2010-11-28
6   200   ACTIVE 2008-08-18 2010-11-29 2010-12-29
7   200 INACTIVE 2008-08-18 2010-12-30 2012-12-31
8   300   ACTIVE 2006-09-19 2007-10-29 2008-02-04
9   300   ACTIVE 2006-09-19 2008-02-05 2008-06-29
10  300   ACTIVE 2006-09-19 2008-06-30 2009-02-06
11  300 INACTIVE 1999-03-15 2009-02-07 2012-12-31            

我想在开始和结束日期的整个范围内每天取每个单位并复制“状态”和“终止”(以及大型数据集中的 N 个其他协变量)上的值。为一张唱片做这件事......

        A <-  seq(TEST$START[1], TEST$STOP[1], "days") #vector of relevant date sequences 

        #keeping the old data, now with daily date "fill" 
        B <- matrix(NA, length(A), dim(TEST[-c(4,5)])[2]) 
        C <- data.frame(A,B)

        #carry forward observations on covariates through date range 
        TEST[-c(4,5)][1,]  #note terminated has the proper date status:
        UNIT STATUS TERMINATED
         1  100 ACTIVE 1999-07-06

        #now the TERMINATED loses its 'date' status for some reason
        C[-c(1)][1,] <- TEST[-c(4,5)][1,] 
        D <-  na.locf(C)
        colnames(D)[2:4] <-colnames(TEST)[1:3]
        colnames(D)[1] <- "DATE"
        head(D)

        DATE UNIT STATUS TERMINATED
1 2007-04-23  100      1      10778
2 2007-04-24  100      1      10778
3 2007-04-25  100      1      10778
4 2007-04-26  100      1      10778
5 2007-04-27  100      1      10778
6 2007-04-28  100      1      10778

第一行的观测值在 START 到 END 的范围内重复,并创建一个新向量:整个期间的每日时间序列。我想对第 2 行执行此操作,将其绑定到 D 等等,按 UNIT 分析。我用 na.locf 编写了一个 for 循环,但未能成功进行概括:

for(i in 1:nrow(TEST)){
  for(j in 0:nrow(TEST)-1) {
  A <-  seq(TEST$START[i], TEST$STOP[i], "days")

  B <- matrix(NA, length(A), dim(TEST[-c(4,5)])[2])
  C <- data.frame(A,B)

  C[-c(1)][1,] <- TEST[-c(4,5)][i,] 
  assign(paste("D",i, sep=""),na.locf(C)) 

  #below here the code does not work. R does not recognize i and j as I intend
  #I haven't been able to overcome this using assign, evaluate etc. 
  colnames(Di)[2:4] <-colnames(TEST)[1:3]
  colnames(Di)[1] <- "DATE"

  D0 <- matrix(NA, 1, dim(Di)[2])
  assign(paste("D", j, sep = ""),Dj)
  rbind(Di,Dj)

   }
  }            

单一记录“解决方案”的明显问题是处理“终止”日期。就在使用 na.locf 之前,它失去了它的日期状态。

我希望有一种更好的方式来看待这个问题,而我只是因为无知而把自己埋在了复杂之中。

4

1 回答 1

2

在 SQL 中比较容易做到,所以可以使用sqldf,它将 data.frames 视为 SQL 表。

dates <- data.frame( date = seq.Date( min(TEST$START), max(TEST$STOP), by = 1 ) )
library(sqldf)
result <- sqldf( "
  SELECT *
  FROM TEST, dates
  WHERE START <= date AND date <= STOP
" )
head( result )

如果数据很大,可能值得将数据存储在数据库中,并在那里进行计算。

# With SQLite, a database is just a file
library(RSQLite)
connection <- dbConnect( SQLite(), "/tmp/test.db" )  

# Copy the data.frames to the "Test" and "Dates" table.
# When transfering data across systems, it is often easier 
# to convert dates to strings.
convert_dates <- function(d) {
  as.data.frame( lapply( 
    d, 
    function(u) if( "Date" %in% class(u) ) as.character(u) else u 
  ) ) 
}
dbWriteTable(connection, "Test",  convert_dates(TEST),  row.names = FALSE )
dbWriteTable(connection, "Dates", convert_dates(dates), row.names = FALSE )

# Check how many rows the query has: it could be 
# that the result does not fit in memory
dbGetQuery( connection, "
  SELECT COUNT(*) 
  FROM   Test, Dates 
  WHERE  start <= date AND date <= stop
" )

# If it is reasonable, retrieve all the data
dbGetQuery( connection, "
  SELECT * 
  FROM   Test, Dates 
  WHERE  start <= date AND date <= stop
" )

# If not, only retrieve what you need
dbGetQuery( connection, "
  SELECT * 
  FROM   Test, Dates 
  WHERE  start <= date AND date <= stop
  AND    '2013-04-01' <= date AND date <= '2013-04-30'
" )
于 2013-03-30T09:51:04.967 回答