transformed data{ vector[2] coef_means ; #intercept & effect means vector[2] coef_sds ; #intercept & effect sds corr_matrix[2] cor_mat ; #correlation btwn intercept & effect matrix[2,2] contrasts ; #contrasts to generate observed data #filling correlation matrix cor_mat[1,1] = 1 ; cor_mat[1,2] = .6 ; cor_mat[2,1] = .6 ; cor_mat[2,2] = 1 ; #fill means & sds coef_means[1] = 0 ; #intercept mean coef_means[2] = 0 ; #effect mean coef_sds[1] = 1 ; #intercept sd coef_sds[2] = 1 ; #effect sd #fill contrasts contrasts[1,1] = 1 ; #intercept contrasts[2,1] = 1 ; #intercept contrasts[1,2] = -1 ; #effect * -1 contrasts[2,2] = 1 ; #effect * 1}parameters{ vector[2] coefs ; #observed intercept & effect}model{ #observed intercept & effect as multivariate normal coefs ~ multi_normal( coef_means , quad_form_diag( cor_mat , coef_sds ) ) ;}generated quantities{ matrix[2,3] dat ; dat[,1:2] = contrasts ; #save the contrasts used dat[,3] = contrasts * coefs ; #compute observed vals}library(rstan)nSubj = 100
#generate nSubj "subjects" of observed datafrom_stan = rstan::stan( file = 'make_data.stan' , iter = nSubj*2 , seed = 1 , chains = 1)dat = rstan::extract(from_stan)$datdat = matrix(dat,ncol=3) #unwrap arrayX = cbind(dat[,1],dat[,2]) #contrastsY = dat[,3] #observed valuessubjID = rep(1:nSubj,times=2) #subject labelsest_coefs = matrix(NA,ncol=2,nrow=nSubj)for(i in 1:nSubj){ est_coefs[i,] = coef(lm(Y[subjID==i]~0+X[subjID==i,]))}data_for_stan = list( nSubj = nSubj , est_coefs = est_coefs)
data { int<lower=1> nSubj ; matrix[nSubj,2] est_coefs ;}parameters { vector[2] coef_means ; vector<lower=0>[2] coef_sds ; corr_matrix[2] cor_mat ;}model { coef_means ~ normal(0,1) ; coef_sds ~ weibull(2,1) ; cor_mat ~ lkj_corr(2) ; for(s in 1:nSubj){ est_coefs[s,] ~ multi_normal( coef_means , quad_form_diag(cor_mat, coef_sds) ) ; }}data { int<lower=1> nSubj ; int<lower=1> nY ; int<lower=1> nX ; vector[nY] Y ; matrix[nY,nX] X ; int subjIndices[nSubj,2] ; #indices of start & finish of each subject in Y}transformed data{ matrix[nSubj,nX] obs_coefs ;
for(s in 1:nSubj){ obs_coefs[s] = transpose( inverse( transpose( X[subjIndices[s,1]:subjIndices[s,2]] ) * X[subjIndices[s,1]:subjIndices[s,2]] ) * transpose( X[subjIndices[s,1]:subjIndices[s,2]] ) * Y[subjIndices[s,1]:subjIndices[s,2]] ) ; }}data { int<lower=1> nSubj ; int<lower=1> nY ; int<lower=1> nX ; vector[nY] Y ; matrix[nY,nX] X ; int subjIndices[nSubj,2] ;}transformed data{ matrix[nSubj,nX] obs_coefs ; matrix[nSubj,nX] scaled_coefs ; for(s in 1:nSubj){ obs_coefs[s] = transpose( inverse( transpose( X[subjIndices[s,1]:subjIndices[s,2]] ) * X[subjIndices[s,1]:subjIndices[s,2]] ) * transpose( X[subjIndices[s,1]:subjIndices[s,2]] ) * Y[subjIndices[s,1]:subjIndices[s,2]] ); } #scale the coefs for easy default priors for(i in 1:nX){ scaled_coefs[,i] = (obs_coefs[,i] - mean(obs_coefs[,i])) / sd(obs_coefs[,i]) ; }}parameters { vector[nX] scaled_coef_means ; vector<lower=0>[nX] scaled_coef_sds ; corr_matrix[nX] cor_mat ;}model { scaled_coef_means ~ normal(0,1) ; scaled_coef_sds ~ weibull(2,1) ; cor_mat ~ lkj_corr(1) ; for(s in 1:nSubj){ scaled_coefs[s,] ~ multi_normal( scaled_coef_means , quad_form_diag(cor_mat, scaled_coef_sds) ) ; }}generated quantities{ vector[nX] coef_means ; for(i in 1:nX){ coef_means[i] = scaled_coef_means[i] * sd(obs_coefs[,i]) + mean(obs_coefs[,i]) ; }}--
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+unsubscribe@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.