3

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

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

我看到这篇文章描述了一些这样做的方法,但如果可能的话,我宁愿不必使用引号(只是为了保持我的语法突出显示),并且评论建议“复制 write.model 函数”应该可行。

有人做过吗?

4

2 回答 2

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 包中的一个内部函数。或者,您可以从 CRAN 下载包源并进行探索。

于 2014-02-08T05:53:36.717 回答
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)
于 2015-04-01T22:49:25.597 回答