阻止主题列表中的引导程序

bah*_*kev 11 regression r plyr statistics-bootstrap

我正在尝试有效地实现块引导技术来获得回归系数的分布.主要内容如下.

我有一个面板数据集,并说公司和年份是指数.对于bootstrap的每次迭代,我希望对n个主题进行替换.从这个样本中,我需要构建一个新的数据框,它是rbind()每个采样主题的所有观察的堆栈,运行回归,并拉出系数.重复一堆迭代,比如说100.

  • 每个公司都可能被多次选中,因此我需要在每个迭代的数据集中多次包含它.
  • 使用循环和子集方法,如下所示,似乎计算繁琐.
  • 请注意,对于我的实际数据帧,n和数字迭代比下面的示例大得多.

我的想法最初是使用split()命令按主题将现有数据框分成列表.从那里,使用

sample(unique(df1$subject),n,replace=TRUE)
Run Code Online (Sandbox Code Playgroud)

获取新列表,然后可能quickdfplyr包中实现构造新的数据框.

示例慢代码:

require(plm)
data("Grunfeld", package="plm")

firms = unique(Grunfeld$firm)
n = 10
iterations = 100
mybootresults=list()

for(j in 1:iterations){

  v = sample(length(firms),n,replace=TRUE)
  newdata = NULL

  for(i in 1:n){
    newdata = rbind(newdata,subset(Grunfeld, firm == v[i]))
  }

  reg1 = lm(value ~ inv + capital, data = newdata)
  mybootresults[[j]] = coefficients(reg1)

}

mybootresults = as.data.frame(t(matrix(unlist(mybootresults),ncol=iterations)))
names(mybootresults) = names(reg1$coefficients)
mybootresults

  (Intercept)      inv    capital
1    373.8591 6.981309 -0.9801547
2    370.6743 6.633642 -1.4526338
3    528.8436 6.960226 -1.1597901
4    331.6979 6.239426 -1.0349230
5    507.7339 8.924227 -2.8661479
...
...
Run Code Online (Sandbox Code Playgroud)

sea*_*ody 14

这样的事情怎么样:

myfit <- function(x, i) {
   mydata <- do.call("rbind", lapply(i, function(n) subset(Grunfeld, firm==x[n])))
   coefficients(lm(value ~ inv + capital, data = mydata))
}

firms <- unique(Grunfeld$firm)

b0 <- boot(firms, myfit, 999)
Run Code Online (Sandbox Code Playgroud)


dic*_*koa 5

您还可以使用带有固定块重采样方案的包中的tsboot函数boot

require(plm)
require(boot)
data(Grunfeld)

### each firm is of length 20
table(Grunfeld$firm)
##  1  2  3  4  5  6  7  8  9 10 
## 20 20 20 20 20 20 20 20 20 20


blockboot <- function(data) 
{
 coefficients(lm(value ~ inv + capital, data = data))

}

### fixed length (every 20 obs, so for each different firm) block bootstrap
set.seed(321)
boot.1 <- tsboot(Grunfeld, blockboot, R = 99, l = 20, sim = "fixed")

boot.1    
## Bootstrap Statistics :
##      original     bias    std. error
## t1* 410.81557 -25.785972    174.3766
## t2*   5.75981   0.451810      2.0261
## t3*  -0.61527   0.065322      0.6330

dim(boot.1$t)
## [1] 99  3

head(boot.1$t)
##        [,1]   [,2]      [,3]
## [1,] 522.11 7.2342 -1.453204
## [2,] 626.88 4.6283  0.031324
## [3,] 479.74 3.2531  0.637298
## [4,] 557.79 4.5284  0.161462
## [5,] 568.72 5.4613 -0.875126
## [6,] 379.04 7.0707 -1.092860
Run Code Online (Sandbox Code Playgroud)