daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Generation of simulated data
  2. set.seed(123)
  3. varY <- rnorm(100, 0, 1)
  4. facX <- gl(n = 4, k = 25, labels = c("A", "B", "C", "D"))
  5. block <- factor(rep(x = paste("B",1:25, sep = ""), times = 4))
  6. df <- data.frame(varY, facX, block)
  7.  
  8. # Frequentist analysis
  9. library(lme4)
  10. model.freq <- lmer(varY ~ facX + (1|block), data = df)
  11.    
  12. # Data reshaping
  13. matY <- matrix(data = varY, nrow = 25, ncol = 4, byrow = FALSE)
  14.  
  15. # Model specification
  16. model.bayes <- function(){
  17. # Likelihood
  18.  for(j in 1:Nlev){
  19.   for(i in 1:N){
  20.    varY[i,j] ~ dnorm(mu + theta[j], 1/(sig*sig))
  21.    }
  22.  }
  23. theta[1] <- 0
  24.   for(j in 2:Nlev){
  25.    theta[j] ~ dnorm(0, 0.001)
  26.   }
  27. # Priors
  28. mu ~ dnorm(0, 0.001)
  29. sig ~ dunif(0, 1000)
  30. }
  31.  
  32. # Bayesian analysis with JAGS
  33. dat <- list(varY = matY, Nlev = ncol(matY), N = nrow(matY))
  34. params <- c("mu", "theta", "sig")
  35. inits <- function(){list(mu = rnorm(1), sig = rlnorm(1))}
  36.  
  37. library(R2jags)
  38. out <- jags(data = dat, inits = inits, parameters.to.save = params, model.file = model.bayes, n.chains = 3, n.iter = 10000, n.burnin = 1000, n.thin = 1)
RAW Paste Data
Top