Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exporting confidence intervals that loop through all vars in an excel format

    For each of my regional data files, I am trying to export confidence interval tables in a csv file. The current output in STATA looks like below, which I would like to replicate in an excel table.

    Variable | Obs | Mean | Std. Err. | [95% Conf. Interval]
    -------------+---------------------------------------------------------------
    accept_vio~n | 330 .3364758 .02605 .2852303 .3877213

    However, when I use the code below, the output does not include the variable, nor does it include the observation number. Any advice would be greatly appreciated, as I have tried a number of different solutions on this forum. Thank you.

    #delimit;
    clear;
    use "Dta Files\2019_Indonesia_Weighted_DM.dta";

    drop if geographies_DM != 1;
    recode city (12=3)(13=2)(14=1), gen (sundacity);
    label define sundacity
    1"West Nusa Tenggara"
    2"East Nusa Tenggara"
    3"Bali";
    label values sundacity sundacity;

    cap postclose ci_results_sunda;

    postfile ci_results_sunda var obs mean se lowerCI upperCI using "2019_ci_results_sunda.dta", replace;

    foreach var of varlist optimist-accept_violence_religion {;
    ci `var'[aweight=weight_pop];
    ci `var' if sundacity==3 [aweight=weight_pop];
    ci `var' if sundacity==2 [aweight=weight_pop];
    ci `var' if sundacity==1 [aweight=weight_pop];
    ci `var' if age_group_DM==4 [aweight=weight_pop];
    ci `var' if age_group_DM==3 [aweight=weight_pop];
    ci `var' if age_group_DM==2 [aweight=weight_pop];
    ci `var' if age_group_DM==1 [aweight=weight_pop];
    ci `var' if religion_all_DM==4 [aweight=weight_pop];
    ci `var' if religion_all_DM==3 [aweight=weight_pop];
    ci `var' if religion_all_DM==2 [aweight=weight_pop];
    ci `var' if religion_all_DM==1 [aweight=weight_pop];
    ci `var' if female_DM==2 [aweight=weight_pop];
    ci `var' if female_DM==1 [aweight=weight_pop];
    ci `var' if urban_DM==2 [aweight=weight_pop];
    ci `var' if urban_DM==1 [aweight=weight_pop];
    post ci_results_sunda (`var') (r(obs)) (r(mean)) (r(se)) (r(lb)) (r(ub));
    };

    postclose ci_results_sunda;
    use ci_results_sunda.dta, clear;

    use "2019_ci_results_sunda.dta";
    outsheet using "ci_results_sunda.csv", comma replace;
    Last edited by Aubrey Hilbert; 08 Jul 2020, 17:57. Reason: confidence intervals, CIs, loops, post

  • #2
    One obvious problem with your code is that you have only one post command in the whole loop but calculate many confidence intervals. So only the last confidence interval would be stored.
    So you have to use the
    post command after every confidence interval.
    Besides that, the
    ci-command is incorrect because it expects a subcommand which you miss. You probably meant ci means.

    Comment


    • #3
      The ci command would be correct in an old version of Stata but you should tell us which version you are using.

      Comment

      Working...
      X