'data.frame': 30000 obs. of 4 variables: $ pupil.ID : num 1 2 3 4 5 6 7 8 9 10 ... $ school.ID: num 1 1 1 1 1 1 1 1 1 1 ... $ outcome : num 16.2 22.1 14.8 23.6 16.9 ... $ year : Factor w/ 3 levels "2004","2005",..: 1 1 1 1 1 1 1 1 1 1 ...
I tried to run it in rstan like this :
schools_code <- '
data {
int<lower=1> n2 ;
int<lower=1> N ;
real y[N] ;
int school[N] ;
}
parameters {
real mu[N] ;
real u2[n2] ;
real<lower=0> tau ;
real<lower=0> tau.u2 ;
real beta ;
}
model
{
# Level 1 definition
for(i in 1:N) {
y[i] ~ normal(mu[i],tau) ;
mu[i] <- beta + u2[school[i]] ;
}
# level 2 definitions
for (j in 1:n2) {
u2[j] ~ normal(0,tau.u2) ;
}
# Priors for fixed effects
beta[1] ~ uniform(0,1000) ;
# Priors for random terms
tau ~ dgamma(0.001000,0.001000) ;
sigma2 <- 1/tau ;
tau.u2 ~ dgamma(0.001000,0.001000) ;
sigma2.u2 <- 1/tau.u2 ;
}
'
schools_dat <- list(N = 30000, n2 = 300,
y = dt.all$outcome,
school = dt.all$school.ID)
fit <- stan(model_code = schools_code, data = schools_dat,
iter = 1000, chains = 4)
but it does note parse.If anyone can tell me what is wrong I would be grateful.
The 2nd model islmer(outcome ~1 + (year|school.ID),data=dt.all)
If anyone can provide some hints for how to specify this model I would be grateful.
data {
int<lower=1> n2 ;
int<lower=1> N ;
real y[N] ;
int school[N] ;
}
parameters {
// real mu[N] ; // moved to model{} section
real u2[n2] ;
real<lower=0> tau ;
// real<lower=0> tau.u2 ;
real<lower=0> tau_u2 ; // no periods in names
// real beta ;
real <lower=0,upper=1000> beta; // add bounds on the support of beta
}
model
{
real mu; // can be scalar in this case
real sigma;
sigma <- sqrt(1/tau);
# Level 1 definition
for(i in 1:N) {
mu <- beta + u2[school[i]] ;
y[i] ~ normal(mu,sigma) ; // normal is parameterized in terms of the standard deviation
}
# level 2 definitions
// for (j in 1:n2) {
// u2[j] ~ normal(0,tau.u2) ;
// }
u2 ~ normal(0, sqrt(1/tau_u2)); // vectorize instead of looping where possible and use standard deviation
# Priors for fixed effects
// beta[1] ~ uniform(0,1000) ; // not necessary when the support of beta is bounded on both sides
# Priors for random terms
// use gamma, rather than dgamma
tau ~ gamma(0.001000,0.001000) ;
tau_u2 ~ gamma(0.001000,0.001000) ;
}
However it seems to be very slow - so far it has taken about 30 mins for 1000 iterations (chain 1) and so far on chain 2 it has taken 40 mins and only reached 500 iterations..... This compares to about 30 seconds for 5000 iterations with mlwin (MCMC estimation mode) and 40 seconds for 10,000 iterations with MCMCglmm - with the same data.
Is there anything I can do to improve the speed ? I already set CXXFLAGS to 03 in the makeconf
Oh, I guess the u2[1] to u2[300] are the individual school effects and I just have to compute the variance of them ?
But it's strange that there are only 100 unique schools.
transformed parameters {
real<lower=0> sigma_u2;
sigma_u2 <- sqrt(1/tau_u2);
}
Oh, I guess the u2[1] to u2[300] are the individual school effects and I just have to compute the variance of them ?
ranef(myLmerModel)Thank you, although I am sorry that I don't understand. With the code you wrote below, u2 is then undefined and it doesn't parse.
data {
int<lower=1> n2 ;
int<lower=1> N ;
real y[N] ;
int school[N] ;
}
parameters {
real u2_z[n2] ;
real<lower=0> sigma ;
real<lower=0> sigma_u2;
real <lower=0,upper=1000> beta;
}
model {
real mu[N];
for (i in 1:N)
mu[i] <- beta + u2_z[school[i]] * sigma_u2;
y ~ normal(mu,sigma);
u2_z ~ normal(0, 1) ;
// or some other prior on sigma and sigma_u2
sigma ~ gamma(0.001000,0.001000) ;
sigma_u2 ~ gamma(0.001000,0.001000) ;
}
Ben
On Monday, October 8, 2012 2:41:50 PM UTC-4, Joe King wrote:Thank you, although I am sorry that I don't understand. With the code you wrote below, u2 is then undefined and it doesn't parse.
Can't quite tell what you mean. It is always easier if you just attach the file that does or doesn't parse. Here is what I would do, approximately incorporating Matt and Bob's advice (also attached):
Thanks ! It has parsed now. And it seems very much faster, However, it gives the following warning several times during iterations:
Warning (non-fatal rejection): Error in function stan::prob::normal_log(N4stan5agrad3varE): Scale parameter is 0:0, but must be > 0!
Also from the final output from print(fit) sigma and sigma_u2 have an n_eff of only 2 and beta of only 15
parameters {
real<lower = .1, upper = 100> sigma;
real<lower = .1, upper = 100> sigma_u2;
...
}
The REML estimates of the standard deviations in the lmer fit are 5.9 and 10.3 for between and within groups, respectively. My guess is that the between groups standard deviation (sigma_u2) is very poorly determined, which may be causing the chain to wander. When the between groups standard deviation is dominated by the residual variability like this, it is very hard to estimate it.Joe, would you be willing to make the data available somewhere so that we could check on some of these issues?
--
lmer(outcome ~1 + (year|school.ID),data=dt.all)> make /path/to/joe
> summary(samples[,!grepl("u2_z", colnames(samples))])
lp__ treedepth__ stepsize__ sigma
Min. :-21258 Min. :4.000 Min. :0.006126 Min. :4.941
1st Qu.:-21230 1st Qu.:4.000 1st Qu.:0.006126 1st Qu.:5.019
Median :-21224 Median :4.000 Median :0.006126 Median :5.046
Mean :-21224 Mean :4.226 Mean :0.006126 Mean :5.046
3rd Qu.:-21217 3rd Qu.:4.000 3rd Qu.:0.006126 3rd Qu.:5.074
Max. :-21200 Max. :7.000 Max. :0.006126 Max. :5.172
sigma_u2 beta
Min. :14.85 Min. :18.60
1st Qu.:17.10 1st Qu.:21.13
Median :17.89 Median :22.15
Mean :18.11 Mean :21.81
3rd Qu.:19.13 3rd Qu.:22.60
Max. :21.08 Max. :23.90
Is sigma_u2 really so far from the true value? It looks to me like its
true value (16) is within the range of the simulations.
Never mind about that hypothesis I mentioned.
The reason for the slow mixing we're seeing is pretty basic—in this
model, the intercept beta (just out of curiosity—why call it beta
instead of alpha?) is very highly anti-correlated with the
school-level effects u2. On average, the posterior correlation is
around -0.9. So that's the source of the slow mixing.
library(lme4)library(rstan)
set.seed(1234321)samp <- within(data.frame(schoolID=gl(300,100)), outcome <- 20 + rnorm(30000, sd=10) + rnorm(300,sd=5)[schoolID])(lm1 <- lmer(outcome ~ 1|schoolID, samp))## The next line can only be run with the development lme4 installed as## install.packages("lme4", repos="http://R-forge.R-project.org")densityplot(pr1 <- profile(lm1), ylab=NULL, layout=c(3,1))
schools_code <- ' data { int<lower=1> N; // number of observations int<lower=1> J; // number of schools int<lower=1,upper=J> school[N]; // school for each obs real y[N]; // observed outcome } parameters { real mu; real<lower=0> sigma; real<lower=0> sigma1; real eta[J]; } model { real mm[N]; for (i in 1:N) mm[i] <- mu + eta[school[i]];
y ~ normal(mm, sigma); eta ~ normal(0, sigma1);
sigma ~ gamma(0.001000,0.001000) ; sigma1 ~ gamma(0.001000,0.001000) ; }'
fit <- stan(model_name="schools", model_code=schools_code, data=with(samp, list(N=nrow(samp), J=length(levels(schoolID)), y=outcome, school=as.integer(schoolID))), iter = 1000, chains = 4)