从列表中创建一个矩阵,该列表由各个引导程序的不相等矩阵组成

时间:2018-08-14 14:57:33

标签: r list statistics-bootstrap

我试图从包含N个不等矩阵的列表中创建一个矩阵... 这样做的原因是使R个独立的引导程序样本。 在下面的示例中,您可以找到例如2家公司,其中1家拥有10个,而1家拥有5个观察。

数据:

set.seed(7)
Time <- c(10,5)

xv <- matrix(c(rnorm(10,5,2), rnorm(5,20,1), rnorm(10,5,2), rnorm(5,20,1)), ncol=2);
y <- matrix( c(rnorm(10,5,2), rnorm(5,20,1))); 
z <- matrix(c(rnorm(10,5,2), rnorm(5,20,1), rnorm(10,5,2), rnorm(5,20,1)), ncol=2)

# create data frame of input variables which helps
# to conduct the rowise bootstrapping 
data <- data.frame (y = y, xv = xv, z = z); 
rows <- dim(data)[1]; 
cols <- dim(data)[2]; 

# create the index to sample from the different panels 
cumTime <- c(0, cumsum (Time)); 
index <- findInterval (seq (1:rows), cumTime, left.open = TRUE); 

# draw R individual bootstrap samples 
bootList <- replicate(R = 5, list(), simplify=F); 
bootList <- lapply (bootList, function(x) by (data, INDICES = index, FUN = function(x) dplyr::sample_n (tbl = x, size = dim(x)[1], replace = T))); 

---------- 取消列表 ---------

目前,我尝试这样做是错误的: 列表中只有1个条目的示例:

matrix(unlist(bootList[[1]], recursive = T), ncol = cols)

所需的输出仅为

bootList[[1]]

作为矩阵。

您是否知道如何执行此操作,以及在可能的情况下是否合理有效?

然后不幸的是,以缓慢的MLE估计来处理矩阵...

1 个答案:

答案 0 :(得分:0)

我为您找到了解决方案。根据我的收集,您有一个数据框,其中包含所有公司的所有观察结果,可能会有不同的面板长度。因此,您希望每个公司都拥有与原始面板长度相同大小的Bootstap示例。 您只需要添加公司指标

data$company = c(rep(1, 10), rep(2, 5)) # this could even be a factor.
L1 = split(data, data$company)
L2 = lapply(L1, FUN = function(s) s[sample(x = 1:nrow(s), size = nrow(s), replace = TRUE),] ) 

如果您想获得明智的引导采样,请在此处停止。如果您想单独估算

bootdata = do.call(rbind, L2)

最良好的祝愿

蒂姆

相关问题