Hello Statalist,

As a result of an LCA I have three variables containing the probability of each observation to belong to a specific group (prob_1, prob_2, prob_3). The values of all three variables are by definition between 0 and 1.
To account for uncertainty in class assignment ( when using LCA as an independent variable) I want to run a simulation with a specific amount of iterations. In this simulation, the assignment to the class should be random while taking the probabilities (prob_1, prob_2, prob_3) into account.
The following code should demonstrate what I want to do (regarding the assignment of the class)
Code:
generate wanted = . 
mata: st_store(., "wanted", rdiscrete(2100, 1, ("0.5", "0.2", "0.3")))
I my case, probabilities vary across observations. Therefore I would like to substitute the general probabilities by individual probabilities, which are saved in the variables prob_1, prob_2 and prob_3
Code:
generate wanted = . 
mata: st_store(., "wanted", rdiscrete(2100, 1, ("prob_1", "prob_2", "prob_3")))
This however gives me the following error
rdiscrete(): 3253 nonreal found where real required
<istmt>: - function returned error
I guess the error message means that only numbers can be put where I wrote "prob_1" etc.

Therefore I thought of saving individual values in a local and than looping across all observations.
Something like this:
Code:
forvalues i = 1(1)2100 {
local prob_1_local = prob_1[`i']
local prob_2_local = prob_2[`i']
local prob_3_local = prob_3[`i']
display `prob_1_local'
display `prob_2_local'
display `prob_3_local'

mata: st_store(., "wanted", rdiscrete(1, 1, (`prob_1_local', `prob_2_local', `prob_3_local')))
}
This code gives me the error
sum of the probabilities must be 1
rdiscrete(): 3300 argument out of range
<istmt>: - function returned error
The error message is the reason why I added the display function to the code. The displayed values in my case are:
.3423453
.56572497
.09192975
Which almost perfectly add up to 1.

Can anyone help me with my approach? I'm also open to totally different approaches in case my whole approach was wrong.

Thanks

Jay