0

When plotting runjags output, how does one plot a single specific variable, when many other variables have similar names? Providing a quoted variable name with the varsargument doesn't seem to do it (it still provides all partial matches).

Here is a simple reproducible example.

N <- 200
nobs <- 3
psi <- 0.35
p <- 0.45
z <- rbinom(n=N, size=1,prob=psi)
y <- rbinom(n=N, size=nobs,prob=p*z)

sink("model.txt")
cat("
model {
for (i in 1:N){
    z[i] ~ dbern(psi)
    pz[i] <- z[i]*p
    y[i] ~ dbin(pz[i],nobs) 
    } #i
psi ~ dunif(0,1)
p ~ dunif(0,1)
}                                                   
",fill = TRUE)
sink()

m <-list(y=y,N=N,nobs=nobs)
inits <- function(){list(psi=runif(1),p=runif(1),z=as.numeric(y>0))}  
parameters <- c("p","psi")

ni <- 1000
nt <- 1
nb <- 200
nc <- 3
ad <- 100

library(runjags)

out <- run.jags(model="model.txt",monitor=parameters,data=m,n.chains=nc,inits=inits,burnin=nb,
    sample=ni,adapt=ad,thin=nt,modules=c("glm","dic"),method="parallel")

windows(9,4)
plot(out,plot.type=c("trace","histogram"),vars="p",layout=c(1,2),new.window=FALSE)
4

1 回答 1

1

应该可以双引号变量以获得完全匹配,但这似乎被打破了。也应该可以为 vars 指定一个逻辑向量,但这似乎被 plot 方法破坏了......多么尴尬。以下确实有效:

# Generate a logical vector to use with matching variable names:
variables <- extract(out, 'stochastic')
variables['psi'] <- FALSE

# Add summary statistics only for the specified variables and pre-draw plots:
out2 <- add.summary(out, vars=variables, plots=TRUE)

plot(out2, plot.type=c("trace","histogram"))

我将在下一个版本中修复其他问题。

马特

于 2015-10-07T13:35:09.147 回答