简体   繁体   English

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

[英]Create a matrix from a list consisting of unequal matrices for individual bootstraps

I tried to create a matrix from a list which consists of N unequal matrices... The reason to do this is to make R individual bootstrap samples. 我试图从包含N个不相等矩阵的列表中创建一个矩阵...这样做的原因是使R个单独的引导程序样本。 In the example below you can find eg 2 companies, where we have 1 with 10 & 1 with just 5 observations. 在下面的示例中,您可以找到例如2家公司,其中我们有1家公司的10和1家公司只有5个观察值。

Data: 数据:

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))); 

---------- UNLISTING --------- ---------- 取消上市 ---------

Currently, I try do it incorrectly like this: Example for just 1 entry of the list: 当前,我尝试这样错误地执行此操作:仅列出1个条目的示例:

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

The desired output is just 所需的输出只是

bootList[[1]]

as a matrix. 作为矩阵。

Do you have an idea how to do this & if possible reasonably efficient? 您是否有一个想法,如果可能的话,应该有效率吗?

The matrices are then processed in unfortunately slow MLE estimations... 不幸的是,这些矩阵随后会以缓慢的MLE估计进行处理...

i found a solution for you. 我为您找到了解决方案。 From what i gather, you have a Dataframe containing all observations of all companies, which may have different panel lengths. 根据我的收集,您有一个数据框,其中包含所有公司的所有观察结果,可能会有不同的面板长度。 And as a result you would like to have a Bootstap sample for each company of same size as the original panel length. 因此,您希望每个公司都拥有与原始面板长度相同大小的Bootstap示例。 You mearly have to add a company indicator 您必须先添加公司指标

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),] ) 

stop here if you would like to have saperate bootstap samples eg in case you want to estimate seperately 如果您想获得合理的自助抽样,请在此处停止,例如,如果您要单独估算

bootdata = do.call(rbind, L2)

Best wishes, 最好的祝愿,

Tim 蒂姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM