Advertisement
Guest User

Untitled

a guest
May 25th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. # Claas Heuer, 2015
  2.  
  3. #####################################
  4. ### including PCAs in BVSR - STAN ###
  5. #####################################
  6.  
  7.  
  8. ## does not work - stan cant handle indicator variables
  9.  
  10. # some random data
  11. n = 100
  12. p = 20
  13. y <- rbinom(n,1,0.4)
  14. Z <- matrix(rnorm(n*p),n,p)
  15. X <- matrix(1,n,1)
  16.  
  17. dat = list(N=length(y),Px = ncol(X), Pz = ncol(Z), Z=Z,y=y,X=X)
  18.  
  19. library(rstan)
  20.  
  21. hcode =
  22. "
  23.  
  24. data {
  25.  
  26. int<lower=0> Px;
  27. int<lower=0> Pz;
  28. int<lower=0> N;
  29. matrix[N,Px] X;
  30. matrix[N,Pz] Z;
  31. # binomial phenotype
  32. int<lower=0,upper=1> y[N];
  33.  
  34. }
  35.  
  36. parameters {
  37.  
  38. vector[Px] beta; // flat prior
  39. vector[Pz] u;
  40. // this gives the lower bound for the variance component
  41. real<lower=0> tau;
  42.  
  43. // inclusion probability parameter
  44. real<lower=0,upper=1> pind; // uniform prior between 0 and 1
  45. int<lower=0,upper=1> include[Pz];
  46.  
  47. }
  48.  
  49.  
  50. model {
  51.  
  52. # sample inclusion paramter
  53. for (k in 1:Pz)
  54. include[k] ~ bernoulli(pind);
  55.  
  56. for (k in 1:Pz)
  57. u[k] ~ normal(0,tau);
  58.  
  59. for (i in 1:N)
  60. y[i] ~ bernoulli(Phi(X[i] * beta + Z[i] * (u * include)));
  61.  
  62. tau ~ cauchy(0,5);
  63.  
  64. }
  65.  
  66. "
  67.  
  68.  
  69. hlm = stan(model_name="Hierarchical Linear Model", model_code = hcode, data=dat , iter = 10000, verbose = FALSE, chains=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement