Announcement

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

  • Calling scalars from two different datasets in succession

    I have two cross-sectional datasets (year1 and Year 20) that I have pooled. My aim is to compute concentration indices and contributions for each of the dataset and then calculate the difference between the concentration indices and contributions calculated for the two periods.

    I used the if command to keep data for year 1 and calculated quantities and stored them in sclars. Thereafter, I executed the clear command, drop my program and then called the pooled data and kept the Year 20 using the if command. I realised that at the end of calculating my quantities for year 20 it seems the scalars for year 1 are not stored. I have reproduced a section of my program here for help:

    clear
    set memory 300m

    /* Computations of Quantities for 2003 */

    use "/Users/gordonabekah-nkrumah/Documents/Document General/Main Documents/Latest Buckup 29 JAn/Academic Documents/WIP and Project papers/Institutional Projects/University of Ghana/Inequality In Child Nutrition in Ghana/Data/Ghana_Pro_03-2014.DTA"

    keep if year==2003

    /* Computation of Concentration Index of Y */

    gen weight12=sample_weight
    replace weight12=weight12*10^6
    replace weight12= int(sample_weight)


    tempvar rank wi sumwi wj rk
    gen n = _N
    egen rank = rank(wealth_transfmed), unique
    sort rank
    gen wi = 1/n
    gen sumwi = sum(wi)
    gen wj = sumwi[_n-1]
    replace wj = 0 if wj ==.
    gen rk = wj + 0.5*wi

    qui sum neghaz [fw=weight12]
    scalar mean1= r(mean)
    qui cor rk neghaz [fw=weight12], c
    sca var1=r(cov_12)
    sca ciy01= (2/mean1)*var1
    sca list ciy01


    /* Decomposition of Concentration Index */

    capture program drop myboot4, rclass
    program define myboot4

    qui xi:reg haz_who i.chld_age_mth1 i.sex_child i.size_birth i.educat_level i.ptner_educ_level i.gender_hh numb_u5_child numb_elig_women women_height wealth_transfmed i.eco_zone1 i.rural_resid netwk_gwater2 netwk_gtoilet2 netwk_vaccp2 netwk_assist_12p2 i.year, vce(cluster hh_numb)

    /* mean values */

    qui sum neghaz [fw=weight12]
    scalar my = r(mean)
    qui sum _Ichld_age__2
    scalar m01=r(mean)
    qui sum _Ichld_age__3
    scalar m02=r(mean)
    qui sum _Ichld_age__4
    scalar m03=r(mean)

    /* Regression Coefficients */

    sca b01 = _b[_Ichld_age__2]
    sca b02 = _b[_Ichld_age__3]
    sca b03 = _b[_Ichld_age__4]

    /* Computations of Elasticities */

    sca e01 =(b01*m01)/my
    sca e02 =(b02*m02)/my
    sca e03 =(b03*m03)/my

    /* concentration indices */
    corr rk neghaz , c
    sca ciy = 2*r(cov_12)/my
    corr rk _Ichld_age__2 , c
    sca ci01 = 2*r(cov_12)/m01
    corr rk _Ichld_age__3 , c
    sca ci02 = 2*r(cov_12)/m02

    /* contribution from variables in numbers */

    sca c01 = b01*m01*ci01/(my*ciy)
    sca c02 = b02*m02*ci02/(my*ciy)
    sca c03 = b03*m03*ci03/(my*ciy)

    /* contributions from variables in percentage of predicted CI */

    sca cp01 = 100*b01*m01*ci01/(my*ciy)
    sca cp02 = 100*b02*m02*ci02/(my*ciy)
    sca cp03 = 100*b03*m03*ci03/(my*ciy)

    end

    bootstrap ciy ci01 ci02 ci03

    clear

    pr drop myboot4


    use "/Users/gordonabekah-nkrumah/Documents/Document General/Main Documents/Latest Buckup 29 JAn/Academic Documents/WIP and Project papers/Institutional Projects/University of Ghana/Inequality In Child Nutrition in Ghana/Data/Ghana_Pro_03-2014.DTA"

    keep if year==2014

    gen weight12=sample_weight
    replace weight12=weight12*10^6
    replace weight12= int(sample_weight)


    tempvar rank wi sumwi wj rk
    gen n = _N
    egen rank = rank(wealth_transfmed), unique
    sort rank
    gen wi = 1/n
    gen sumwi = sum(wi)
    gen wj = sumwi[_n-1]
    replace wj = 0 if wj ==.
    gen rk = wj + 0.5*wi

    qui sum neghaz [fw=weight12]
    scalar mean1= r(mean)
    qui cor rk neghaz [fw=weight12], c
    sca var1=r(cov_12)
    sca ciy01_1= (2/mean1)*var1
    sca list ciy01_1


    capture program drop myboot4, rclass
    program define myboot4

    qui xi:reg haz_who i.chld_age_mth1 i.sex_child i.size_birth i.educat_level i.ptner_educ_level i.gender_hh numb_u5_child numb_elig_women women_height wealth_transfmed i.eco_zone1 i.rural_resid netwk_gwater2 netwk_gtoilet2 netwk_vaccp2 netwk_assist_12p2 i.year, vce(cluster hh_numb)

    /* mean values */

    qui sum neghaz [fw=weight12]
    scalar my_1 = r(mean)
    qui sum _Ichld_age__2
    scalar m01_1=r(mean)
    qui sum _Ichld_age__3
    scalar m02_1=r(mean)

    /* Regression Coefficients */

    sca b01_1 = _b[_Ichld_age__2]
    sca b02_1 = _b[_Ichld_age__3]
    sca b03_1 = _b[_Ichld_age__4]

    /* Computations of Elasticities */

    sca e01_1 =(b01_1*m01_1)/my_1
    sca e02_1 =(b02_1*m02_1)/my_1
    sca e03_1 =(b03_1*m03_1)/my_1

    /* concentration indices */
    corr rk neghaz , c
    sca ciy_1 = 2*r(cov_12)/my_1
    corr rk _Ichld_age__2 , c
    sca ci01_1 = 2*r(cov_12)/m01_1
    corr rk _Ichld_age__3 , c
    sca ci02_1 = 2*r(cov_12)/m02_1

    /* Contributions from Variables in Numbers */

    sca c01_1 = b01_1*m01_1*ci01_1/(my_1*ciy_1)
    sca c02_1 = b02_1*m02_1*ci02_1/(my_1*ciy_1)
    sca c03_1 = b03_1*m03_1*ci03_1/(my_1*ciy_1)

    /* contributions from variables in percentage of predicted CI */

    sca cp01_1 = 100*b01_1*m01_1*ci01_1/(my_1*ciy_1)
    sca cp02_1 = 100*b02_1*m02_1*ci02_1/(my_1*ciy_1)
    sca cp03_1 = 100*b03_1*m03_1*ci03_1/(my_1*ciy_1)

    end

    bootstrap ciy_1 ci01_1 ci02_1 ci03_1

    * Decomposition of Change in Inequality - Oaxaca */

    sca dc01 = e01_1*(ci01_1-ci01)+ci01*(e01_1-e01)
    sca dc02 = e02_1*(ci02_1-ci02)+ci02*(e02_1-e02)
    sca dc03 = e03_1*(ci03_1-ci03)+ci03*(e03_1-e03)

  • #2
    I don't know what the cause of the problem is, but one solution is to pass the scalars as arguments to the next program.
    Last edited by Kris Bitney; 28 Mar 2017, 18:09.

    Comment


    • #3
      First a side issue: Stata does have an -if- command. But -keep if year == 2014- isn't an example of it. That's a use of the -if- qualifier or condition. The -if- command does something very different. You might want to read up on that: -help ifcmd- and -help if-. In any case, your use of the -if- qualifier is correct.

      The problem is not that your scalars from the first set of calculations aren't being stored. The problem is that you are overwriting them in the second round of calculations. You first define scalar mean1 in the code for the calculations on the year 2003 data. Then you later have another -scalar mean1 = ...- command in the 2014 calculations. Well, you have redefined scalar mean1 and, of course, its previous value is lost. This appears to apply to all of your scalars.

      Another aside. In the code:
      Code:
      replace weight12=weight12*10^6
      replace weight12= int(sample_weight)
      the first statement accomplishes nothing, because its results are immediately overwritten by the second one. So you may as well omit it.

      I also don't understand why you define the program myboot4 twice. As far as I can see (I haven't scrutinized every line, so maybe I missed something), it is the exact same program defined, then dropped and redefined. There's no need to do that. Doing it once at the top of the code would suffice. Also, though it harms nothing, there is no reason to declare it as -rclass- as it doesn't actually return anything in r().

      Finally, your -bootstrap- command is syntactically wrong and makes no sense: it doesn't specify any program to bootstrap.
      Last edited by Clyde Schechter; 28 Mar 2017, 18:48.

      Comment


      • #4
        Dear Kris and Clyde,

        Thank you very much for your response. I am very grateful. I will make the necessary amendments and go through again. However, In the case of the scalars, i tried to differentiate them with an underscore. as reproduced below:

        Code:
        Year 1:
        qui sum _Ichld_age__2
        scalar m01=r(mean)

        Year 20
        qui sum _Ichld_age__2
        scalar m01_1=r(mean)

        Thank you very much

        Comment


        • #5
          I also want to find out whether by clearing the Year1 data from memory and loading the Year20 data automatically clears all the scalars from Year1 stored in memory?

          Comment


          • #6
            Re #4: Sorry, I hadn't noticed that. The names looked the same to me. But you're right. So, the scalars are not being overwritten.

            Re #5: No, clearing the data does not clear the scalars:

            Code:
            . clear*
            
            . scalar dir
            
            . sysuse auto
            (1978 Automobile Data)
            
            . summ price
            
                Variable |        Obs        Mean    Std. Dev.       Min        Max
            -------------+---------------------------------------------------------
                   price |         74    6165.257    2949.496       3291      15906
            
            . scalar mean_price = r(mean)
            
            . scalar dir
            mean_price =  6165.2568
            
            . webuse nlswork, clear
            (National Longitudinal Survey.  Young Women 14-26 years of age in 1968)
            
            . scalar dir
            mean_price =  6165.2568
            Re-reading #1, I wonder how you have concluded that your scalars from year 1 are not stored. Did you run -scalar dir- and find they were not there?

            Comment

            Working...
            X