Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. ## First create the data
  2. set.seed(42)
  3.  
  4. samplesize <- 200
  5. nsites <- 10 # Number of sites
  6. b_length <- sort(rnorm(samplesize)) # Body length (explanatory variable)
  7. sites <- sample(1:10, samplesize, replace = T) # Site (grouping variable)
  8. table(sites)
  9.  
  10. int_true_mean <- 45 # True mean intercept
  11. int_true_sigma <- 10 # True standard deviation of random intercepts
  12. int_true_sites <- rnorm(n = nsites, mean = int_true_mean, sd = int_true_sigma) # True intercept of each site
  13.  
  14. # Intercept of each snake individual (depending on the site where it was captured):
  15. sitemat <- matrix(0, nrow = samplesize, ncol = nsites)
  16. for (i in 1:nrow(sitemat)) sitemat[i, sites[i]] <- 1
  17. int_true <- sitemat %*% int_true_sites
  18.  
  19. slope_true <- 10 # True slope (coefficient of length)
  20. mu <- int_true + slope_true * b_length # True means of normal distributions
  21. sigma <- 5 # True standard deviation of normal distribution (random variation)
  22.  
  23. b_mass <- rnorm(samplesize, mean = mu, sd = sigma) # Body mass (response variable)
  24.  
  25. snakes3 <- data.frame(b_length = b_length, b_mass = b_mass, site = sites)
  26. head(snakes3)
  27.  
  28. ## Create list for jags to work on, include number of sites as an additional element in the list:
  29. Nsites <- length(levels(as.factor(snakes3$site)))
  30. jagsdata_s3 <- with(snakes3, list(b_mass = b_mass, b_length = b_length, site = site, N = length(b_mass), Nsites = Nsites))
  31.  
  32. ## Define model
  33.  
  34. lm3_jags <- function(){
  35. # Likelihood:
  36. for (i in 1:N){
  37. b_mass[i] ~ dnorm(mu[i], tau) # tau is precision (1 / variance)
  38. mu[i] <- alpha + a[site[i]] + beta * b_length[i] # Random intercept for site
  39. }
  40. # Priors:
  41. alpha ~ dnorm(0, 0.01) # intercept
  42. sigma_a ~ dunif(0, 100) # standard deviation of random effect (variance between sites)
  43. tau_a <- 1 / (sigma_a * sigma_a) # convert to precision
  44. for (j in 1:Nsites){
  45. a[j] ~ dnorm(0, tau_a) # random intercept for each site
  46. }
  47. beta ~ dnorm(0, 0.01) # slope
  48. sigma ~ dunif(0, 100) # standard deviation of fixed effect (variance within sites)
  49. tau <- 1 / (sigma * sigma) # convert to precision
  50. }
  51.  
  52. ## Define initial values
  53. init_values <- function(){
  54. list(alpha = rnorm(1), sigma_a = runif(1), beta = rnorm(1), sigma = runif(1))
  55. }
  56.  
  57. # Define parameters to kept in results
  58. params <- c("alpha", "beta", "sigma", "sigma_a")
  59.  
  60. # Fit the model
  61. fit_lm3 <- jags(data = jagsdata_s3, inits = init_values, parameters.to.save = params, model.file = lm3_jags, n.chains = 3, n.iter = 20000, n.burnin = 5000, n.thin = 2, DIC = F)
  62. # I have reduced n.thin to 2, so output slightly different from website
  63.  
  64. ## Output
  65. fit_lm3
  66.  
  67.  
  68. mu.vect sd.vect 2.5% 25% 50% 75% 97.5% Rhat n.eff
  69. alpha 42.583 4.481 30.875 41.144 43.476 45.208 48.110 1.004 3600
  70. beta 9.893 0.349 9.208 9.662 9.895 10.127 10.582 1.001 19000
  71. sigma 4.755 0.248 4.300 4.581 4.745 4.915 5.272 1.001 22000
  72. sigma_a 8.803 4.386 4.610 6.292 7.689 9.845 19.898 1.002 2400
  73.  
  74. samplesize <- 1000
  75.  
  76. mu.vect sd.vect 2.5% 25% 50% 75% 97.5% Rhat n.eff
  77. alpha 33.587 8.982 11.181 28.906 35.602 40.078 45.933 1.001 6100
  78. beta 9.839 0.164 9.519 9.727 9.838 9.950 10.165 1.001 22000
  79. sigma 5.265 0.118 5.038 5.184 5.263 5.343 5.503 1.001 22000
  80. sigma_a 19.635 9.749 8.960 12.965 16.852 23.377 45.792 1.001 5300
  81.  
  82. samplesize <- 1000
  83. nsites <- 50 # Number of sites
  84. b_length <- sort(rnorm(samplesize)) # Body length (explanatory variable)
  85. sites <- sample(1:50, samplesize, replace = T) # Site (grouping variable)
  86.  
  87. mu.vect sd.vect 2.5% 25% 50% 75% 97.5% Rhat n.eff
  88. alpha 43.398 1.533 40.296 42.395 43.420 44.436 46.353 1.001 22000
  89. beta 9.836 0.162 9.522 9.726 9.836 9.945 10.156 1.001 22000
  90. sigma 5.013 0.114 4.794 4.936 5.011 5.089 5.241 1.001 22000
  91. sigma_a 10.816 1.151 8.832 10.009 10.728 11.517 13.362 1.001 22000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement