Reputation: 4807
The R2WinBUGS package has a function called write.model()
. The R package rjags has no such function of which I am aware. write.model
creates a temporary text file that can be read as a model by WinBUGS.
I know I can enter write.model
into the console to see the function, but this function seems to make calls to function that I've never seen before, and can't search for in help()
(e.g., replaceScientificNotationR
is apparently a function).
I saw This Post that describes some methods for doing this, but if possible, I'd prefer to not have to use the quotes (just to maintain my syntax highlighting), and a comment suggested that "copying the write.model function" should be doable.
Has anyone done this?
Upvotes: 3
Views: 788
Reputation: 2697
You don't need to copy the write.model function unless you want to. The trick is to use write.model with textConnection. For example:
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)
Upvotes: 2
Reputation: 34937
Presumably, you could just load R2WinBUGS
to get access to the function.
However, in general, where there is a function that you can't see the code for, try getAnywhere
.
E.g., getAnywhere(replaceScientificNotationR)
produces:
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>
Thus, it is an internal function in the R2WinBUGS package. Alternatively, you could download the package source from CRAN and explore.
Upvotes: 2