简体   繁体   English

如何将mcmc.list转换为bug对象?

[英]How can I convert an mcmc.list to a bugs object?

I am using the rjags R library. 我正在使用rjags R库。 The function coda.samples produces an mcmc.list , for example (from example(coda.samples) ): 例如,函数coda.samples生成一个mcmc.list (来自example(coda.samples) ):

library(rjags)
data(LINE)
LINE$recompile()
LINE.out <- coda.samples(LINE, c("alpha","beta","sigma"), n.iter=1000)
class(LINE.out)
[1] "mcmc.list"

However, I would like to use the plot.bugs function, which requires a bugs object as input. 但是,我想使用plot.bugs函数,它需要一个bugs对象作为输入。

Is it possible to convert an object from an mcmc.list to a bugs object, so that plot.bugs(LINE.out) ? 是否可以将对象从mcmc.list转换为bugs对象,以便plot.bugs(LINE.out)

Note that there is a similar question on stats.SE that has been unanswered for over a month. 请注意, stats.SE上有一个类似的问题,一个多月没有得到答复。 That question had a bounty that ended on 08/29/2012. 这个问题有一笔在2012年8月29日结束的赏金。

More hints: 更多提示:

I have discovered that the R2WinBUGS package has a function "as.bugs.array" function - but it is not clear how the function can be applied to an mcmc.list. 我发现R2WinBUGS包有一个函数“as.bugs.array”函数 - 但是不清楚该函数如何应用于mcmc.list。

I do not know whether this will give you what you want. 我不知道这是否会给你你想要的东西。 Note that the model code came from using your code and then typing LINE at the cursor. 请注意, model代码来自使用您的代码,然后在光标处键入LINE The rest is just standard bugs code, except I used tau = rgamma(1,1) for an initial value and do not know how standard that is. 其余的只是标准的错误代码,除了我使用tau = rgamma(1,1)作为初始值而不知道标准是多少。 More than once I have seen tau = 1 used as an initial value. 我不止一次看到tau = 1用作初始值。 Perhaps that would be better. 也许那会更好。

In effect, I created an rjags object using the same model code you were using and added a jags statement to run it. 实际上,我使用您使用的相同model代码创建了一个rjags对象,并添加了一个jags语句来运行它。 I admit that is not the same thing as converting coda output to a bugs object, but it might result in you getting the desired plot . 我承认这与将coda输出转换为bugs对象不同,但它可能会导致你获得所需的plot

If all you have is an mcmc.list and no model code and you simply want to plot the mcmc.list , then my answer will not help. 如果您拥有的是mcmc.list并且没有model代码,并且您只想绘制mcmc.list ,那么我的答案将无济于事。

library(R2jags)

x <- c(1, 2, 2, 4, 4,  5,  5,  6,  6,  8) 
Y <- c(7, 8, 7, 8, 9, 11, 10, 13, 14, 13) 

N <- length(x)
xbar <- mean(x)

summary(lm(Y ~ x))

x2 <- x - xbar

summary(lm(Y ~ x2))

# Specify model in BUGS language

sink("model1.txt")

cat("

model  {
                for( i in 1 : N ) {
                        Y[i] ~ dnorm(mu[i],tau)
                        mu[i] <- alpha + beta * (x[i] - xbar)
                }
                tau ~ dgamma(0.001,0.001) 
                sigma <- 1 / sqrt(tau)
                alpha ~ dnorm(0.0,1.0E-6)
                beta ~ dnorm(0.0,1.0E-6)        
        }

",fill=TRUE)
sink()

win.data <- list(Y=Y, x=x, N=N, xbar=xbar)

# Initial values
inits <- function(){ list(alpha=rnorm(1), beta=rnorm(1), tau = rgamma(1,1))}

# Parameters monitored
params <- c("alpha", "beta", "sigma")

# MCMC settings
ni <- 25000
nt <-     5
nb <-  5000
nc <-     3

out1 <- jags(win.data, inits, params, "model1.txt", n.chains = nc, 
             n.thin = nt, n.iter = ni, n.burnin = nb)

print(out1, dig = 2)
plot(out1)

#library(R2WinBUGS)
#plot(out1)

EDIT: 编辑:

Based on the comments perhaps something like this will help. 根据评论,也许这样的事情会有所帮助。 The line str(new.data) suggests that a large amount of data are available. str(new.data)表明有大量数据可用。 If you are simply trying to create variations of default plots then doing so may only be a matter of extracting and subsetting the data as desired. 如果您只是尝试创建默认图的变体,那么这样做可能只是根据需要提取和子集化数据。 Here plot(new.data$sims.list$P1) is just one straight-forward example. 这里的plot(new.data$sims.list$P1)只是一个直截了当的例子。 Without knowing exactly what plot you want I will not attempt more specific data extractions. 如果不知道您想要什么样的情节,我将不会尝试更具体的数据提取。 If you post a figure showing an example of the exact kind of plot you want perhaps someone can take it from here and post the code needed to create it. 如果您发布一个图形,显示您想要的确切类型的图表的示例,也许有人可以从此处获取并发布创建它所需的代码。

By the way, I recommend reducing the size of the example data set to perhaps three chains and perhaps no more than 30 iterations until you have the exact code you want for the exact plot you want: 顺便说一句,我建议将示例数据集的大小减小到三个链,也许不超过30次迭代,直到您拥有所需的确切代码,以获得所需的精确绘图:

load("C:/Users/mmiller21/simple R programs/test.mcmc.list.Rdata")

class(test.mcmc.list)

library(R2WinBUGS)

plot(as.bugs.array(sims.array = as.array(test.mcmc.list)))

new.data <- as.bugs.array(sims.array = as.array(test.mcmc.list))

str(new.data)

plot(new.data$sims.list$P1)

EDIT: 编辑:

Note also that: 还要注意:

class(new.data)
[1] "bugs"

whereas: 然而:

class(test.mcmc.list)
[1] "mcmc.list"

which is what the title of your post requests. 这是你的帖子的标题要求。

Not an answer, but this blog post has the following wrapper function for converting coda output (.txt) to BUGS using R2WinBUGS:::bugs.sims: 不是答案,但是这篇博文有以下包装函数,用于使用R2WinBUGS ::: bugs.sims将coda输出(.txt)转换为BUGS:

coda2bugs <- function(path, para, n.chains=3, n.iter=5000, 
                      n.burnin=1000, n.thin=2) {   
 setwd(path)   
 library(R2WinBUGS)   
 fit <- R2WinBUGS:::bugs.sims(para, n.chains=n.chains, 
        n.iter=n.iter, n.burnin=n.burnin, n.thin=n.thin, 
        DIC = FALSE)   
 class(fit) <- "bugs"   
 return(fit) 
} 

This is not a solution to your question, but in response to your comment on @andybega's answer, here's a way to convert an mcmc.list object to typical coda text files. 这不是你的问题的解决方案,但是回应你对@ andybega的答案的评论,这里是一种将mcmc.list对象转换为典型的coda文本文件的方法。

mcmc.list.to.coda <- function(x, outdir=tempdir()) {
  # x is an mcmc.list object
  x <- as.array(x)
  lapply(seq_len(dim(x)[3]), function(i) {
    write.table(cbind(rep(seq_len(nrow(x[,,i])), ncol(x)), c(x[,,i])), 
                paste0(outdir, '/coda', i, '.txt'),
                row.names=FALSE, col.names=FALSE)
  })

  cat(paste(colnames(x), 
            1 + (seq_len(ncol(x)) - 1) * nrow(x),
            nrow(x) * seq_len(ncol(x))), 
      sep='\n', 
      file=file.path(outdir, 'codaIndex.txt'))
}

# For example, using the LINE.out from your question:
mcmc.list.to.coda(LINE.out, tempdir())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM