r - 从包含单个引导程序的不等矩阵的列表中创建一个矩阵
问题描述
我试图从一个包含 N 个不相等矩阵的列表中创建一个矩阵......这样做的原因是为了制作 R 个单独的引导样本。在下面的示例中,您可以找到例如 2 家公司,其中我们有 1 家公司,其中 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 估计中处理矩阵......
解决方案
我为你找到了解决方案。据我所知,您有一个数据框,其中包含所有公司的所有观察结果,这些观察结果可能具有不同的面板长度。因此,您希望为每个公司提供与原始面板长度相同大小的 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)
最良好的祝愿,
蒂姆
推荐阅读
- hyperledger-fabric - 测试调用另一个链码的 GoLang Invoke 方法
- android - om.google.android.material.textfield.TextInputLayout 浮动在 EditText 的左侧可绘制对象上方
- c# - 使用数组中的现有元素向类中的变量声明值(按从上到下的顺序)
- android - 为什么在 Looper 主线程中运行 Handler
- java - 运行时类型擦除验证
- android - 视图内的 RecyclerView 页面指示器
- javascript - 将数据从 Chrome 扩展弹出窗口保存到速率受限的后端
- javascript - change() 不是函数
- visual-studio-code - 带有 Jupyter 交互窗口的 VS Code:如何更改 Python 交互窗口中的语法颜色(某些文本在深色主题上不可读)?
- unity3d - 在单个场景中关闭多人连接