library(rjags) library(MCMCpack) library(ggplot2) # Model Files getwd() ##change the working directory to the working directory of this supplementary material setwd("/suplMat") oneHTSM <- file.path("1HTSM.R") ##The results in the paper are based on the 1 HTSM, to compare the results with the 2HTSM, this model is provided as well. ##twoHTSM <- file.path("2HTSM.R") #load the data exp1DataNoImag <- read.csv("Exp1NoImagery.csv", sep="") exp1DataImag <- read.csv("Exp1Imagery.csv", sep="") exp2DataNoImag <- read.csv("Exp2NoImagery.csv", sep="") exp2DataImag <- read.csv("Exp2Imagery.csv", sep="") # number of parameters dependent on model P = 6 ## create list for JAGS data_listNOImag <- with(exp1DataNoImag, { list (P = P, Pm = rep(0, P), W = diag(P), n_items = length(x1), x1 = x1, #d' x2 = x2, # Ease of mental Imagery C = C, H = H, N = N, tree_complete = cbind(tree_complete.CC,tree_complete.CH,tree_complete.CN), tree_halved = cbind(tree_halved.HH,tree_halved.HC,tree_halved.HN), tree_new = cbind(tree_new.NN, tree_new.NC,tree_new.NH)) } ) data_listNOImag2 <- with(exp2DataNoImag, { list (P = P, Pm = rep(0, P), W = diag(P), n_items = length(x1), x1 = x1, #d' x2 = x2, # Ease of mental Imagery C = C, H = H, N = N, tree_complete = cbind(tree_complete.CC,tree_complete.CH,tree_complete.CN), tree_halved = cbind(tree_halved.HH,tree_halved.HC,tree_halved.HN), tree_new = cbind(tree_new.NN, tree_new.NC,tree_new.NH)) } ) data_listImag <- with(exp1DataImag, { list (P = P, Pm = rep(0, P), W = diag(P), n_items = length(x1), x1 = x1, #d' x2 = x2, # Ease of mental Imagery C = C, H = H, N = N, tree_complete = cbind(tree_complete.CC,tree_complete.CH,tree_complete.CN), tree_halved = cbind(tree_halved.HH,tree_halved.HC,tree_halved.HN), tree_new = cbind(tree_new.NN, tree_new.NC,tree_new.NH)) } ) data_listImag2 <- with(exp2DataImag, { list (P = P, Pm = rep(0, P), W = diag(P), n_items = length(x1), x1 = x1, #d' x2 = x2, # Ease of mental Imagery C = C, H = H, N = N, tree_complete = cbind(tree_complete.CC,tree_complete.CH,tree_complete.CN), tree_halved = cbind(tree_halved.HH,tree_halved.HC,tree_halved.HN), tree_new = cbind(tree_new.NN, tree_new.NC,tree_new.NH)) } ) ## define parameters to be monitored params <- c("b0", "b1", "b2", "theta", "postpred_complete", "postpred_halved", "postpred_new", "fit", "fit.new","bpvalue") # define intial values inits_func <- function(n_chains) { inits_list=list() for(cc in 1:n_chains) { inits_list[[cc]]= list( b0 = runif(P,0,1), b1 = runif(P,0,1), b2 = runif(P,0,1), tau_prec_mu = rwish(P+1, diag(P)) ) } return(inits_list) } ## parameters for the sampler n_chains <- 3 n_burnin <- 1000 n_samples <- 80000 n_thin <- 5 #run the 1HTSM model (to run the 2HTSM change comments) exp1ImagModel1 <- jags.model(oneHTSM, data_listImag, inits_func(n_chains), n.chains=n_chains, n.adapt=1000) #exp1ImagModel1 <- jags.model(twoHTSM, data_listImag, inits_func(n_chains), n.chains=n_chains, n.adapt=1000) exp1ImagModel1samples <- coda.samples(exp1ImagModel1, n.iter=5000, variable.names=params) exp2ImagModel1 <- jags.model(oneHTSM, data_listImag2, inits_func(n_chains), n.chains=n_chains, n.adapt=1000) #exp2ImagModel1 <- jags.model(twoHTSM, data_listImag, inits_func(n_chains), n.chains=n_chains, n.adapt=1000) exp2ImagModel1samples <- coda.samples(exp2ImagModel1, n.iter=5000, variable.names=params) exp1NOImagModel1 <- jags.model(oneHTSM, data_listNOImag, inits_func(n_chains), n.chains=n_chains, n.adapt=1000) #exp1NOImagModel1 <- jags.model(twoHTSM, data_listImag, inits_func(n_chains), n.chains=n_chains, n.adapt=1000) exp1NOImagModel1samples <- coda.samples(exp1NOImagModel1, n.iter=5000, variable.names=params) exp2NOImagModel1 <- jags.model(oneHTSM, data_listNOImag2, inits_func(n_chains), n.chains=n_chains, n.adapt=1000) #exp2NOImagModel1 <- jags.model(twoHTSM, data_listImag, inits_func(n_chains), n.chains=n_chains, n.adapt=1000) exp2NOImagModel1samples <- coda.samples(exp2NOImagModel1, n.iter=5000, variable.names=params) ##compute DIC DIC1expImag <- dic.samples(exp1ImagModel1, 1000,'pD') DIC2expImag <- dic.samples(exp2ImagModel1, 1000,'pD') DIC1expNOImag <- dic.samples(exp1NOImagModel1, 1000,'pD') DIC2expNOImag <- dic.samples(exp2NOImagModel1, 1000,'pD') exp1Imag <- as.matrix(exp1ImagModel1samples) exp2Imag <- as.matrix(exp2ImagModel1samples) exp1NOImag <- as.matrix(exp1NOImagModel1samples) exp2NOImag <- as.matrix(exp2NOImagModel1samples) ### Summary of grand means in csv file PostParamBeta0mean <- round(cbind(apply(exp1Imag[,1:6],2,mean), apply(exp1NOImag[,1:6],2,mean), apply(exp2Imag[,1:6],2,mean), apply(exp2NOImag[,1:6],2,mean)),2) PostParamBeta0low <- round(cbind(apply(exp1Imag[,1:6],2,quantile, probs= c(.025)), apply(exp1NOImag[,1:6],2,quantile, probs= c(.025)), apply(exp2Imag[,1:6],2,quantile, probs= c(.025)), apply(exp2NOImag[,1:6],2,quantile, probs= c(.025))),2) PostParamBeta0high <- round(cbind(apply(exp1Imag[,1:6],2,quantile, probs= c(.975)), apply(exp1NOImag[,1:6],2,quantile, probs= c(.975)), apply(exp2Imag[,1:6],2,quantile, probs= c(.975)), apply(exp2NOImag[,1:6],2,quantile, probs= c(.975))),2) PostParamBeta0 <- round(pnorm(cbind(PostParamBeta0mean[,1], PostParamBeta0low[,1],PostParamBeta0high[,1], PostParamBeta0mean[,2], PostParamBeta0low[,2],PostParamBeta0high[,2], PostParamBeta0mean[,3], PostParamBeta0low[,3],PostParamBeta0high[,3], PostParamBeta0mean[,4], PostParamBeta0low[,4],PostParamBeta0high[,4])),2) write.table(PostParamBeta0, "grandMean.csv", sep = ",") ## calculation of Bayesfactors with Savage-Dickey Method (Wagemakers et ak. 2010) ##for beta1 and beta1 in the different conditions library(polspline) # this package can be installed from within R prior <- dnorm(0, 0, 1) BFBeta1exp1Imag <- 0 BFBeta1exp1NOImag <- 0 BFBeta1exp2Imag <- 0 BFBeta1exp2NOImag <- 0 BFBeta2exp1Imag <- 0 BFBeta2exp1NOImag <- 0 BFBeta2exp2Imag <- 0 BFBeta2exp2NOImag <- 0 for (i in 7:12){ fitBeta11 <- logspline(exp1Imag[,i]) posteriorBeta11 <- dlogspline(0, fitBeta11) # this gives the pdf at BFBeta1exp1Imag[i-6] <- prior/posteriorBeta11 } for (i in 7:12){ fitBeta11 <- logspline(exp1NOImag[,i]) posteriorBeta11 <- dlogspline(0, fitBeta11) # this gives the pdf at BFBeta1exp1NOImag[i-6] <- prior/posteriorBeta11 } for (i in 7:12){ fitBeta11 <- logspline(exp2Imag[,i]) posteriorBeta11 <- dlogspline(0, fitBeta11) # this gives the pdf at BFBeta1exp2Imag[i-6] <- prior/posteriorBeta11 } for (i in 7:12){ fitBeta11 <- logspline(exp2NOImag[,i]) posteriorBeta11 <- dlogspline(0, fitBeta11) # this gives the pdf at BFBeta1exp2NOImag[i-6] <- prior/posteriorBeta11 } for (i in 13:18){ fitBeta21 <- logspline(exp1Imag[,i]) posteriorBeta21 <- dlogspline(0, fitBeta21) # this gives the pdf at BFBeta2exp1Imag[i-12] <- prior/posteriorBeta21 } for (i in 13:18){ fitBeta21 <- logspline(exp1NOImag[,i]) posteriorBeta21 <- dlogspline(0, fitBeta21) # this gives the pdf at BFBeta2exp1NOImag[i-12] <- prior/posteriorBeta21 } for (i in 13:18){ fitBeta21 <- logspline(exp2Imag[,i]) posteriorBeta21 <- dlogspline(0, fitBeta21) # this gives the pdf at BFBeta2exp2Imag[i-12] <- prior/posteriorBeta21 } for (i in 13:18){ fitBeta21 <- logspline(exp2NOImag[,i]) posteriorBeta21 <- dlogspline(0, fitBeta21) # this gives the pdf at BFBeta2exp2NOImag[i-12] <- prior/posteriorBeta21 } ## summary of beta1 in csv file PostParamBeta1mean <- round(cbind(apply(exp1Imag[,7:12],2,mean), apply(exp1NOImag[,7:12],2,mean), apply(exp2Imag[,7:12],2,mean), apply(exp2NOImag[,7:12],2,mean)),2) PostParamBeta1low <- round(cbind(apply(exp1Imag[,7:12],2,quantile, probs= c(.025)), apply(exp1NOImag[,7:12],2,quantile, probs= c(.025)), apply(exp2Imag[,7:12],2,quantile, probs= c(.025)), apply(exp2NOImag[,7:12],2,quantile, probs= c(.025))),2) PostParamBeta1high <- round(cbind(apply(exp1Imag[,7:12],2,quantile, probs= c(.975)), apply(exp1NOImag[,7:12],2,quantile, probs= c(.975)), apply(exp2Imag[,7:12],2,quantile, probs= c(.975)), apply(exp2NOImag[,7:12],2,quantile, probs= c(.975))),2) PostParamBeta1logBF <- round(cbind(log(BFBeta1exp1Imag),log(BFBeta1exp1NOImag),log(BFBeta1exp2Imag),log(BFBeta1exp2NOImag)),2) PostParamBeta1 <- cbind(PostParamBeta1mean[,1], PostParamBeta1low[,1],PostParamBeta1high[,1], PostParamBeta1logBF[,1], PostParamBeta1mean[,2], PostParamBeta1low[,2],PostParamBeta1high[,2], PostParamBeta1logBF[,2], PostParamBeta1mean[,3], PostParamBeta1low[,3],PostParamBeta1high[,3], PostParamBeta1logBF[,3], PostParamBeta1mean[,4], PostParamBeta1low[,4],PostParamBeta1high[,4], PostParamBeta1logBF[,4]) write.table(PostParamBeta1, "beta1.csv", sep = ",") ## summary of beta2 in csv file PostParamBeta2mean <- round(cbind(apply(exp1Imag[,13:18],2,mean), apply(exp1NOImag[,13:18],2,mean), apply(exp2Imag[,13:18],2,mean), apply(exp2NOImag[,13:18],2,mean)),2) PostParamBeta2low <- round(cbind(apply(exp1Imag[,13:18],2,quantile, probs= c(.025)), apply(exp1NOImag[,13:18],2,quantile, probs= c(.025)), apply(exp2Imag[,13:18],2,quantile, probs= c(.025)), apply(exp2NOImag[,13:18],2,quantile, probs= c(.025))),2) PostParamBeta2high <- round(cbind(apply(exp1Imag[,13:18],2,quantile, probs= c(.975)), apply(exp1NOImag[,13:18],2,quantile, probs= c(.975)), apply(exp2Imag[,13:18],2,quantile, probs= c(.975)), apply(exp2NOImag[,13:18],2,quantile, probs= c(.975))),2) PostParamBeta2logBF <- round(cbind(log(BFBeta2exp1Imag),log(BFBeta2exp1NOImag),log(BFBeta2exp2Imag),log(BFBeta2exp2NOImag)),2) PostParamBeta2 <- cbind(PostParamBeta2mean[,1], PostParamBeta2low[,1],PostParamBeta2high[,1], PostParamBeta2logBF[,1], PostParamBeta2mean[,2], PostParamBeta2low[,2],PostParamBeta2high[,2], PostParamBeta2logBF[,2], PostParamBeta2mean[,3], PostParamBeta2low[,3],PostParamBeta2high[,3], PostParamBeta2logBF[,3], PostParamBeta2mean[,4], PostParamBeta2low[,4],PostParamBeta2high[,4], PostParamBeta2logBF[,4]) write.table(PostParamBeta1, "beta1.csv", sep = ",") PostParamBeta1 <- round(rbind(apply(exp1Imag[,7:12],2,mean), apply(exp1NOImag[,7:12],2,mean), apply(exp2Imag[,7:12],2,mean), apply(exp2NOImag[,7:12],2,mean)),3) ### summary of Fit statistics FIT <- rbind (cbind(DIC1expImag, mean(exp1Imag[,"bpvalue"]), mean(exp1Imag[,"fit"]), mean(exp1Imag[,"fit.new"])), cbind(DIC1expNOImag, mean(exp1NOImag[,"bpvalue"]), mean(exp1NOImag[,"fit"]), mean(exp1NOImag[,"fit.new"])), cbind(DIC2expImag, mean(exp2Imag[,"bpvalue"]), mean(exp2Imag[,"fit"]), mean(exp2Imag[,"fit.new"])), cbind(DIC2expNOImag, mean(exp2NOImag[,"bpvalue"]), mean(exp2NOImag[,"fit"]), mean(exp2NOImag[,"fit.new"])))