Y[i,j] ~ Normal(alpha[k[i,j] + Beta1[X1[i,j] + Beta2[X2[i,j] +Beta3(X1[i,j]*X2[i,j,), sigma^2), for i = 1,...,n subjects, for j = 1 to 2,m gender.Model1 <- lmer(Y ~ X1 + X2 + X1:X2 + (1 | subject) + (1 | gender)
data { int<lower=0> N; //no. of observations int<lower=0> n_subject; //no. of subjects int<lower=0> n_gender; //no. of genders int<lower=1, upper=n_subject> subject[N]; // label for each subject or individual int<lower=1, upper=n_sex> gender[N]; // grouping gender vector[N] u1; //fixed or predictator variable vector[N] u2; //fixed or predicator variable 2 vector[N] y; //Y 'growth' variable}parameters { real b1; real b2; real b3; vector[n_subject] a1; vector[n_gender] a2; real<lower=0, upper=100> sigma_a1; real<lower=0, upper=100> sigma_a2; real<lower=0, upper=100> sigma_y;}transformed parameters{ vector[N] y_hat; vector[N] inter; interaction term for (i in 1:N) inter <- u1*u2 y_hat[i] <- b1*u1[i] + b2*u2[i] + b3*inter[i] + a1[subject[i]] + a2[gender[i]];} model {b1 ~ normal(0, 100);b2 ~ normal(0, 100);b3 ~ normal(0, 100); a1 ~ normal(0, sigma_a1);a2 ~ normal(0, sigma_a2);growth ~ normal(y_hat, sigma_y);}--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Also, don’t do normal(0,100); better to do normal(0,10) or even normal(0,1). I’m assuming your predictors have been scaled reasonably.
Can you send something that compiles?
u1 and u2 are both vectors, so you can't multiply
them like this. And you're missing semicolons and
brackets on the loop.
inter <-u1.*u2;y_hat <- b1 * u1 + b2 * u2 + b3 * inter;
for (n in 1:N)
y_hat[n] <- y_hat[n] + a1[subject[n]] + a2[gender[n]];
transformed parameters {
b1 <- b1_mu + b1_raw * b1_sd;
}
parameters {
real b1_mu;
real<lower=0> b1_sd;
real b1_raw;
}
model {
b1_raw ~ normal(0, 1); // implies b1 ~ normal(b1_mu, b1_sd)
}
Then you just use b1 in your model as you normally would. Also, you can just drop the b1_mu if you want to use a mean of 0.
Model1 <-lmer(Y~F.sex+Age*C1*C2+(1|subject)
data { int N; //number of data points real Y[N]; //growth real age[N]; //age real Fsex2[N]; //factor of gender coded as 1 for being sex "2" and 0 for not being sex "2" real C1[N]; //covariate 1 real C2[N]; //covariate 2 int subject_index[N]; //subject id int N_subject_index;}transformed data{ real age_C1[N]; real age_C2[N]; real C1_C2[N]; real age_C1_C2[N]; for(i in 1:N){ age_C1[i] <- age[i]*C1[i]; age_C2[i] <- age[i]*C2[i]; C1_C2[i] <- C1[i]*C2[i]; age_C1_C2[i] <- age[i]*C1[i]*C2[i]; }}parameters { real Intercept; // Grand intercept real beta_age; // could use a vector of betas instead, i.e. vector[9] Beta, but its easier to pick out each coefficient this way. real beta_Fsex2; real beta_C1; real beta_C2; real beta_ageC1; // age_chum interaction real beta_ageC2; // age_npgo interaction real beta_C1C2; real beta_ageC1C2; real<lower=0> sigma; real vary_subject_index[N_subject_index]; real<lower=0> sigma_subject_index;} model { real vary[N]; real glm[N]; //priors Intercept ~ normal(0,10); beta_age ~ normal(0,10);
beta_Fsex2 ~ normal(0,10); beta_C1 ~ normal(0,10); beta_C2 ~ normal(0,10); beta_ageC1 ~ normal(0,10); beta_ageC2 ~ normal(0,10); beta_C1C2 ~ normal(0,10); beta_ageC1C2 ~ normal(0,10); sigma_subject_index ~ lognormal(0,1); sigma ~ lognormal(0,1); // Varying or Random effects for ( j in 1:N_subject_index ) vary_subject_index[j] ~ normal( 0 , sigma_subject_index ); // Fixed effects for ( i in 1:N ) { vary[i] <- vary_subject_index[subject_index[i]]; glm[i] <- vary[i] + Intercept + beta_age * age[i] + beta_Fsex2 * Fsex2[i] + beta_C1*C1[i] + beta_C2*C2[i] + beta_ageC1*age_C1[i] + beta_agec2*age_C2[i] + beta_C1C2*C1_C2[i] + beta_ageC1C2*age_C1_C2[i]; } Y ~ normal(glm, sigma);}generated quantities{ real vary[N]; real glm[N]; vector [N] log_lik; real dev; dev <- 0; for ( i in 1:N ) { vary[i] <- vary_subject_index[subject_index[i]]; glm[i] <- vary[i] + Intercept vary[i] <- vary_subject_index[subject_index[i]]; glm[i] <- vary[i] + Intercept + beta_age * age[i] + beta_Fsex2 * Fsex2[i] + beta_C1*C1[i] + beta_C2*C2[i] + beta_ageC1*age_C1[i] + beta_agec2*age_C2[i] + beta_C1C2*C1_C2[i] + beta_ageC1C2*age_C1_C2[i]; log_lik[i] <- normal_log( growth[i] , glm[i] , sigma ); dev <- dev + (-2)* normal_log(growth[i] , glm[i] , sigma); }}
That still has a bunch of multiply-by-zero you don't need in
the Fsex2 case, which is a boolean indicator, but there's not
a great way to speed that up without writing a function that either
inserts aconstant 0 if Fsex2[n] is 0 and inserts beta_Fsex2 otherwise.
No useless multiplications by 1 or 0 that way.
vector fill_if(int[] include, real beta) {
vector[num_elements(vector)] v;
real z;
z <- 0;
for (i in 1:num_elements(vector))
if (include[i])
v[i] <- beta;
else
v[i] <- z;
}
}
and then replace
+ beta_Fsex2 * Fsex2
with
+ fill_if(Fsex2, beta_Fsex2)