【问题标题】:tseries - block bootstrap two series same order of resamplingtseries - 块引导两个系列相同的重采样顺序
【发布时间】:2017-12-28 14:16:03
【问题描述】:

例如

    require(tseries)
    series1 <- c(100,140,150,200,150,260,267,280,300,350)
    series2 <- c(500,600,250,300,350,500,100,130,50,60)
    data <- data.frame("series1" = series1, "series2" = series2)
ts  = tsbootstrap(data$series1, m=1, b=2, type="block", nb=10)
ts <- as.data.frame(ts)
head(ts)

> head(ts)
   V1  V2  V3  V4  V5  V6  V7  V8  V9 V10
1 280 280 150 200 100 300 150 140 100 260
2 300 300 200 150 140 350 260 150 140 267
3 140 260 140 260 267 200 150 150 260 300
4 150 267 150 267 280 150 200 200 267 350
5 260 100 260 150 300 100 150 267 100 200
6 267 140 267 200 350 140 260 280 140 150

我们现在有两个块并以不同的顺序拼接在一起。我的问题是,如何通过块 boostrap 来“重新洗牌” series1 和 series2,同时保持两个系列的块顺序相同?

例如..如果我们将块设置为 2,它会抓取 2 个块,比如说它的位置 5,6(共 10 个)。它抓取元素 5,6 并将其移动到位置 1,2...这是用于系列 1 ,对于系列 2,它抓取元素 5,6 并移动到位置 1,2。这样我保持两个系列的顺序相同,这可能吗?

到目前为止,我已尝试将 series1 和 series2 合并为一个新列。这样,当使用引导程序时,它将两个系列移动到相同的位置:

    data <- transform(data, ts.merge=paste(series1, series2, sep=","))
head(data)
  series1 series2 ts.merge
1     100     500  100,500
2     140     600  140,600
3     150     250  150,250
4     200     300  200,300
5     150     350  150,350
6     260     500  260,500

但是,, 分隔符与 tseries 不兼容...

Error in FUN(newX[, i], ...) : 
  NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning messages:
1: In as.vector(x, mode = "double") : NAs introduced by coercion
2: In as.vector(x, mode = "double") : NAs introduced by coercion

我也尝试使用分隔符 "" 但是,我不确定之后如何区分两个数值以便将它们分开(请注意,我的现实生活示例不仅仅是如上所示的三位数值,否则我可以将它们分成两半之后)

【问题讨论】:

    标签: r time-series statistics-bootstrap


    【解决方案1】:

    花了我一整天,但这是一个手动解决方案,将每行重新采样:

        # Random Data
        data=matrix(rnorm(20*100), ncol = 2)
        data=as.data.frame(data)
        # Set block size
        reps <- NROW(data)/5 # Set group number
        data$id <- rep(1:reps,each=5) # each = 5 corresponds to number of blocks to bootstrap by (5 in this case)
        # Id data
        IDs<-unique(data$id)
        runs <- 1:1000
        temp <- list()
        # Function for bootstrap 1x data frame
        # subsets data by id number
        # Resamples the subsets
        bootSTRAP = function(x){
          for (i in 1:length(IDs)){ 
            temp[i] <- list(data[data$id==IDs[i],])
          }
          out <- sample(temp,replace=TRUE)
          df <- do.call(rbind, out)
        }
    
        # Loop for running it a 1000 times
        runs <- 1:1000
        run.output <- list()
        i=1
        for (i in 1:length(runs)){    # Length of optimization
          tryCatch({
            temp.1 <- bootSTRAP(runs[i])
            #cum_ret <- rbind.data.frame(cum_ret, temp)
            run.output[[i]] <- cbind.data.frame(temp.1)
            ptm0 <- proc.time()
            Sys.sleep(0.1)  
            ptm1=proc.time() - ptm0
            time=as.numeric(ptm1[3])
            cat('\n','Iteration',i,'took', time, "seconds to complete")
          }, error = function(e) { print(paste("i =", i, "failed:")) })
        }
    
    # cbind outputs
    master <- do.call(cbind, run.output)
    # Rename columns 
    col.ids <- rep(1:1000,each=3)
    cnames   <- paste(col.ids)
    colnames(master) <- cnames
    

    【讨论】:

      【解决方案2】:

      如果目标是保持 series1 和 series2 行同步,您可以在创建“数据”时添加索引,如下所示:

      data <- data.frame("series1" = series1, "series2" = series2, index = 
      seq(1:length(series1))) 
      

      然后将数据字段更改为引导到'index',如下所示:

      ts  = tsbootstrap(data$index, m=1, b=2, type="block", nb=10)
      

      【讨论】:

      • 好主意,但是,引导程序将仅重新采样 data$index 列,而不是整个数据框。可能必须使用 dplyr 制作自定义解决方案,并根据组或其他东西随机重新洗牌
      【解决方案3】:

      试试:

      ts.index    = tsbootstrap(index(series1), m=1, b=2, type="block", nb=10)
      series1[ts.index[,1]]
      series2[ts.index[,1]]
      

      接下来,您可以根据需要管理最终的数据框。

      【讨论】:

      • 这也只引导索引值 1:10
      • 没关系,因为您在 series1 中只有 10 个值,在 series2 中只有 10 个值。
      • 我的意思是......它在索引中重新采样数字 1:10,而不是实际的系列 1 值
      猜你喜欢
      • 2015-03-08
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 2019-06-22
      相关资源
      最近更新 更多