﻿ 从主题列表中阻止引导程序 - 代码日志

#### 从主题列表中阻止引导程序

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

``````sample(unique(df1\$subject),n,replace=TRUE)
``````

``````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)
``````
https://stackoverflow.com/questions/11919808/block-bootstrap-from-subject-list