Dear Statalist members,
I would greatly appreciate your assistance with the following matter.
While running some code, I am using the swindex command iteratively within a loop. I have noticed that when summarizing the distribution of the output variable, the mean (and only the mean, not any of the other moments) seems to change with each run. I have attached a script below that should replicate this behavior.
Please note that I also compute a swindex outside the loop (swi_pooled), which maintains the same mean at every run. However, if I reorganize the code to produce the swindex outside the loop after running the loop, even the mean from this swindex calculation appears to change.
I am using swindex from SSC in Stata 16.1. For more infor on swindex, see Schwab et al. (2021).
Thank you in advance for any insights or suggestions you may have.
Benjamin Schwab & Sarah Janzen & Nicholas P. Magnan & William M. Thompson, 2021. "SWINDEX: Stata module to create a standardized weighted index of multiple indicator variables," Statistical Software Components S458912, Boston College Department of Economics. Available at: https://ideas.repec.org/c/boc/bocode/s458912.html.
clear all
* Create dataset
set obs 300
set seed 12345
gen v1=runiform(0,1)
gen v2=runiform(0,1)
gen v3=runiform(0,1)
gen v4=runiform(0,1)
gen v5=runiform(0,1)
* Pooled swindex
swindex v1 v2 v3 v4 v5, gen(swi_pooled)
* Generate classes
gen class=1
replace class=2 if _n>134
replace class=3 if _n>228
* Swindex by classes
gen swi_by=.
forval n=1/3 {
swindex v1 v2 v3 v4 v5 if class==`n', gen(swi_`n')
replace swi_by=swi_`n' if class==`n'
drop swi_`n'
}
* Distribution
sum swi_by swi_pooled, d
I would greatly appreciate your assistance with the following matter.
While running some code, I am using the swindex command iteratively within a loop. I have noticed that when summarizing the distribution of the output variable, the mean (and only the mean, not any of the other moments) seems to change with each run. I have attached a script below that should replicate this behavior.
Please note that I also compute a swindex outside the loop (swi_pooled), which maintains the same mean at every run. However, if I reorganize the code to produce the swindex outside the loop after running the loop, even the mean from this swindex calculation appears to change.
I am using swindex from SSC in Stata 16.1. For more infor on swindex, see Schwab et al. (2021).
Thank you in advance for any insights or suggestions you may have.
Benjamin Schwab & Sarah Janzen & Nicholas P. Magnan & William M. Thompson, 2021. "SWINDEX: Stata module to create a standardized weighted index of multiple indicator variables," Statistical Software Components S458912, Boston College Department of Economics. Available at: https://ideas.repec.org/c/boc/bocode/s458912.html.
clear all
* Create dataset
set obs 300
set seed 12345
gen v1=runiform(0,1)
gen v2=runiform(0,1)
gen v3=runiform(0,1)
gen v4=runiform(0,1)
gen v5=runiform(0,1)
* Pooled swindex
swindex v1 v2 v3 v4 v5, gen(swi_pooled)
* Generate classes
gen class=1
replace class=2 if _n>134
replace class=3 if _n>228
* Swindex by classes
gen swi_by=.
forval n=1/3 {
swindex v1 v2 v3 v4 v5 if class==`n', gen(swi_`n')
replace swi_by=swi_`n' if class==`n'
drop swi_`n'
}
* Distribution
sum swi_by swi_pooled, d