#### test case where y is a matrix
asdf_jags <- tempfile()
cat('model {
for(i in 1:n) {
for(j in 1:ngrp) {
y[i,j] ~ dnorm(mu[i,j], tau)
mu[i,j] <- b0 + b1*x[i,j] + a[j]
}
}
for(j in 1:ngrp) {
a[j] ~ dnorm(0, tau_a)
}
tau <- pow(sig, -2)
sig ~ dunif(0, 10)
b0 ~ dnorm(0, 0.001)
b1 ~ dnorm(0, 0.001)
tau_a <- pow(sig_a, -2)
sig_a ~ dunif(0, 10)
}', file=asdf_jags)
# simulate data to go with the example model
n <- 45
x <- matrix(rnorm(n, sd=3),
nrow=20, ncol=3)
y <- matrix(rnorm(n, mean=rep(1:3, each=20)-x),
nrow=20, ncol=3)
asdf_data <- list(x=x,
y=y,
n=nrow(x),
ngrp=ncol(x))
# JAGS controls
niter <- 1000
ncores <- 2
# ncores <- min(10, parallel::detectCores()-1)
## random assignment of folds
kfold1 <- kfold(p="y",
k=5,
model.file=asdf_jags, data=asdf_data,
n.chains=ncores, n.iter=niter,
n.burnin=niter/2, n.thin=niter/1000,
parallel=FALSE)
str(kfold1)
kfold1$fold
## Performing LOOCV, but assigning folds by row of input data
kfold2 <- kfold(p="y",
loocv=TRUE, fold_dims=1,
model.file=asdf_jags, data=asdf_data,
n.chains=ncores, n.iter=niter,
n.burnin=niter/2, n.thin=niter/1000,
parallel=FALSE)
str(kfold2)
kfold2$fold
Run the code above in your browser using DataLab