rJAGS版本的write.model()?

时间:2013-07-23 16:08:12

标签: r winbugs jags r2winbugs

R2WinBUGS包有一个名为write.model()的函数。 R包rjags没有我所知道的这种功能。 write.model创建一个临时文本文件,可以通过WinBUGS作为模型读取。

我知道我可以在控制台中输入write.model来查看该函数,但是这个函数似乎调用了我以前从未见过的函数,并且无法在help()中搜索(例如,replaceScientificNotationR显然是一种功能)。

我看到This Post描述了执行此操作的一些方法,但如果可能的话,我宁愿不必使用引号(只是为了维护我的语法突出显示),并且评论建议“复制” write.model函数“应该可行。

有没有人这样做过?

2 个答案:

答案 0 :(得分:2)

据推测,您只需加载R2WinBUGS即可访问该功能。

但是,通常情况下,如果某个功能无法查看代码,请尝试getAnywhere

例如,getAnywhere(replaceScientificNotationR)产生:

A single object matching ‘replaceScientificNotationR’ was found
It was found in the following places
  namespace:R2WinBUGS
with value

function (bmodel, digits = 5) 
{
    env <- new.env()
    assign("rSNRidCounter", 0, envir = env)
    replaceID <- function(bmodel, env, digits = 5) {
        for (i in seq_along(bmodel)) {
            if (length(bmodel[[i]]) == 1) {
                if (as.character(bmodel[[i]]) %in% c(":", "[", 
                  "[[")) 
                  return(bmodel)
                if ((typeof(bmodel[[i]]) %in% c("double", "integer")) && 
                  ((abs(bmodel[[i]]) < 0.001) || (abs(bmodel[[i]]) > 
                    10000))) {
                  counter <- get("rSNRidCounter", envir = env) + 
                    1
                  assign("rSNRidCounter", counter, envir = env)
                  id <- paste("rSNRid", counter, sep = "")
                  assign(id, formatC(bmodel[[i]], digits = digits, 
                    format = "E"), envir = env)
                  bmodel[[i]] <- id
                }
            }
            else {
                bmodel[[i]] <- replaceID(bmodel[[i]], env, digits = digits)
            }
        }
        bmodel
    }
    bmodel <- deparse(replaceID(bmodel, env, digits = digits), 
        control = NULL)
    for (i in ls(env)) {
        bmodel <- gsub(paste("\"", i, "\"", sep = ""), get(i, 
            envir = env), bmodel, fixed = TRUE)
    }
    bmodel
}
<environment: namespace:R2WinBUGS>

因此,它是R2WinBUGS包中的内部函数。 或者,您可以下载package source from CRAN并浏览。

答案 1 :(得分:2)

除非您愿意,否则无需复制write.model函数。诀窍是将write.model与textConnection一起使用。例如:

require(nlme)
require(rjags)
require(R2WinBUGS)

jdat <- list(nobs=nrow(Rail), travel=Rail$travel, Rail=Rail$Rail)
jinit <- list(mu=50, tau=1, tau.theta=1)

jfun6 <- function() {
  for(i in 1:nobs){
    travel[i] ~ dnorm(mu + theta[Rail[i]], tau)
  }
  for(j in 1:6) {
    theta[j] ~ dnorm(0, tau.theta)
  }
  # mu ~ dnorm() works fine since the mean is far from 0
  # mu ~ dgamma(1, 0.0001) could be used to ensure positive numbers.
  # mu ~ dnorm(50, 0.0001) I(0,) also ensures positive numbers.
  # We use truncation to show a difference between rjags and R2WinBUGS.
  # R2WinBUGS needs a dummy operator %_% that will be removed.
  mu ~ dnorm(50, 0.0001) %_% I(0,)
  tau ~ dgamma(1, .001)
  tau.theta ~ dgamma(1, .001)
  sigma <- 1/sqrt(tau)
  sigma.theta <- 1/sqrt(tau.theta)
}

# Save the jfun6 function into a text object called jmod6
tc1 <- textConnection("jmod6", "w")
write.model(jfun6, tc1)
close(tc1)

# Read the text object
tc2 <- textConnection(jmod6)
j6 <- jags.model(tc2, data=jdat, inits=jinit)
close(tc2)
c6 <- coda.samples(j6, c("mu","theta", "sigma", "sigma.theta"), n.iter=100000, thin=5)
summary(c6)