如何通过迭代R中的列名来对rJAGS MCMC后验分布进行子集化?

时间:2018-11-28 14:59:37

标签: r bayesian rjags

这是我的MCMC示例的快照:

enter image description here 尽管以samples[,1:15]的形式逐列进行子集化是很直观的,但显然我想从对列号的硬编码转换为公式化子集。

例如,这是我(成功地)通过beta子集的方式:

p <- ncol(X.mod) # p=8   
head(samples[, c(paste("beta[", 1:p, "]", sep = "")])

问题是,我无法弄清楚如何通过迭代列名来以公式化方式对这些样本进行子集化,这与我的y后验分布相关:

enter image description here

当我尝试通过np进行子集化时,会发生以下情况:

n <- nrow(X.mod) # n=52
p <- ncol(X.mod) # p=8
head(samples[, c(paste("y[",1:n,",", 1:p, "]", sep = ""))])

enter image description here

该子集无法选择所有y,因为1:n不会重复1:p

如何为y[1:n,1:p]后验分布的所有子集?

样本数据:

samples=list(structure(c(-51.2677209544157, -21.4563066467399, 16.8163390467341, 
9.4403233375381, 48.1649529600012, 527.042041706746, 512.571280385213, 
573.056966431797, 492.87177606163, 515.43937748028, 1341.16877015021, 
1314.46850289829, 1299.28259509269, 1301.53294554643, 1320.78264289766, 
299.286629146479, 301.909535290067, 300.894814624028, 300.804302607962, 
300.232964708285, -3006.34967221901, -2874.20964445009, -2788.20702516807, 
-2769.57351439289, -2841.15094775797, 18978.8438818469, 18861.3329448127, 
18835.2759211416, 18843.9480121326, 19006.8849778275, 90.8125367572606, 
91.0541633678037, 90.9102211374358, 91.1560822802519, 91.0031643433501, 
-20.5391638580293, -20.598494387781, -20.6003012597676, -20.6303435292573, 
-20.6369790962729, 586.880362343332, 580.677805863983, 573.044264526859, 
574.309536298313, 567.446409074497, -121.00760356184, -120.443670943994, 
-119.403902369317, -120.025713139514, -120.12086457878, 262.761481825014, 
262.8571924679, 263.091693357767, 263.636726737316, 263.836394193792, 
2.99998007884072, 2.99942379397774, 2.99994292246781, 2.99999437834094, 
2.99999393092779, 43684833.8709732, 43853657.9085946, 43965230.3484224, 
43913903.0394132, 43908130.4319428, 26293534.6902094, 26383613.17045, 
26147749.137356, 26098414.0977608, 26280692.1742495, 2.28912396222813e-08, 
2.28031148982903e-08, 2.27452464612387e-08, 2.27718314881392e-08, 
2.27748253037098e-08, 51147.5432500239, 52617.8839201446, 52387.1302975914, 
51694.3819485642, 52661.5824788161, 8288.63042107145, 7271.35048802946, 
8004.90224346885, 4927.79219294683, 6213.91939841326, -121910.191472484, 
-122362.741428785, -124110.51332543, -120733.292837535, -121831.210377512, 
230071.066478833, 230287.794814824, 230860.030232403, 228148.880125279, 
230333.173973695, 58966.85989857, 58290.5265589913, 58966.2214960444, 
59519.8070715974, 58802.1396637729, 158277.070604939, 158278.084243585, 
159558.969341073, 158206.366529875, 159426.208472455, -34631.8355987461, 
-34609.6845179111, -35917.7360955642, -35814.2800339074, -36978.4911829625, 
-210042.316842331, -208652.462028129, -207671.232944661, -210062.270909624, 
-211754.369098217, 213171.553198798, 216038.734915484, 214225.650295615, 
217038.211300394, 216500.160309277, 384888.807797524, 386473.402753383, 
387137.68468714, 386364.186716497, 384817.509068656, -47476.4513690518, 
-47494.6913172312, -46897.9242162581, -48308.4448066041, -46508.2339745099, 
-37992.2553102205, -41389.0487476472, -38890.8257286547, -38346.9125216416, 
-39100.5425333134, 7949.2782206436, 9989.52625801751, 8942.85996626707, 
8332.73330020014, 7834.47084680051, 62141.308245952, 67139.4502479832, 
63832.4336294562, 60989.0636476163, 63079.6258807178, -16093.0306896024, 
-13553.6863631994, -15273.1350833869, -12514.0536652939, -13847.8162498959, 
4740.84679297971, 5553.23308406456, 4646.47350755076, 5203.83240565698, 
3836.21264149301, -42452.3446436508, -44176.1773649656, -44254.4042803305, 
-42368.0239702027, -43843.4858454448, 23357.5860159426, 19323.9544070424, 
19637.6033108415, 21705.1873323843, 21024.8148051923, 18776.4359330499, 
19286.7821182529, 15618.4910885203, 17440.5647607157, 18530.2743457382, 
694380.200995391, 696718.995471421, 693905.474727437, 695063.192208464, 
695165.567173854, -43586.9966504837, -45505.3179234906, -43643.7473655112, 
-43058.9839554769, -43724.8308933968, -40043.0022511632, -39014.236458568, 
-38063.2925580272, -39235.2603601708, -38927.2812579938, -463.298832842201, 
2822.19621960134, 2740.63785430782, 149.257113779129, 1431.76075651258, 
-39913.5482978342, -39620.7226918087, -40950.5478134214, -40039.0598099734, 
-39743.0636592209, -11262.1593685704, -11407.938151165, -9562.48525030229, 
-10134.1845743571, -10050.1172896042, 335835.254975648, 337999.045289531, 
337194.341376322, 338018.485648888, 335416.635970107, 23408.103811026, 
21144.4341406962, 21915.8382523749, 20431.5874969477, 22195.0238358184, 
11169.0206742275, 11772.4584342754, 12825.9731500737, 13211.0567986642, 
10618.099051088, 8479.9109630537, 7013.25500204239, 9382.6346215863, 
6961.04502216892, 7047.00952673883, -37681.1500934371, -37354.6452405205, 
-37878.4023105368, -37433.5592530078, -35855.6025831903, -146001.886786988, 
-147224.189953544, -145506.506570676, -147908.944079161, -145594.031371361, 
38118.8521232409, 37619.8858767155, 38388.6938356845, 35848.3258117989, 
37915.9597084273, -57573.6586200799, -59886.9943499311, -59110.030726184, 
-61325.9710661656, -59195.4058499934, -29487.0449745073, -30584.3155751739, 
-29424.1147117356, -27894.8314082331, -28663.3600550983, -96536.9208466624, 
-97412.1971464943, -96998.1614528458, -95951.2597070591, -99589.8781640841, 
-144891.97507194, -144133.996148857, -146904.501665921, -147366.361999527, 
-147546.102127573, -114719.855798716, -116688.792729423, -117296.137041345, 
-117485.559230925, -116814.014195805, -65638.5909352318, -63757.0976210234, 
-64996.2550329519, -65685.9292800577, -66224.8557774864, -55126.4040901648, 
-54167.4359328517, -55661.407854157, -56257.9266654811, -53833.9423088926, 
-107659.048785587, -105271.927487463, -106844.285557872, -107351.456685968, 
-105062.537456003, 36800.151051035, 36614.1125896956, 35383.4128487622, 
36071.7216164739, 34679.5993594339, -88278.5285388991, -86870.6769322943, 
-87487.5243492138, -88535.0891267321, -87215.625811017, -40395.9008156264, 
-40285.8302776255, -39333.0129216309, -40082.7263990535, -36625.6824906002, 
-89292.6539848126, -88879.6270954307, -88552.1809735678, -89447.4769006171, 
-90529.5219129098, -49519.6416600316, -47606.9722392849, -46510.0185968123, 
-47633.8104654962, -45788.0300150143, -6733.68790270814, -8175.62499754323, 
-6656.38624378975, -7933.87717601099, -8826.27342533908, -24296.8117513574, 
-21040.1429957975, -22854.3352422732, -24540.3392066565, -22262.6674224603, 
36368.7050027012, 35771.2638538132, 37582.6249503995, 37474.0672148882, 
34786.309716829, 61238.0631051517, 61395.3553743695, 62595.9115888391, 
62409.4707276882, 61469.3534951057, -8946.0324795431, -8446.70860487308, 
-9544.72651309003, -8679.57342708674, -9576.25960931779, -85699.6074359973, 
-85155.9899379678, -85845.0121999715, -85085.0799491324, -83116.5520568948, 
-69475.3035748836, -71336.8481369083, -69854.4490875094, -68279.0163230788, 
-68475.4397308316, 244333, 244333, 244333, 244333, 244333, 13297, 
13297, 13297, 13297, 13297, 19162, 19162, 19162, 19162, 19162, 
243937, 243937, 243937, 243937, 243937, 76185, 76185, 76185, 
76185, 76185, 233828, 233828, 233828, 233828, 233828, 2296, 2296, 
2296, 2296, 2296, 406547, 406547, 406547, 406547, 406547, 370495, 
370495, 370495, 370495, 370495, 486495, 486495, 486495, 486495, 
486495, 8792, 8792, 8792, 8792, 8792, 59675, 59675, 59675, 59675, 
59675, 69841, 69841, 69841, 69841, 69841, 26473, 26473, 26473, 
26473, 26473, 4005, 4005, 4005, 4005, 4005, 1702, 1702, 1702, 
1702, 1702, 132, 132, 132, 132, 132, 50975, 50975, 50975, 50975, 
50975, 9685, 9685, 9685, 9685, 9685, 1045967, 1045967, 1045967, 
1045967, 1045967, 216288, 216288, 216288, 216288, 216288, 18604, 
18604, 18604, 18604, 18604, 2129, 2129, 2129, 2129, 2129, 12801, 
12801, 12801, 12801, 12801, 3888, 3888, 3888, 3888, 3888, 372269, 
372269, 372269, 372269, 372269, 13215, 13215, 13215, 13215, 13215, 
27538, 27538, 27538, 27538, 27538, 2392, 2392, 2392, 2392, 2392, 
1957, 1957, 1957, 1957, 1957, 760, 760, 760, 760, 760, 118500, 
118500, 118500, 118500, 118500, 30644, 30644, 30644, 30644, 30644, 
18752, 18752, 18752, 18752, 18752, 146876, 146876, 146876, 146876, 
146876, 27131, 27131, 27131, 27131, 27131, 13543, 13543, 13543, 
13543, 13543, 6153, 6153, 6153, 6153, 6153, 4651, 4651, 4651, 
4651, 4651, 4364, 4364, 4364, 4364, 4364, 56880, 56880, 56880, 
56880, 56880, 1050, 1050, 1050, 1050, 1050, 12421, 12421, 12421, 
12421, 12421, 109042, 109042, 109042, 109042, 109042, 1461, 1461, 
1461, 1461, 1461, 6319, 6319, 6319, 6319, 6319, 472, 472, 472, 
472, 472, 20443, 20443, 20443, 20443, 20443, 168310, 168310, 
168310, 168310, 168310, 44013, 44013, 44013, 44013, 44013, 1120, 
1120, 1120, 1120, 1120, 1772, 1772, 1772, 1772, 1772, 476631, 
476631, 476631, 476631, 476631, 12789, 12789, 12789, 12789, 12789, 
21351, 21351, 21351, 21351, 21351, 414038, 414038, 414038, 414038, 
414038, 79148, 79148, 79148, 79148, 79148, 301104, 301104, 301104, 
301104, 301104, 2962, 2962, 2962, 2962, 2962, 723272, 723272, 
723272, 723272, 723272, 390602, 390602, 390602, 390602, 390602, 
486495, 486495, 486495, 486495, 486495, 8846, 8846, 8846, 8846, 
8846, 199212, 199212, 199212, 199212, 199212, 70980, 70980, 70980, 
70980, 70980, 71552, 71552, 71552, 71552, 71552, 34019, 34019, 
34019, 34019, 34019, 1713, 1713, 1713, 1713, 1713, 66, 66, 66, 
66, 66, 49359, 49359, 49359, 49359, 49359, 12638, 12638, 12638, 
12638, 12638, 1305076, 1305076, 1305076, 1305076, 1305076, 246909, 
246909, 246909, 246909, 246909, 14680, 14680, 14680, 14680, 14680, 
3276, 3276, 3276, 3276, 3276, 14159, 14159, 14159, 14159, 14159, 
5718, 5718, 5718, 5718, 5718, 468027, 468027, 468027, 468027, 
468027, 12470, 12470, 12470, 12470, 12470, 256106, 256106, 256106, 
256106, 256106, 4530, 4530, 4530, 4530, 4530, 3080, 3080, 3080, 
3080, 3080, 1143, 1143, 1143, 1143, 1143, 121909, 121909, 121909, 
121909, 121909, 28563, 28563, 28563, 28563, 28563, 164844, 164844, 
164844, 164844, 164844, 269901, 269901, 269901, 269901, 269901, 
17939, 17939, 17939, 17939, 17939, 14969, 14969, 14969, 14969, 
14969, 6506, 6506, 6506, 6506, 6506, 5901, 5901, 5901, 5901, 
5901, 10427, 10427, 10427, 10427, 10427, 132296, 132296, 132296, 
132296, 132296, 850, 850, 850, 850, 850, 15330, 15330, 15330, 
15330, 15330, 151457, 151457, 151457, 151457, 151457, 3746, 3746, 
3746, 3746, 3746, 10349, 10349, 10349, 10349, 10349, 522, 522, 
522, 522, 522, 35006, 35006, 35006, 35006, 35006, 157159, 157159, 
157159, 157159, 157159, 42363, 42363, 42363, 42363, 42363, 1334, 
1334, 1334, 1334, 1334, 2726, 2726, 2726, 2726, 2726, 506833, 
506833, 506833, 506833, 506833, 12512, 12512, 12512, 12512, 12512, 
13392, 13392, 13392, 13392, 13392, 397624, 397624, 397624, 397624, 
397624, 58609, 58609, 58609, 58609, 58609, 274949, 274949, 274949, 
274949, 274949, 1764, 1764, 1764, 1764, 1764, 856448, 856448, 
856448, 856448, 856448, 367996, 367996, 367996, 367996, 367996, 
662737, 662737, 662737, 662737, 662737, 6585, 6585, 6585, 6585, 
6585, 180863, 180863, 180863, 180863, 180863, 69859, 69859, 69859, 
69859, 69859, 24343, 24343, 24343, 24343, 24343, 4312, 4312, 
4312, 4312, 4312, 1823, 1823, 1823, 1823, 1823, 72, 72, 72, 72, 
72, 42823, 42823, 42823, 42823, 42823, 9680, 9680, 9680, 9680, 
9680, 1191827, 1191827, 1191827, 1191827, 1191827, 183967, 183967, 
183967, 183967, 183967, 11172, 11172, 11172, 11172, 11172, 2326, 
2326, 2326, 2326, 2326, 15195, 15195, 15195, 15195, 15195, 6702, 
6702, 6702, 6702, 6702, 389572, 389572, 389572, 389572, 389572, 
18055, 18055, 18055, 18055, 18055, 91818, 91818, 91818, 91818, 
91818, 2833, 2833, 2833, 2833, 2833), .Dim = c(5L, 200L), .Dimnames = list(
    NULL, c("b_0", "b_1", "b_2", "beta[1]", "beta[2]", "beta[3]", 
    "beta[4]", "beta[5]", "beta[6]", "beta[7]", "beta[8]", "phi", 
    "sigma.sq", "sigma.sq.w", "tau.sq", "w[1]", "w[2]", "w[3]", 
    "w[4]", "w[5]", "w[6]", "w[7]", "w[8]", "w[9]", "w[10]", 
    "w[11]", "w[12]", "w[13]", "w[14]", "w[15]", "w[16]", "w[17]", 
    "w[18]", "w[19]", "w[20]", "w[21]", "w[22]", "w[23]", "w[24]", 
    "w[25]", "w[26]", "w[27]", "w[28]", "w[29]", "w[30]", "w[31]", 
    "w[32]", "w[33]", "w[34]", "w[35]", "w[36]", "w[37]", "w[38]", 
    "w[39]", "w[40]", "w[41]", "w[42]", "w[43]", "w[44]", "w[45]", 
    "w[46]", "w[47]", "w[48]", "w[49]", "w[50]", "w[51]", "w[52]", 
    "y[1,1]", "y[2,1]", "y[3,1]", "y[4,1]", "y[5,1]", "y[6,1]", 
    "y[7,1]", "y[8,1]", "y[9,1]", "y[10,1]", "y[11,1]", "y[12,1]", 
    "y[13,1]", "y[14,1]", "y[15,1]", "y[16,1]", "y[17,1]", "y[18,1]", 
    "y[19,1]", "y[20,1]", "y[21,1]", "y[22,1]", "y[23,1]", "y[24,1]", 
    "y[25,1]", "y[26,1]", "y[27,1]", "y[28,1]", "y[29,1]", "y[30,1]", 
    "y[31,1]", "y[32,1]", "y[33,1]", "y[34,1]", "y[35,1]", "y[36,1]", 
    "y[37,1]", "y[38,1]", "y[39,1]", "y[40,1]", "y[41,1]", "y[42,1]", 
    "y[43,1]", "y[44,1]", "y[45,1]", "y[46,1]", "y[47,1]", "y[48,1]", 
    "y[49,1]", "y[50,1]", "y[51,1]", "y[52,1]", "y[1,2]", "y[2,2]", 
    "y[3,2]", "y[4,2]", "y[5,2]", "y[6,2]", "y[7,2]", "y[8,2]", 
    "y[9,2]", "y[10,2]", "y[11,2]", "y[12,2]", "y[13,2]", "y[14,2]", 
    "y[15,2]", "y[16,2]", "y[17,2]", "y[18,2]", "y[19,2]", "y[20,2]", 
    "y[21,2]", "y[22,2]", "y[23,2]", "y[24,2]", "y[25,2]", "y[26,2]", 
    "y[27,2]", "y[28,2]", "y[29,2]", "y[30,2]", "y[31,2]", "y[32,2]", 
    "y[33,2]", "y[34,2]", "y[35,2]", "y[36,2]", "y[37,2]", "y[38,2]", 
    "y[39,2]", "y[40,2]", "y[41,2]", "y[42,2]", "y[43,2]", "y[44,2]", 
    "y[45,2]", "y[46,2]", "y[47,2]", "y[48,2]", "y[49,2]", "y[50,2]", 
    "y[51,2]", "y[52,2]", "y[1,3]", "y[2,3]", "y[3,3]", "y[4,3]", 
    "y[5,3]", "y[6,3]", "y[7,3]", "y[8,3]", "y[9,3]", "y[10,3]", 
    "y[11,3]", "y[12,3]", "y[13,3]", "y[14,3]", "y[15,3]", "y[16,3]", 
    "y[17,3]", "y[18,3]", "y[19,3]", "y[20,3]", "y[21,3]", "y[22,3]", 
    "y[23,3]", "y[24,3]", "y[25,3]", "y[26,3]", "y[27,3]", "y[28,3]", 
    "y[29,3]"))))

0 个答案:

没有答案