I generated some fake 3-variable multivariate data via MASS::mvrnorm, and ran it through the following standard-ish multivariate model:
data {
# N: number of cases
int<lower=1> N ;
# K: outcomes per case
int<lower=2> K ;
# Y: matrix of outcomes for each case & variable
matrix[N,K] Y ;
}
parameters {
# means: vector of population-level means for intercept & slope
vector[K] means ;
# sds: vector of population-level sds for intercept & slope
vector<lower=0>[K] sds ;
# cor: population-level correlation between intercept & slope
corr_matrix[K] cor ;
}
transformed parameters{
# cov: covariance matrix (derived from cor & sds)
matrix[K,K] cov ; #used in getting correlation to work
#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 ~ weibull(2,1) ;
cor ~ lkj_corr(1) ;
#assert sampling of case-level outcomes on each variable given
# multivariate normal population
for(s in 1:N){
Y[s,] ~ multi_normal(means,cov) ;
}
}
I was interested in doing something akin to principal component analysis, so in R I computed the eigenvectors given each iteration's covariance matrix:
#get the samples of the covariance matrix
cov = rstan::extract(post,pars='cov')[[1]]
#define a function to compute eigenvectors on each sample
# covariance matrix
get_evec = function(i){
e = eigen(cov[i,,])
v = tibble::as_tibble(eigen(z[i,,])$vectors)
v$variable = 1:nrow(v)
v %>% tidyr::gather(
key = 'component'
, value = 'value'
, -variable
)
}
#compute all the eigenvectors
evec = purrr::map_df(
.x = 1:dim(z)[1]
, .f = get_evec
)
#visualize the eigenvectors
evec %>%
ggplot(
mapping = aes(
x = value
)
)+
geom_density(
colour = 'transparent'
, fill = 'black'
, alpha = .5
)+
facet_wrap(
~ component * variable
, scale = 'free'
, labeller = labeller(component = label_both,variable=label_both)
)
Which yields this plot that has clear bimodality in a few of the eigenvectors:
Is this expected behaviour?
Mike
--
Mike Lawrence
Graduate Student
Department of Psychology & Neuroscience
Dalhousie University
~ Certainty is (possibly) folly ~