问题描述
我正在尝试有效地实现块自举技术来获取回归系数的分布.主要概述如下.
I'm trying to efficiently implement a block bootstrap technique to get the distribution of regression coefficients. The main outline is as follows.
我有一个面板数据集,并说公司和年份是指数.对于引导程序的每次迭代,我希望对n个主题进行替换采样.从这个样本中,我需要构建一个新的数据框,该数据框是每个采样对象的所有观测值的rbind()
堆栈,进行回归并提取系数.重复进行一堆迭代,比如说100.
I have a panel data set, and say firm and year are the indices. For each iteration of the bootstrap, I wish to sample n subjects with replacement. From this sample, I need to construct a new data frame that is an rbind()
stack of all the observations for each sampled subject, run the regression, and pull out the coefficients. Repeat for a bunch of iterations, say 100.
- 每个公司都有可能被多次选择,因此我需要在每次迭代的数据集中多次将其数据包含进来.
- 像下面一样,使用循环和子集方法似乎在计算上很麻烦.
- 请注意,对于我的真实数据帧n,其次数迭代比下面的示例大得多.
我最初的想法是使用split()
命令按主题将现有的数据框分成一个列表.在此使用
My thoughts initially are to break the existing data frame into a list by subject using the split()
command. From there, use
sample(unique(df1$subject),n,replace=TRUE)
以获取新列表,然后也许从plyr
包中实现quickdf
以构造一个新的数据框.
to get the new list, then perhaps implement quickdf
from the plyr
package to construct a new data frame.
慢速代码示例:
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
...
...
推荐答案
这样的事情如何?
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)
这篇关于从主题列表阻止引导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!