library(MCMCpack) library(knitr) #library(loo) # #表9.1の「知覚時間」データ入力 #対照 x1<-c(31.43,31.09,33.38,30.49,29.62,35.40,32.58,28.96,29.43,28.52, 25.39,32.68,30.51,30.15,32.33,30.43,32.50,32.07,32.35,31.57) #聴音 x2<-c(32.30,34.24,28.10,33.40,37.71,31.62,31.37,35.85,32.33,34.04, 34.96,31.43,35.28,30.19,35.09,33.38,31.49,28.44,32.12,31.81) #音読 x3<-c(31.62,37.04,33.76,30.01,34.18,33.08,28.77,33.90,28.06,37.54, 33.89,32.23,35.95,36.68,33.57,30.87,32.20,29.98,33.08,35.12) #運動 x4<-c(27.79,30.33,29.02,27.02,35.28,26.89,30.33,27.98,27.99,28.04, 25.02,28.80,30.74,26.79,28.65,32.43,29.13,29.50,28.14,32.19) # E1Indfun <- function(beta, x1 , x2 , x3 , x4) { # beta[1] : mu1 , beta[2] : mu2 ,beta[3] : mu3 , beta[4] : mu4 , beta[5] : sigmaE ifelse( beta[5] < 0 , l<- -Inf , l <- sum(log(dnorm(x1, mean=beta[1], sd=beta[5])) ) + sum(log(dnorm(x2, mean=beta[2], sd=beta[5])) ) + sum(log(dnorm(x3, mean=beta[3], sd=beta[5])) ) + sum(log(dnorm(x4, mean=beta[4], sd=beta[5]))) ) return(l) } # chains <- 1:5 seeds <- seq(1,5,1) init<-c(0,0,0,0,30) #init <- c(mean(x1), mean(x2), max(sd(x1),sd(x2))*5 ) burnin<-2000 mcmc_n<-200000 thin<- verbose <-10 #sink("./likelihood.txt") E1Ind <- lapply(chains , function(chain) { MCMCmetrop1R(fun = E1Indfun , theta.init = init , burnin = burnin, mcmc = mcmc_n ,thin=thin, # verbose= verbose, seed = seeds[chain], x1=x1, x2=x2 ,x3=x3 , x4=x4)}) # sink() # system("gawk '/function value/' ./likelihood.txt | gawk 'gsub(\"function value =\",\"\")' > ./E1Ind_lik.txt") # l<-scan("./E1Ind_lik.txt") # ll<-matrix(l,ncol=length(chains),byrow = FALSE) # head(ll) ; tail(ll) # lll<-ll[-c(1 : burnin/thin),] # ( waic1 <- waic(matrix(lll,ncol=1)) ) # E1Ind.mcmc <- mcmc.list(E1Ind) # #plot(E1Ind.mcmc, trace = TRUE, density = TRUE) # summary(E1Ind.mcmc) # gelman.diag(E1Ind.mcmc) # effectiveSize(E1Ind.mcmc) # df<-data.frame(mu1=unlist(E1Ind.mcmc[,1]),mu2=unlist(E1Ind.mcmc[,2]), mu3=unlist(E1Ind.mcmc[,3]),mu4=unlist(E1Ind.mcmc[,4]),sigmaE=unlist(E1Ind.mcmc[,5]) ) # head(df) # ryou<-data.frame(c(round(mean(df[,1]),2) , round(sd(df[,1]),2) , round(quantile(df[,1],c(0.025,0.05,0.5,0.95,0.975)),2))) for (i in 2:ncol(df) ){ ryou<-cbind(ryou,data.frame(c(round(mean(df[,i]),2) , round(sd(df[,i]),2) , round(quantile(df[,i],c(0.025,0.05,0.5,0.95,0.975)),2)))) } rownames(ryou)<-c("EAP","post.sd","2.5%","5%","50%","95%","97.5%") names(ryou)<-c("mu1","mu2","mu3","mu4","sigmaE") kable(t(ryou))
|