# Perform a drop-1 validation study for a simple model:
themodel <- "model{
for(i in 1:N){
Y[i] ~ dnorm(true.y[i], precision)
true.y[i] <- (m * X[i]) + c
}
m ~ dunif(-1000,1000)
c ~ dunif(-1000,1000)
precision ~ dexp(1)
#data# N, X
}"
# Simulate the data
set.seed(1)
N <- 20
X <- 1:N
Y <- rnorm(length(X), 2*X + 1, 1)
# Some initial values to use for 2 chains:
initfun <- function(chain){
# data is made available within this function when it
# is evaluated for each simulation:
stopifnot(length(data$X) == data$N)
m <- c(-10,10)[chain]
c <- c(10,-10)[chain]
precision <- c(0.01,100)[chain]
.RNG.seed <- chain
.RNG.name <- c("base::Super-Duper",
"base::Wichmann-Hill")[chain]
return(list(m=m, c=c, precision=precision,
.RNG.seed=.RNG.seed, .RNG.name=.RNG.name))
}
# A simple function that removes (over-writes with NA) one datapoint at a time:
datafun <- function(s){
simdata <- Y
simdata[s] <- NA
return(list(Y=simdata))
}
# Set up a cluster to use with the parLapply method:
library(parallel)
cl <- makeCluster(20)
# Call the 20 simulations over the snow cluster:
results <- run.jags.study(simulations=20, model=themodel, datafunction=datafun,
targets=list(Y=Y, m=2, c=1), runjags.options=list(n.chains=2, inits=initfun),
cl=cl)
# Examine the results:
results
Run the code above in your browser using DataLab