Hi,

I am trying to find the best combination of number of groups and polynomials in a model. I am using the Stata plug-in traj. It permits group-based trajectory modeling. My problem is that it takes such a long time to iterate all the possible combinations. Any suggestions to speed up my code a bit?

Dataset example.
sofa_card_orig_* is a variable taking integer values between 0-4.
time_* is just a dummy indicating time between the above measurements. It goes from 1-5

My real dataset has 722 observations, all observed under 28 days, sofa_card_orig_1 - sofa_card_orig_28 and time_1-time_28.


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(sofa_card_orig_1 sofa_card_orig_2 sofa_card_orig_3 sofa_card_orig_4 sofa_card_orig_5 time_1 time_2 time_3 time_4 time_5)
0 0 0 0 0 1 2 3 4 5
3 3 3 3 1 1 2 3 4 5
3 1 . . . 1 2 3 4 5
3 . . . . 1 2 3 4 5
0 0 0 0 0 1 2 3 4 5
end

Code:
* Example generated by -dataex-. To install: ssc install dataex
set trace off
set more off
set matsize 11000


    * set 
local groups 6
local polynom 2
local first "sofa_card_orig_1"
local connect "-"
local second "sofa_card_orig_28"




local maxnum: display `polynom'^1+`polynom'^2+`polynom'^3+`polynom'^4+`polynom'^5+`polynom'^6
matrix a = J(`maxnum',6,.)
matrix colnames a = "Groups" "Polynomials" "BIC(N)" "BIC(panels)" "e(AIC)" "e(ll)"
local bicn: display -10^99
local bicp: display -10^99
local e(BIC_N_data): display -10^99
local e(BIC_N_subjects): display -10^99



local i 1
forvalues a = 1/`polynom' {
quietly traj, var(`first'`connect'`second') indep(time_1-time_28) model(cnorm) min(0) max(4) order(`a')
matrix a[`i',1]= e(numGroups1)
matrix a[`i',2]= `a'
matrix a[`i',3]= e(BIC_N_data)
matrix a[`i',4]= e(BIC_n_subjects)
matrix a[`i',5]= e(AIC)
matrix a[`i',6]= e(ll)
if `e(BIC_N_data)' > `bicn' {
local bicn `e(BIC_N_data)'
local solutionn `a'
}
if `e(BIC_n_subjects)' > `bicp' {
local bicp `e(BIC_n_subjects)'
local solutionp `a'
}


local i `++i'
forvalues b = 1/`polynom' {
quietly traj, var(`first'`connect'`second') indep(time_1-time_28) model(cnorm) min(0) max(4) order(`a' `b')
matrix a[`i',1]= e(numGroups1)
matrix a[`i',2]= `a'`b'
matrix a[`i',3]= e(BIC_N_data)
matrix a[`i',4]= e(BIC_n_subjects)
matrix a[`i',5]= e(AIC)
matrix a[`i',6]= e(ll)
if `e(BIC_N_data)' > `bicn' {
local bicn `e(BIC_N_data)'
local solutionn `a'`b'
}
if `e(BIC_n_subjects)' > `bicp' {
local bicp `e(BIC_n_subjects)'
local solutionp `a'`b'
}


local i `++i'
forvalues c = 1/`polynom' {
quietly traj, var(`first'`connect'`second') indep(time_1-time_28) model(cnorm) min(0) max(4) order(`a' `b' `c')
matrix a[`i',1]= e(numGroups1)
matrix a[`i',2]= `a'`b'`c'
matrix a[`i',3]= e(BIC_N_data)
matrix a[`i',4]= e(BIC_n_subjects)
matrix a[`i',5]= e(AIC)
matrix a[`i',6]= e(ll)
if `e(BIC_N_data)' > `bicn' {
local bicn `e(BIC_N_data)'
local solutionn `a'`b'`c'
}
if `e(BIC_n_subjects)' > `bicp' {
local bicp `e(BIC_n_subjects)'
local solutionp `a'`b'`c'
}


local i `++i'
forvalues d = 1/`polynom' {
quietly traj, var(`first'`connect'`second') indep(time_1-time_28) model(cnorm) min(0) max(4) order(`a' `b' `c' `d')
matrix a[`i',1]= e(numGroups1)
matrix a[`i',2]= `a'`b'`c'`d'
matrix a[`i',3]= e(BIC_N_data)
matrix a[`i',4]= e(BIC_n_subjects)
matrix a[`i',5]= e(AIC)
matrix a[`i',6]= e(ll)
if `e(BIC_N_data)' > `bicn' {
local bicn `e(BIC_N_data)'
local solutionn `a'`b'`c'`d'
}
if `e(BIC_n_subjects)' > `bicp' {
local bicp `e(BIC_n_subjects)'
local solutionp `a'`b'`c'`d'
}

local i `++i'
forvalues e = 1/`polynom' {
quietly traj, var(`first'`connect'`second') indep(time_1-time_28) model(cnorm) min(0) max(4) order(`a' `b' `c' `d' `e')
matrix a[`i',1]= e(numGroups1)
matrix a[`i',2]= `a'`b'`c'`d'`e'
matrix a[`i',3]= e(BIC_N_data)
matrix a[`i',4]= e(BIC_n_subjects)
matrix a[`i',5]= e(AIC)
matrix a[`i',6]= e(ll)
if `e(BIC_N_data)' > `bicn' {
local bicn `e(BIC_N_data)'
local solutionn `a'`b'`c'`d'`e'
}
if `e(BIC_n_subjects)' > `bicp' {
local bicp `e(BIC_n_subjects)'
local solutionp `a'`b'`c'`d'`e'
}


local i `++i'
forvalues f = 1/`polynom' {
quietly traj, var(`first'`connect'`second') indep(time_1-time_28) model(cnorm) min(0) max(4) order(`a' `b' `c' `d' `e' `f')
matrix a[`i',1]= e(numGroups1)
matrix a[`i',2]= `a'`b'`c'`d'`e'`f'
matrix a[`i',3]= e(BIC_N_data)
matrix a[`i',4]= e(BIC_n_subjects)
matrix a[`i',5]= e(AIC)
matrix a[`i',6]= e(ll)
if `e(BIC_N_data)' > `bicn' {
local bicn `e(BIC_N_data)'
local solutionn `a'`b'`c'`d'`e'`f'
}
if `e(BIC_n_subjects)' > `bicp' {
local bicp `e(BIC_n_subjects)'
local solutionp `a'`b'`c'`d'`e'`f'
}






local i `++i'
}
}
}
}
}
}



matrix list a
display "Best solution BIC(n): " `solutionn'
display "Best solution BIC(p): " `solutionp'


* set 
putexcel set `first'`connect'`second'gr`groups'poly`polynom', replace
putexcel A1 = matrix(a), names
I have tried to remove all locals (as many as i can). I also tried to skip the parts where I get the best solution according to BIC. Both with minimal, if none, improvement in time.

All suggestions are welcome!

/Jesper