如何创建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中。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:2)
创建单独的函数以运行
run_sample <- function(bootsample) {
new <- bootsample[sample(1:nrow(bootsample),size = nrow(bootsample),replace = T),]
logit_test(new)
}
,然后replicate
次n
次。因此,对于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