regression - Stata: why is my matrix not clearing over the foreach loop -


when run following code, 2 output matrices (diffindiffone & diffindifftwo) same. guess coeffs not being replaced after each loop have no idea why . think coefficients matrix being overwritten have no idea how. tried changing loop order surprisingly didn't solve issue either:

local treatments treat_one treat_two matrix diffindiffone = j(1,9,.) matrix diffindifftwo = j(1,9,.)  foreach treatment in `treatments' {     reg science inschool#`treatment'#male     matrix coeffs=e(b)      if treat_one==`treatment'{        matrix diffindiffone = diffindiffone\coeffs     }     if treat_two==`treatment'{        matrix diffindifftwo = diffindifftwo\coeffs     }  }   matrix list diffindiffone  matrix list diffindifftwo 

when list matrix both same, depsite fact 2 regressions give different answers. issue appreciated. thanks

this code appears @ first sight reduce

reg science inschool#treat_one#male matrix li e(b)  reg science inschool#treat_two#male  matrix li e(b)  

apart detail of adding 9 missing values matrix.

however, not code, biting you? guess @ more subtle.

you should need careful if command. variables evaluated in if commands evaluated in first observation. so, first time round loop conditions are

if treat_one[1] == treat_one[1]  if treat_two[1] == treat_one[1]  

the second time,

if treat_one[1] == treat_two[1]  if treat_two[1] == treat_two[1]  

if true in data treat_one[1] == treat_two[1] effect not may imagine.

if want test equality of strings,

 if "`treatment'" == "treat_one"  

you may have in mind more

foreach treatment in treat_one treat_two {     reg science inschool#`treatment'#male     matrix `treatment' = e(b)      matrix list `treatment` } 

you seem wanting write complicated code rather simple problems. while back, recommended thinking in terms of do-files rather programs. may advice reconsider.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -