# Non-convergence of latent variable model

**URL:** https://discourse.mc-stan.org/t/non-convergence-of-latent-variable-model/12450
**Category:** Modeling
**Created:** [December 26, 2019, 8:03pm UTC](https://discourse.mc-stan.org/t/non-convergence-of-latent-variable-model/12450 "2019-12-26T20:03:03Z")
**Posts on this page:** 1
**Showing post:** 28

<div class="post-metadata">

### Author: ![Mauricio\_Garnier-Villarre](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mauricio_garnier-villarre/32/5702_2.png) [@Mauricio\_Garnier-Villarre](https://discourse.mc-stan.org/u/Mauricio_Garnier-Villarre)
#### Post date: [February 13, 2020, 7:20am UTC](https://discourse.mc-stan.org/t/non-convergence-of-latent-variable-model/12450/28 "2020-02-13T07:20:32Z")

</div>

Lucas

Glad it is working now

> [@ldeschamps](#):
>
> I am still in search for a solution about how to write a combination of loops and if statement to correct the sign of rho without having to change the code when the number of factor change.

I agree, i havent found a way to automate this section of sign adjusting.

> [@ldeschamps](#):
>
> Does the sign correction will have to be propagated to the slopes linking predictor to factor scores too??

Yes, basically sign correct the slope as another correlation, following the same rules/steps as before

blavaan handles the regressions in a way way more fancy than I can code, this does makes it harder to read sometimes

How I have code the regressions between factors, follow the next “rules” changes in the model section. For an example like this

```r
HS.model <- ' visual =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed =~ x7 + x8 + x9 
visual ~ textual + speed
'

```

1. The outcome is set to follow a normal() instead of multivariate\_normal(), because it is no longer correlated with anything else
2. The 2 predictors factor, still follow a multivariate\_normal(), for the correlation between them
3. The mean of the predictor factors are fixed to 0, and their SD = 1
4. The mean of the outcome factor is a function of the regression (M3), with an intercept of 0, to keep the factor identification constraints, the total variance is set to 1

I dont have a full example, but here would be the transform parameter and model section. In the generated quantities you would need to add the slopes into the sign correction if statements

```stan
transformed parameters{
vector[D-1] M;
vector<lower=0, upper=100>[D-1] Sd_d; // standard deviations
real mu[N,P];
matrix[D-1,D-1] Ly;// cholesky of the corr between factors
vector[N] M3;

for (m in 1:(D-1)) {
M[m] = 0;
Sd_d[m] = 1;}

Ly = diag_pre_multiply(Sd_d, L_corr_d);

for(i in 1:N){
mu[i,1] = b[1] + lam[1]*FS[i,1];
mu[i,2] = b[2] + lam[2]*FS[i,1];
mu[i,3] = b[3] + lam[3]*FS[i,1];
mu[i,4] = b[4] + lam[4]*FS[i,2];
mu[i,5] = b[5] + lam[5]*FS[i,2];
mu[i,6] = b[6] + lam[6]*FS[i,2];
mu[i,7] = b[7] + lam[7]*FS3[i];
mu[i,8] = b[8] + lam[8]*FS3[i];
mu[i,9] = b[9] + lam[9]*FS3[i];

M3[i] = reg[1]*FS[i,1] + reg[2]*FS[i,2];
}
}

model{

L_corr_d ~ lkj_corr_cholesky(2);
b ~ normal(0, 5);
lam ~ normal(0.4,10);
reg ~ normal(0.2,10);

FS3 ~ normal(M3, 1);

for(i in 1:N){  
FS[i] ~ multi_normal_cholesky(M, Ly);
}

}

```

Hope something like this works

---

_[View the full topic](https://discourse.mc-stan.org/t/non-convergence-of-latent-variable-model/12450)._
