
### Model 0: Lognormal distribution


# Clear old results

rm(list = ls())


# Set seed

set.seed(73198)


# Directory

setwd("Z:\\My Documents\\Artiklar\\Mariana\\Table 2\\models-lognormal")


# Libraries

library(coda)
library(R2WinBUGS)
library(BRugs)
library(lattice)


## Read the dataset

Example <- read.table("data.csv", header = T)

CreepingThistle <- Example$crepe
Treatment <- Example$Treat
Block <- Example$block

Y <- CreepingThistle
Y[Y==0] <- 0.000001


# Number of observations and blocks
n <- length(Y) 
nb <- 8


# Number of iterations for Gibbs sampling

niter <- 100000
thin <- 100
nchains <- 2


# Initial values

inits <- function(){
  list(phi = 1, beta = rep(0.1, 10), alpha = rep(0.1, 10), 
       tau_eta1 = 10, tau_eta2 = 10, b = matrix(0, nb, 2))
}


# Path to model

model <- file.path("Model-0-lognormal.R")


# Run in WinBUGS

datas <- list("Y","Treatment","Block","n","nb")

params <- c("phi", "sigmab1", "sigmab2", "alpha", "beta", "tp2", "p2","tp","p", "mu")

resu.simLN <- bugs(datas, inits, params, model,
                      n.chains = 2, n.iter = niter, n.thin = thin,
                      n.burnin = round(0.5*niter), program = "openbugs", 
                      debug = TRUE,
                      codaPkg = FALSE, working.directory = NULL,
                      digits = 5, clearWD = FALSE)                        


# Print and save the results

print(resu.simLN, 5)

sink("Model-0-lognormal-summary.txt")
print(resu.simLN, 5)
sink()


# Marginal median

maxi <- niter/thin
maxj <- n

p <- resu.simLN$sims.list$p
mu <- resu.simLN$sims.list$mu
phi <- resu.simLN$sims.list$phi

tp.marg <- matrix(0, nrow = maxi, ncol = maxj)
for (i in 1:maxi){
  for (j in 1:maxj){  
    if (p[i,j] <= 0.5){
      tp.marg[i, j] <- exp(qnorm((0.5 - p[i, j])/(1 - p[i, j]))*phi[i] + mu[i, j])}}}

tp2.marg <- matrix(0, nrow = maxi, ncol = 10)
tp2.marg[, 1]  <- (tp.marg[, 4]  + tp.marg[, 13] + tp.marg[, 30] + tp.marg[, 36])/4 
tp2.marg[, 2]  <- (tp.marg[, 5]  + tp.marg[, 16] + tp.marg[, 29] + tp.marg[, 33])/4 
tp2.marg[, 3]  <- (tp.marg[, 1]  + tp.marg[, 17] + tp.marg[, 28] + tp.marg[, 32])/4 
tp2.marg[, 4]  <- (tp.marg[, 6]  + tp.marg[, 20] + tp.marg[, 23] + tp.marg[, 35])/4 
tp2.marg[, 5]  <- (tp.marg[, 9]  + tp.marg[, 12] + tp.marg[, 25] + tp.marg[, 37])/4 
tp2.marg[, 6]  <- (tp.marg[, 2]  + tp.marg[, 11] + tp.marg[, 27] + tp.marg[, 38])/4 
tp2.marg[, 7]  <- (tp.marg[, 10] + tp.marg[, 18] + tp.marg[, 21] + tp.marg[, 31])/4 
tp2.marg[, 8]  <- (tp.marg[, 7]  + tp.marg[, 14] + tp.marg[, 26] + tp.marg[, 34])/4 
tp2.marg[, 9]  <- (tp.marg[, 8]  + tp.marg[, 15] + tp.marg[, 22] + tp.marg[, 39])/4 
tp2.marg[, 10] <- (tp.marg[, 3]  + tp.marg[, 19] + tp.marg[, 24] + tp.marg[, 40])/4

sink("Model-0-lognormal-summary-marg.txt")
rbind(
  quantile(tp2.marg[, 1],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 2],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 3],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 4],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 5],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 6],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 7],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 8],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 9],  c(0.025, 0.5, 0.975)),
  quantile(tp2.marg[, 10], c(0.025, 0.5, 0.975)))
sink()


# Export results for Figure 2

means.tp2.marg <- apply(tp2.marg, 2, mean)
lower.tp2.marg <- apply(tp2.marg, 2, quantile, probs = c(0.025))
upper.tp2.marg <- apply(tp2.marg, 2, quantile, probs = c(0.975))

means.tp2 <- apply(resu.simLN$sims.list$tp2, 2, mean)
lower.tp2 <- apply(resu.simLN$sims.list$tp2, 2, quantile, probs = c(0.025))
upper.tp2 <- apply(resu.simLN$sims.list$tp2, 2, quantile, probs = c(0.975))

means.p2 <- apply(resu.simLN$sims.list$p2, 2, mean)
lower.p2 <- apply(resu.simLN$sims.list$p2, 2, quantile, probs = c(0.025))
upper.p2 <- apply(resu.simLN$sims.list$p2, 2, quantile, probs = c(0.975))


Figure.2 <- rbind(means.tp2.marg, lower.tp2.marg, upper.tp2.marg,
                  means.tp2, lower.tp2, upper.tp2,
                  means.p2, lower.p2, upper.p2)

write.table(Figure.2, file = "Z:\\My Documents\\Artiklar\\Mariana\\Credability interval figures\\Model-0-lognormal-Figure-2.txt", col.names = F, row.names = F)


