Hi everyone,
I am having a problem when building a loop that allwos me to add chunks of code to the next loop. Unfortunally I cant share the data because it is protected by national law. But I hope you understand what I am trying to achieve:
Here is the idea. I need to perform some operations (using the command nlcom) after running a regression and this has to be done for many lags of the data. And in each lag I have to add a chunck of code in the nlcom estimation.
In the next code I will present the code without using a loop for the first and second Lags(this code works well and get the estimator I want):
yvar: Outcome variable (continous variable)
Copay: independant variable (0/1)
data: independant variable (0/1/2) indicating where the data comes from as I am stacking data (lag0, lag 1 and lag 2 regressions)
************************************************** ************************************************** **********
*** 6 step: running the integrated regression
regress yvar i.Copay_lag#i.data_index, cluster(PersonaBasicaID)
************************************************** ************************************************** **********
*** 7 step: calculating coheficients needed
** Creating a matrix for collecting the results of TOTs(coheficients I am interestd in)
matrix TOTsI=J(10,3,-99)
matrix colnames TOTsI = TOT_I se Lag
matrix list TOTsI
** TOT lag0
local tot0 _b[1.Copay_lag#1.data_index]
nlcom `tot0'
matrix TOTsI[1,1] = r(b)[1,1]
matrix TOTsI[1,2] = r(V)[1,1]
matrix TOTsI[1,3] = 0
matrix list TOTsI
** TOT lag1
local itt1 _b[1.Copay_lag#2.data_index]-_b[0.Copay_lag#2.data_index]
local pi1 _b[1.Copay_lag#12.data_index]-_b[0.Copay_lag#12.data_index]
nlcom `itt1' - ((`pi1')*(`tot0')) // -------------------------------------- lines that I haven´t been able to change in the loop formulated.
matrix TOTsI[2,1] = r(b)[1,1]
matrix TOTsI[2,2] = r(V)[1,1]
matrix TOTsI[2,3] = 1
matrix list TOTsI
local tot1 (`itt1') - ((`pi1')*(`tot0')) // -------------------------------------- lines that I haven´t been able to change in the loop formulated.
** TOT lag2
local itt2 _b[1.Copay_lag#3.data_index]-_b[0.Copay_lag#3.data_index]
local pi2 _b[1.Copay_lag#13.data_index]-_b[0.Copay_lag#13.data_index]
nlcom `itt2' - ( ((`pi1')*(`tot1')) + ((`pi2')*(`tot0')) ) // -------------------------------------- lines that I haven´t been able to change in the loop formulated.
matrix TOTsI[3,1] = r(b)[1,1]
matrix TOTsI[3,2] = r(V)[1,1]
matrix TOTsI[3,3] = 2
matrix list TOTsI
local tot2 (`itt2') - ( ((`pi1')*(`tot1')) + ((`pi2')*(`tot0')) ) // -------------------------------------- lines that I haven´t been able to change in the loop formulated.
After thinking throug the problem I came with the next code to loop over the lags but I haven´t been able to solve the problem with the lines indicated the previous code and in the next with "// -------------------------------------- lines that I haven´t been able to change in the loop formulated."
************************************************** ************************************************** **********
*** 6 step: running the integrated regression
regress yvar i.Copay_lag#i.data_index, cluster(PersonaBasicaID)
************************************************** ************************************************** **********
*** 7 step: calculating coheficients needed
** Creating a matrix for collecting the results of TOTs(coheficients I am interestd in)
matrix TOTsI=J(10,3,-99)
matrix colnames TOTsI = TOT_I se Lag
matrix list TOTsI
** TOT lag0
local tot0 _b[1.Copay_lag#1.data_index]
nlcom `tot0'
matrix TOTsI[1,1] = r(b)[1,1]
matrix TOTsI[1,2] = r(V)[1,1]
matrix TOTsI[1,3] = 0
matrix list TOTsI
foreach lag of numlist 1 2 {
di "----------------------- Lag " `lag'
** Creating locals requiered
local one `lag'
local two = 1 + `one'
local three = 10 + `two'
local tot_term1_lag = `lag' - 1
local tot_term2_lag = `lag' - 2
** TOT lag
local itt`lag' _b[1.Copay_lag#`two'.data_index]-_b[0.Copay_lag#`two'.data_index]
local pi`lag' _b[1.Copay_lag#`three'.data_index]-_b[0.Copay_lag#`three'.data_index]
nlcom (`itt`lag'') - (((`pi`lag'')*(`tot`tot_term1_lag''))) // -------------------------------------- lines that I haven´t been able to change in the loop formulated because I need to add the terms "+ ((`pi2')*(`tot0')) " for the loop in teh lag 2 as shown in the first code presented.
di "-----------------------------tot_term1_lag " `tot`tot_term1_lag''
matrix TOTsI[(`lag'+1),1] = r(b)[1,1]
matrix TOTsI[(`lag'+1),2] = r(V)[1,1]
matrix TOTsI[(`lag'+1),3] = `lag'
matrix list TOTsI
local tot`lag' (`itt1') - ((`pi1')*(`tot0')) // -------------------------------------- lines that I haven´t been able to change in the loop formulated because I need to add the terms "+ ((`pi2')*(`tot0')) " for the loop in teh lag 2 as shown in the first code presented.
macro list
}
THANK YOU VERY MUCH IF SOME ONE TAKE SOME TIME TO HELP ME WITH THIS PROBLEM.
I am having a problem when building a loop that allwos me to add chunks of code to the next loop. Unfortunally I cant share the data because it is protected by national law. But I hope you understand what I am trying to achieve:
Here is the idea. I need to perform some operations (using the command nlcom) after running a regression and this has to be done for many lags of the data. And in each lag I have to add a chunck of code in the nlcom estimation.
In the next code I will present the code without using a loop for the first and second Lags(this code works well and get the estimator I want):
yvar: Outcome variable (continous variable)
Copay: independant variable (0/1)
data: independant variable (0/1/2) indicating where the data comes from as I am stacking data (lag0, lag 1 and lag 2 regressions)
************************************************** ************************************************** **********
*** 6 step: running the integrated regression
regress yvar i.Copay_lag#i.data_index, cluster(PersonaBasicaID)
************************************************** ************************************************** **********
*** 7 step: calculating coheficients needed
** Creating a matrix for collecting the results of TOTs(coheficients I am interestd in)
matrix TOTsI=J(10,3,-99)
matrix colnames TOTsI = TOT_I se Lag
matrix list TOTsI
** TOT lag0
local tot0 _b[1.Copay_lag#1.data_index]
nlcom `tot0'
matrix TOTsI[1,1] = r(b)[1,1]
matrix TOTsI[1,2] = r(V)[1,1]
matrix TOTsI[1,3] = 0
matrix list TOTsI
** TOT lag1
local itt1 _b[1.Copay_lag#2.data_index]-_b[0.Copay_lag#2.data_index]
local pi1 _b[1.Copay_lag#12.data_index]-_b[0.Copay_lag#12.data_index]
nlcom `itt1' - ((`pi1')*(`tot0')) // -------------------------------------- lines that I haven´t been able to change in the loop formulated.
matrix TOTsI[2,1] = r(b)[1,1]
matrix TOTsI[2,2] = r(V)[1,1]
matrix TOTsI[2,3] = 1
matrix list TOTsI
local tot1 (`itt1') - ((`pi1')*(`tot0')) // -------------------------------------- lines that I haven´t been able to change in the loop formulated.
** TOT lag2
local itt2 _b[1.Copay_lag#3.data_index]-_b[0.Copay_lag#3.data_index]
local pi2 _b[1.Copay_lag#13.data_index]-_b[0.Copay_lag#13.data_index]
nlcom `itt2' - ( ((`pi1')*(`tot1')) + ((`pi2')*(`tot0')) ) // -------------------------------------- lines that I haven´t been able to change in the loop formulated.
matrix TOTsI[3,1] = r(b)[1,1]
matrix TOTsI[3,2] = r(V)[1,1]
matrix TOTsI[3,3] = 2
matrix list TOTsI
local tot2 (`itt2') - ( ((`pi1')*(`tot1')) + ((`pi2')*(`tot0')) ) // -------------------------------------- lines that I haven´t been able to change in the loop formulated.
After thinking throug the problem I came with the next code to loop over the lags but I haven´t been able to solve the problem with the lines indicated the previous code and in the next with "// -------------------------------------- lines that I haven´t been able to change in the loop formulated."
************************************************** ************************************************** **********
*** 6 step: running the integrated regression
regress yvar i.Copay_lag#i.data_index, cluster(PersonaBasicaID)
************************************************** ************************************************** **********
*** 7 step: calculating coheficients needed
** Creating a matrix for collecting the results of TOTs(coheficients I am interestd in)
matrix TOTsI=J(10,3,-99)
matrix colnames TOTsI = TOT_I se Lag
matrix list TOTsI
** TOT lag0
local tot0 _b[1.Copay_lag#1.data_index]
nlcom `tot0'
matrix TOTsI[1,1] = r(b)[1,1]
matrix TOTsI[1,2] = r(V)[1,1]
matrix TOTsI[1,3] = 0
matrix list TOTsI
foreach lag of numlist 1 2 {
di "----------------------- Lag " `lag'
** Creating locals requiered
local one `lag'
local two = 1 + `one'
local three = 10 + `two'
local tot_term1_lag = `lag' - 1
local tot_term2_lag = `lag' - 2
** TOT lag
local itt`lag' _b[1.Copay_lag#`two'.data_index]-_b[0.Copay_lag#`two'.data_index]
local pi`lag' _b[1.Copay_lag#`three'.data_index]-_b[0.Copay_lag#`three'.data_index]
nlcom (`itt`lag'') - (((`pi`lag'')*(`tot`tot_term1_lag''))) // -------------------------------------- lines that I haven´t been able to change in the loop formulated because I need to add the terms "+ ((`pi2')*(`tot0')) " for the loop in teh lag 2 as shown in the first code presented.
di "-----------------------------tot_term1_lag " `tot`tot_term1_lag''
matrix TOTsI[(`lag'+1),1] = r(b)[1,1]
matrix TOTsI[(`lag'+1),2] = r(V)[1,1]
matrix TOTsI[(`lag'+1),3] = `lag'
matrix list TOTsI
local tot`lag' (`itt1') - ((`pi1')*(`tot0')) // -------------------------------------- lines that I haven´t been able to change in the loop formulated because I need to add the terms "+ ((`pi2')*(`tot0')) " for the loop in teh lag 2 as shown in the first code presented.
macro list
}
THANK YOU VERY MUCH IF SOME ONE TAKE SOME TIME TO HELP ME WITH THIS PROBLEM.
Comment