#generate some fake data
N = 20
#parameters
intercept_mean = 100
intercept_sd = 15
effect_mean = 10
effect_sd = 3
intercept_effect_correlation = 0
#covariance matrix
v = matrix(
data = c(
intercept_sd^2
, intercept_sd*effect_sd*intercept_effect_correlation
, intercept_sd*effect_sd*intercept_effect_correlation
, effect_sd^2
)
, nrow = 2
, ncol = 2
)
obs = MASS::mvrnorm(N,c(intercept_mean,effect_mean),v)
#combine to long data format
long_data = data.frame(
S = rep(1:N,times=2) #subject labels
, Y = c(
obs[,1]-obs[,2]/2
, obs[,1]+obs[,2]/2
)
, condition = factor(rep(c('A','B'),each=N))
)
data {
int nY ; # number of observations
int nS ; # number of subjects
vector[nY] Y ; # observations
vector[nY] X ; # condition (-.5,.5)
int S[nY] ; # subject label (1:nS) for each observation
}
parameters {
real intercept ; #intercept
real effect ; #effect
real<lower=0> intercept_sd ; #between-Ss sd on intercept
real<lower=0> effect_sd ; #between-Ss sd on effect
vector[nS] subject_intercept ; #storage vector for subject-by-subject intercept values
vector[nS] subject_effect ; #storage vector for subject-by-subject effect values
}
transformed parameters{
vector[nY] y ; #storage vector for observation-by-observation predicted y values
for(ny in 1:nY){
mu[ny] <- subject_intercept[S[ny]] + X[ny]*(subject_effect[S[ny]]/2) ;
}
}
model {
#priors for parameters
intercept ~ normal(100,5) ; #prior on intercept
effect ~ normal(0,5) ; #prior on effect
intercept_sd ~ cauchy(0,15) ; #prior on intercept_sd
effect_sd ~ cauchy(0,15) ; #prior on effect_sd
#asserting that subjects are sampled from a population
subject_intercept ~ normal(intercept,intercept_sd) ;
subject_effect ~ normal(effect,effect_sd) ;
for(ny in 1:nY){
Y[ny] ~ y[ny] ;
}
}PS: note that I did not look at your R code
data { # N: number of cases int<lower=1> N ; # Y: matrix of outcomes for each case & variable matrix[N,2] Y ; # n_missing: number of instances of missing data int n_missing ; # indices_of_missing: matrix of the row and column indices of missing data int indices_of_missing[n_missing,2] ;}parameters { # means: vector of population-level means for intercept & slope vector[2] means ; # sds: vector of population-level sds for intercept & slope vector<lower=0>[2] sds ; # cor: population-level correlation between intercept & slope corr_matrix[2] cor ; # imputed: vector of imputed values for missing observations vector[n_missing] imputed ;}transformed parameters{ # cov: covariance matrix (derived from cor & sds) matrix[2,2] cov ; #used in getting correlation to work # Y_with_imputed: matrix of observations including imputed values matrix[N,2] Y_with_imputed ; # Z: matrix of latent subject-by-subject intercepts & effects matrix[N,2] Z ; #replace any missing values in Y Y_with_imputed = Y ; for(i in 1:n_missing){ Y_with_imputed[indices_of_missing[i][1],indices_of_missing[i][2]] = imputed[i] ; } #express Z as intercept/slope representation of Y_with_imputed for(i in 1:N){ Z[i,1] = (Y_with_imputed[i,1]+Y_with_imputed[i,2])/2 ; Z[i,2] = Y_with_imputed[i,1]-Y_with_imputed[i,2] ; } #compute covariance matrix from cor & sds cov = quad_form_diag(cor,sds) ;}model { #priors on population parameters; presumes data has been scale()ed means ~ normal(0,1) ; sds ~ lognormal(0,1) ; cor ~ lkj_corr(1) ; #assert sampling of case-level outcomes on each variable given # multivariate normal population for(s in 1:N){ Z[s,] ~ multi_normal(means,cov) ; }}