lmer(outcome ~1 + (1|school.ID),data=dt.all)lmer(outcome ~1 + (year|school.ID),data=dt.all)
orlmer(outcome ~1 + (year-1|school.ID),data=dt.all)
Hi Bob
Thanks for your reply, though I am a bit confused. There are no predictors in this model (yet).
--
for (i in 1:N) {
mu[i] <- beta + u2_z[school[i]] * sigma_u2 + gamma + v2_z[year[i]] * sigma_v2 ;} intercept <- beta + u2_z[school[i]] * sigma_u2 ; slope <- gamma + v2_z[year[i]] * sigma_v2 ; slope <- gamma + v2_z[year[i]] * sigma_v2 ;
intercept <- beta + u2_z[school[i]] * sigma_u2 ;After making the change you suggested, I got the following compilation error:
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) :
variable year not found.
Is this what you meant by "You will then run into a problem where your year variable is a factor rather than a numeric variable" ? I didn't know how to specify a factor in the data{} section : can you tell me how to do that ?
data(InsectSprays)
summary(InsectSprays$spray)
int_spray <- as.integer(InsectSprays$spray)
summary(int_spray)
Then it compiled and seemed to be running, until I got the popup:
Microsoft Visual C++ Runtime Library
This application has requested the Rutime to terminate in an unusual way.
Please contact the application's support team for more information.
And the message that R had stopped working, to which I answered "close", though it didn't close down and the process seemed to be still in a loop (thrashing the CPU but unresponsive) so I had to kill the process.
Please advise
system("R CMD config CXX")After making the change you suggested, I got the following compilation error:
Error in new_CppObject_xp(fields$. module, fields$.pointer, ...) :
variable year not found.
Is this what you meant by "You will then run into a problem where your year variable is a factor rather than a numeric variable" ? I didn't know how to specify a factor in the data{} section : can you tell me how to do that ?
Thanks Ben
The result of system ("R CMD config CXX") is
g++ -m64
The problem is different when I use as.integer instead of as.numeric. In that case I get this error
SAMPLING FOR MODEL 'schools_code' NOW (CHAIN 1).
Error in sampler$call_sampler(args_list[[i]]) : std::bad_alloc
Note: it runs OK with
v2_z ~ normal(0, 1) ;
but not
v2_z ~ gamma(0.001000,0.001000) ;
- Bob
--
Thanks Ben
The result of system ("R CMD config CXX") is
g++ -m64
The problem is different when I use as.integer instead of as.numeric. In that case I get this error
SAMPLING FOR MODEL 'schools_code' NOW (CHAIN 1).
Error in sampler$call_sampler(args_ list[[i]]) : std::bad_alloc
Note: it runs OK with
v2_z ~ normal(0, 1) ;
but not
v2_z ~ gamma(0.001000,0.001000) ;
Hi Ben
Why is it wrong to put a gamma prior on v2_z ? It's a variance so it can't be negative.....
parameters {
real u2_z[n2] ;
real v2_z[N_year] ;
...
Joe said:
> v2_z ~ gamma(0.001000,0.001000) ;
Then Ben said:
> .. Putting a gamma(0,0) prior on v2_z is just wrong as far as I can tell.
And Joe then asked:
> Why is it wrong to put a gamma prior on v2_z ? It's a variance so it can't be negative.....
It's not "wrong" in the sense that it's illegal,
but wrong in the sense that it's too concentrated
around zero.
Sorry to keep going on about it, but this is what I am trying to replicate (I highlighted them in red)
> fit.lme4.all <- lmer(outcome ~1 + (year-1|school.ID),data=dt.all)
> summary(fit.lme4.all)
Linear mixed model fit by REML
Formula: outcome ~ 1 + (year - 1 | school.ID)
Data: dt.all
AIC BIC logLik deviance REMLdev
183343 183410 -91664 183325 183327
Random effects:
Groups Name Variance Std.Dev. Corr
school.ID year2004 0.94242 0.97078
year2005 20.03281 4.47580 0.016
year2006 324.96381 18.02675 -0.233 -0.137
Residual 25.26760 5.02669
Number of obs: 30000, groups: school.ID, 100
Fixed effects:
Estimate Std. Error t value
(Intercept) 19.9286 0.1021 195.2
data {
int<lower=1> n2 ; // number of schools
int<lower=1> N ; // number of students
int<lower=1> N_year; // number of years
real y[N] ; // outcome
int school[N] ; // school id
int year[N] ; // year {1,2 or 3}
}
parameters {
real alpha; // intercept
vector[N_year-1] beta; // 2nd and 3rd expected slopes (first is constrained to be 0.0)
real<lower=0> sigma ; // standard error of regression
matrix[n2,N_year] slopes_z; // z-score of the slopes
corr_matrix[N_year] Tau; // correlation matrix of the slopes
real<lower=0> tau[N_year]; // standard deviations of the slopes
}
model {
real mu[N];
int k;
matrix[n2,N_year] slopes_error;
slopes_error <- slopes_z * transpose(cholesky_decompose(Tau)) ;
for (i in 1:N) {
k <- year[i];
mu[i] <- alpha + (if_else(k - 1, beta[k - 1], 0.0) +
tau[k] * slopes_error[school[i],k]);
}
y ~ normal(mu,sigma);
for (j in 1:N_year) {
tau[j] ~ cauchy(0.0,5.0) ;
col(slopes_z,j) ~ normal(0, 1) ;
}
alpha ~ cauchy(0.0,5.0);
beta ~ cauchy(0.0,5.0);
sigma ~ cauchy(0.0,5.0);
// implicit: Tau ~ lkj_corr(1.0);
}
Inference for Stan model: schools_code.
8 chains: each with iter=2000; warmup=1000; thin=1; 2000 iterations saved.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
alpha -0.786 0.002 0.112 -1.001 -0.862 -0.786 -0.712 -0.561 2106 1.003
beta[1] 0.405 0.025 0.466 -0.518 0.090 0.416 0.722 1.311 336 1.037
beta[2] 1.629 0.074 1.783 -1.895 0.412 1.592 2.816 5.213 587 1.018
sigma 5.027 0.000 0.021 4.986 5.012 5.027 5.041 5.068 8000 1.000
tau[1] 0.993 0.002 0.090 0.836 0.929 0.987 1.051 1.185 3077 1.002
tau[2] 4.579 0.012 0.338 3.979 4.341 4.554 4.791 5.289 740 1.012
tau[3] 18.080 0.057 1.237 15.751 17.248 18.049 18.902 20.589 466 1.019
Tau[1,1] 1.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 8000 NaN
Tau[1,2] 0.013 0.006 0.110 -0.200 -0.067 0.014 0.093 0.218 315 1.038
Tau[1,3] -0.217 0.005 0.105 -0.415 -0.290 -0.220 -0.147 -0.008 388 1.015
Tau[2,1] 0.013 0.006 0.110 -0.200 -0.067 0.014 0.093 0.218 315 1.038
Tau[2,2] 1.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 7254 0.999
Tau[2,3] -0.136 0.004 0.093 -0.318 -0.201 -0.135 -0.072 0.040 633 1.011
Tau[3,1] -0.217 0.005 0.105 -0.415 -0.290 -0.220 -0.147 -0.008 388 1.015
Tau[3,2] -0.136 0.004 0.093 -0.318 -0.201 -0.135 -0.072 0.040 633 1.011
Tau[3,3] 1.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 6610 0.999
Do you mean I should centre the outcome in R first with something like
outcome <- outcome - mean(outcome)
Also, would it make much difference if I use a implicit uniform prior on the parameters instead of cauchy ? I know how to do that with <lower=, upper> in the parameter section but how do I do that for the vector ?
real<lower=0> tau[N_year]; // standard deviations of the slopes
An implicit uniform prior on the taus over some reasonable bounded interval would probably yield about the same results. An improper or barely proper prior probably wouldn't yield the same results.
Ben
--
Hi BenI tried (in the model{})// alpha ~ cauchy(0.0,5.0); //now implicitly uniform// sigma ~ cauchy(0.0,5.0); //now implicitly uniformwith (in the parameters {} )real<lower=-10,upper=10> alpha; // intercept
real<lower=0,upper=20> sigma ; // standard error of regressionThat seemed to be working, but then I put
// beta ~ cauchy(0.0,5.0); //now implicitly uniformandvector<lower=0,upper=20>[N_year-1] beta; // expected slopes (first is constrained to be 0.0)which caused it to fail to parse. That's what I meant when I was talking about constraining the vector.....
real<lower=0,upper=20> beta[N_year-1];
On 10/23/12 4:32 PM, Andrew Gelman wrote:
> OK, now I have a question (forgive me if the answer's already in the manual). Suppose I define a parameter theta to
> have a normal (mu, sigma) distribution but then theta is declared as real<lower=0, upper=0.5>. Does Stan then work with
> the truncated normal density (i.e., getting the correct normalizing factor which is now a function of mu and sigma) or
> does it just use the normal density without adjusting for the truncation?
First, you never get the full normalization term in Stan.
All constant terms are dropped. In the normal, we never
include the constant normalizing term -log(sqrt(2*pi)).
The difference between
x ~ normal(mu,sigma); // no space before '('!!!
and
x ~ normal(mu,sigma) T[0,0.5];
is that the latter applies the truncated density and
the former does not.
If the variable x is declared as real<lower=0,upper=0.5>,
that is, with fixed bounds, then the first is OK as
we only need the density up to a proportion. If the
bounds were variable, you'd need the second form.
Hmm, now I'm confused. Here's what I think you're saying. Consider 3 possibilities:
1.
real theta
theta ~ normal (mu,sigma)
2.
real<lower=0,upper=0.5> theta
theta ~ normal (mu, sigma)
3.
real<lower=0,upper=0.5> theta
theta ~ normal (mu, sigma) T[0,0.5]
4.
real theta
theta ~ normal (mu, sigma) T[0,0.5]
On Tuesday, October 23, 2012 5:08:11 PM UTC-4, Andrew Gelman wrote:Hmm, now I'm confused. Here's what I think you're saying. Consider 3 possibilities:
or 4 :). My original FAQ email was a little off on this part. Read the edited version at
https://groups.google.com/d/msg/stan-users/4gv3fNCqSNk/VonPUkdcZuUJ
cauchy(0.0,5.0) T[0.0,] \propto cauchy(0.0,5.0)
and the same is true for other distributions that are symmetric around zero.
and tell me whether it makes sense. But my understanding of your 4 possibilities are ...1.
real theta
theta ~ normal (mu,sigma)
fine, as expected
2.
real<lower=0,upper=0.5> theta
theta ~ normal (mu, sigma)
will yield the wrong results if mu or sigma were unknown
3.
real<lower=0,upper=0.5> theta
theta ~ normal (mu, sigma) T[0,0.5]
will yield the right results, but truncation is not implemented for all distributions yet
4.
real theta
theta ~ normal (mu, sigma) T[0,0.5]
should be avoided because it generates proposals for theta with the log-posterior is not defined or is negative infinity.
Ben
--