I am splitting my dataset by simulation ID and applying a runjags functions to each subsest simultaneously.
Right now, each simulation contains 1000 observations. I know that sometimes the number of observations will differ since I will be dropping rows that meet certain criteria. I don't know how many observations will be dropped but I can calculate that by using groupobs <- fulldata %>% count(SimulID, sort=TRUE).
Is there a way that I can change N=1000 during each simulation run. It would mean having to rewrite the tempModel.txt file with every simulation that is run.
Thank you.
#Subset data by SimulID
subsetdata <- split(fulldata, as.factor(fulldata$SimulID))
#Count obs within each group
groupobs <- fulldata %>% count(SimulID, sort=TRUE)
modelString <- "
model{
#Model specification
for (i in 1:1000) {
y[i]~dnorm(muy[i], Inv_sig2_e)
muy[i]<-b0+b1*x1[i]+b2*x2[i]
}
#priors
b0~dnorm(0, 1.0E-6)
b1~dnorm(0, 1.0E-6)
b2~dnorm(0, 1.0E-6)
Inv_sig2_e~dgamma(1.0E-3, 1.0E-3)
#parameter transformation
Sig2_e<-1/Inv_sig2_e
}
"
writeLines(modelString, "tempModel.txt")
output_models <- lapply(subsetdata, function(x){
model_data = x
initsList1 <- list(b0=1, b1=1, b2=1, Inv_sig2_e=1)
initsList2 <- list(b0=1, b1=2, b2=3, Inv_sig2_e=1)
initsList3 <- list(b0=2, b1=3, b2=4, Inv_sig2_e=1)
runJagsOut <- run.jags(method = "parallel",
model = "tempModel.txt",
# NOTE: theta and omega are vectors:
monitor = c( "b0","b1","b2","Sig2_e"),
data = model_data,
inits = list(initsList1, initsList2, initsList3), # NOTE: Let JAGS initialize.
n.chains = 3, # NOTE: Not only 1 chain.
adapt = 500,
burnin = 2500,
sample = 2500,
thin = 1,
summarise = FALSE,
plots = FALSE)
})
You have several options
You could construct the model string on the fly. [The
model
argument torun.jags
can contain a character string instead of a file name, so there's no need to write to a file and then read it in again.]You can add an element to your
data
list (x
in your code) that contains the number of observations,and refer to that in your
model_string
:You could calculate the number of observations on the fly:
in your
model_string
.Edit In response to OP's comment, here are implementations of each of my three suggestins above. The OP's code is not reproducible as they haven't provided their data, so I will reanalyse an example used by O'Quigley et al in their 1990 CRM paper. To reproduce OP's grouped analysis, I'll duplicate the data and simply analyse it twice.
Input data:
I find the tidyverse's
group_map
function provides code that is both more compact and easier to understand thanlapply
, so I'll use that.Option 1: paste the observation count into the model string.
Option 2: pass the observation count as an element of
data
Option 3: Let JAGS calculate the observation count
My personal preference is for option 2.
I've used
.x
and.y
as argument names to the threefitX
functions to match the convention used in the online documentation forgroup_map
.