I have one of the classic problems. Saving output from a regression to a matrix. I tried to search, google etc but couldn't find a solution.
I am running several regressions. It is actually a command called traj. A Stata plugin written i C. Downloadable from https://www.andrew.cmu.edu/user/bjones/index.htm In short it performs longitudinal finite mixture models. I am running several models, trying to find the best combination of trajectories and polynomials. I am saving BIC, AIC, etc in a matrix. I want to save another result as well. See below.
Dataset, it is actually 28 time points and values. Showing the first 5 values for 5 patients. Each timepoint ( time_n ) measures a organ failure score ( sofa_resp_orig_n ) in 660 critically ill patients.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(sofa_resp_orig_1 sofa_resp_orig_2 sofa_resp_orig_3 sofa_resp_orig_4 sofa_resp_orig_5 time_1 time_2 time_3 time_4 time_5) 2 1 0 0 0 1 2 3 4 5 2 3 2 3 2 1 2 3 4 5 2 2 . . . 1 2 3 4 5 2 . . . . 1 2 3 4 5 2 2 1 0 0 1 2 3 4 5 end
Code:
* Example generated by -dataex-. To install: ssc install dataex clear // Setting polynomials and groups. local polynom 3 local groups 6 // Creating matrix for later saving of results. This part is run once only. // Creating matrix local maxnum: display `polynom'^`groups' +1 display `maxnum' local columns `groups' matrix a = J(`maxnum',6,.) matrix group = J(`maxnum',`columns',.) timer clear timer on 1 // Here I am running the code. I am running it several times with different starting points (from different combinations of polynomials. // Saving the matrix to excel every time I have run a number of iterations. // This is because the regression sometimes does not converge and Stata freezes. I do not want to do it all from the first combination // if this happens. // State polynomials, number of groups (trajectories), maximum iterations (before saving to excel), which combination of // groups and polynomials to start from. local polynom 3 local groups 6 local maxiter 2 local iter_start 1 // Setting variable list for traj command local first "sofa_resp_orig_1" local connect "-" local second "sofa_resp_orig_28" local maxnum: display `polynom'^`groups' // Setting up matrix columns and naming them matrix colnames a = "Groups" "Polynomials" "BIC(N)" "BIC(panels)" "e(AIC)" "e(ll)" local bicn: display -10^99 local bicp: display -10^99 local aic: display -10^99 local e(BIC_N_data): display -10^99 local e(BIC_N_subjects): display -10^99 local e(AIC): display -10^99 // Filling up matrix with the possible combinations of polynomials. So I can start over at a specific combination later. matrix b = J(`maxnum',6,.) local i 1 forvalues a = 1/`polynom' { forvalues b = 1/`polynom' { forvalues c = 1/`polynom' { forvalues d = 1/`polynom' { forvalues e = 1/`polynom' { forvalues f = 1/`polynom' { matrix b[`i',1] = `a' matrix b[`i',2] = `b' matrix b[`i',3] = `c' matrix b[`i',4] = `d' matrix b[`i',5] = `e' matrix b[`i',6] = `f' local i `++i' } } } } } } // Extracting polynomial order to traj command forvalues a = 1/`maxiter'{ local aa = b[`iter_start',1] local bb = b[`iter_start',2] local cc = b[`iter_start',3] local dd = b[`iter_start',4] local ee = b[`iter_start',5] local ff = b[`iter_start',6] // Running the traj command. quietly traj, var(`first'`connect'`second') indep(time_1-time_28) model(cnorm) min(0) max(4) order(`aa' `bb' `cc' `dd' `ee' `ff' ) // Saving output to matrix matrix a[`iter_start',1]= e(numGroups1) matrix a[`iter_start',2]= `aa'`bb'`cc'`dd'`ee'`ff' matrix a[`iter_start',3]= e(BIC_N_data) matrix a[`iter_start',4]= e(BIC_n_subjects) matrix a[`iter_start',5]= e(AIC) matrix a[`iter_start',6]= e(ll) // !! here is my problem !! See below for description. local iter_start `++iter_start' } timer off 1 di "`maxiter' iterations, total time" timer list 1 // Saving matrix a (results summary) to excel in case Stata freezes. cd "whereever/- Renal 5gr 3 poly" putexcel set `first'`connect'`second'gr`groups'poly`polynom'_`maxiter'_`iter_start' , replace putexcel A1 = matrix(a), names end
e(groupSize1) is a matrix with one row and n columns. n is the number of trajectories estimated (6 for 6 groups/trajectories, 5 for five groups/trajectories etc).
SO I want to:
1. I want to be able to run a number of traj estimations.Starting at a given combination of trajectories and poynomials.
2. Save all results (BIC, AIC etc) to matrix a which is done in the code above. 1-2 works fine.
3. I want to save e(groupSize1)for each estimation and add this matrix to matrix a. This I can not do.
I could easily save e(groupSize1) to a matrix ( matrix groupsize = e(groupSize1)) But either matrix groupsize needs to be increased with ++i for each new estimation, and last in the code appended with matrix a.
Or, perhaps better, be added to matrix a after each run. But I then need to append matrix a with a number of columns (corresponding to the columns in e(groupSize1) every time.
Dummy code (for insertion in the code where my problem is:
matrix groupsize = e(groupSize1)
matrix a[`iter_start',7]= groupsize
This obviously does not work, I get a matrix configuration error. I get this even if I increase matrix a to be able to contain more columns. Any suggestions how to add e(groupSize1)to matrix a for every iteration of traj?
Hope I am making myself somewhat clear...
Best regards,
Jesper
0 Response to Iteratively saving output to matrix
Post a Comment