我在 MCMC 算法中有一系列迭代。行代表从分布中抽取。这些列代表分布中的参数(变量)。为了便于说明:假设两个变量,五次迭代。所以我有:
> draws <- data.frame( iteration = c(1:5),
alpha = rnorm(5,0,1),
beta = rnorm(5,0,1))
iteration alpha beta
1 1 -0.3157940 0.2122465
2 2 1.0087298 -0.2346733
3 3 1.0366165 0.3472915
4 4 -2.4256564 0.9863279
5 5 -0.6089072 -1.1213000
当我融化数据集时,我得到:
> melt(draws)
Using as id variables
variable value
1 iteration 1.0000000
2 iteration 2.0000000
3 iteration 3.0000000
4 iteration 4.0000000
5 iteration 5.0000000
6 alpha -0.1042616
7 alpha 1.0707001
8 alpha 0.2166865
9 alpha 0.0771617
10 alpha -0.8893614
11 beta -0.4846693
12 beta -1.5950729
13 beta -0.7178340
14 beta 1.0149766
15 beta -0.3128256
但我想保持迭代,以便获得相当于(手工编辑):
> melt(draws)
Using as id variables
iteration variable value
1 1 alpha -0.1042616
2 2 alpha 1.0707001
3 3 alpha 0.2166865
4 4 alpha 0.0771617
5 5 alpha -0.8893614
6 1 beta -0.4846693
7 2 beta -1.5950729
8 3 beta -0.7178340
9 4 beta 1.0149766
10 5 beta -0.3128256