【问题标题】:Create a for-loop to run the code 1000 times, saving each output in a data frame创建一个 for 循环来运行代码 1000 次,将每个输出保存在一个数据框中
【发布时间】:2019-09-07 08:23:03
【问题描述】:

如何创建一个 for 循环以使用 glm 进行引导并将每个引导样本的计算拟合系数保存在数据框中?

我有代码来获取我的示例引导程序并通过 glm 运行它。我怎样才能做到这一点 1000 次?

bootsample<-landslide.train[sample(1:nrow(landslide.train),size = nrow(landslide.train),replace = T),]



logit_test <- function(d,indices) {  
d <- d[indices,]  
fit <- glm(lslpts ~slope+cplan+cprof+elev+log10_carea, data = d, family = binomial('logit'))  
return(coef(fit))
}

logit_test(bootsample)

我正在使用的bootsample 数据的前 20 行

structure(list(X = c(974L, 781L, 497L, 446L, 251L, 755L, 1200L, 
157L, 1329L, 1375L, 1023L, 17071L, 17101L, 17981L, 158L, 1797L, 
1008L, 16251L, 17101L, 1021L), x = c(715197.726935, 714727.726935, 
713627.726935, 714727.726935, 713997.726935, 713997.726935, 713337.726935, 
715227.726935, 713797.726935, 715667.726935, 713897.726935, 715372.6991, 
714839.3558, 714877.2271, 713687.726935, 714017.6623, 714957.726935, 
713598.9416, 714839.3558, 715097.726935), y = c(9558796.759956, 
9558956.759956, 9560626.759956, 9560596.759956, 9558186.759956, 
9558826.759956, 9558596.759956, 9558816.759956, 9558616.759956, 
9557316.759956, 9558686.759956, 9557755.4511, 9557896.0971, 9558362.3616, 
9558526.759956, 9559267.4409, 9557886.759956, 9559476.7683, 9557896.0971, 
9558676.759956), lslpts = c(FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, 
TRUE, FALSE, TRUE, TRUE, FALSE), slope = c(11.4473142623901, 
32.2565155029297, 18.6946430206299, 46.6216201782227, 45.8349304199219, 
27.5694179534912, 12.916111946106, 18.9391193389893, 28.8392124176025, 
29.4122161865234, 42.0575790405273, 55.0987739562988, 52.2864074707031, 
49.1693649291992, 39.819580078125, 24.117280960083, 26.0378913879395, 
49.9017868041992, 52.2864074707031, 41.2809295654297), cplan = c(0.0876342952251434, 
-0.0120965261012316, 0.0383418053388596, -0.066392257809639, 
-0.00631508277729154, 0.0517702214419842, 0.0720687136054039, 
0.023954214528203, 0.0597645863890648, 0.049027718603611,  0.0450959205627441, 
0.0127706276252866, -0.0201852042227983, -0.079123854637146, 
-0.0227550193667412, -0.115558296442032, 0.0121484752744436, 
-0.00726922834292054, -0.0201852042227983, 0.0911215841770172
), cprof = c(0.00482346303761005, -0.0246740821748972, 0.00308212544769049, 
-0.011747082695365, 0.00434586964547634, 0.0114751122891903, 
0.00684649730101228, 0.00165533868130296, 0.00368972565047443, 
0.012421440333128, 0.00339623377658427, -0.000277994142379612, 
0.0185004360973835, -0.00426706159487367, -0.00908753089606762, 
-0.0154662737622857, 0.0114737479016185, 0.00092382647562772, 
0.0185004360973835, 0.00363937742076814), elev = c(2459.489, 
2309.752, 1941.864, 1819.428, 2390.502, 2446.091, 2260.571, 2451.212, 
2364.178, 3122.653, 2441.109, 2893.675, 2782.211, 2577.096, 2286.541, 
2289.337, 2847.725, 2238.277, 2782.211, 2455.642), log10_carea = c(2.17144513600567, 
3.53906071513275, 2.40238998909584, 4.21675627209886, 3.831078005355, 
2.53007719249512, 2.14356190611046, 2.52523083213064, 2.72167921604092, 
2.20130121256717, 2.65708910239568, 2.84823814320274, 3.10996220704593, 
3.24210575122298, 2.41171154689127, 3.19380584335536, 2.27759739429592, 
3.04796863600853, 3.10996220704593, 2.25602723140176)), row.names = c("202", 
"175", "227", "254", "147", "163", "201", "23", "128", "92", 
"119", "188", "89", "117", "125", "8", "126", "114", "89.1", 
"139"), class = "data.frame")

预计会得到 1000 组拟合系数,保存在 data.frame 中。

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 我不确定我是否正确理解了您的问题:您是否有如上图所示的 1000 个数据集?或者您想创建 1000 个数据子集并在每个子集上执行 glm
  • 创建 1000 个 bootsample 的集合(不是子集)并通过 glm 运行每个集合。或者我怎样才能运行第一块代码 1000 次。 @Julian_Hn 谢谢你
  • ?replicate
  • 复制后如何通过glm函数运行1000组样本并将拟合的系数保存在data.frame中?
  • 就像你的函数 logit_test 上的一个侧节点:它需要 2 个参数,但你只用一个参数调用它:logit_test(bootsample)。这种方式索引在函数中不可用,并且 d[indices,] 仅导致 d (我认为这是您的情况所需的结果,因为您之前是从 landslide.train 中设置引导程序的子集)。为了稍微清理一下,我会完全删除索引参数并将数据的子集传递给函数,或者我会保留索引参数并传递整个样本+应该使用的索引。

标签: r for-loop


【解决方案1】:

创建一个单独的函数来运行

run_sample <- function(bootsample) {
  new <- bootsample[sample(1:nrow(bootsample),size = nrow(bootsample),replace = T),]
  logit_test(new)
}

然后replicaten 次。所以对于n = 5,你会得到类似的东西

replicate(n, run_sample(bootsample))

#                     [,1]          [,2]          [,3]          [,4]          [,5]
#(Intercept)  -47.64062196  3.613736e+02 -318.61197205 -226.24745150 -6.988006e+01
#slope          0.28941853  3.461377e+00    1.46621389    1.57995941  3.769153e+00
#cplan       -370.75174467 -1.205658e+03 -735.59268065 -575.80482651 -6.559626e+02
#cprof       1683.30041594 -2.937241e+03 -491.06656409 1793.83257385  3.698299e+03
#elev          -0.02330619 -3.798966e-02    0.09420479    0.01867963 -9.567981e-03
#log10_carea   29.45346363 -1.409834e+02   -0.23893866   41.05755595 -2.683002e+01

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 2022-12-19
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2019-12-15
    • 2019-03-01
    相关资源
    最近更新 更多