Announcement

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

  • Storing available values only and skipping missing ones

    Hi. My code below fails because the factor variable - treatedt - does not have continuous values (displayed below). The error I get is "[1.treatedt] not found". How can I amend this code to store for all values of treatedt as available and skip where not available? Thanks!



    Code:
        local N = `span' * 2 + 1
        forvalues i = 1/`N' {
            local b_`i'_1 = _b[`i'.treatedt]
            local se_`i'_1 = _se[`i'.treatedt]
            }
        
    
    
    treatedt                                                                                                                               (unlabeled)
    --------------------------------------------------------------------------------------------------------------------------------------------------
    
                      Type: Numeric (float)
    
                     Range: [0,7]                         Units: 1
             Unique values: 5                         Missing .: 0/1,615
    
                Tabulation: Freq.  Value
                            1,563  0
                               13  4
                               13  5
                               13  6
                               13  7
    
    .

  • #2
    The problem here is not that a variable is not continuous; a factor variable must be discrete! It's that your factor variable has levels 0, 4, 5, 6, 7 so that no observation has level 1.

    Perhaps you should recode your treatment variable to have levels that are an integer sequence.

    An alternative is just to loop over the levels that exist in the data. Here is some flavour and you can add code for standard errors.

    Code:
    levelsof treatedt, local(levels)
    
    foreach i of local levels {
        local b_`i'_1 = _b[`i',treatedt]
    }
    Last edited by Nick Cox; 04 Aug 2024, 02:06.

    Comment


    • #3
      Should be
      Code:
      _b[`i'.treatedt]

      Comment


      • #4
        Nick Cox Thank you for that. I altered the code below to reflect your suggestion. As seen in the output, I now get the error "r(101) - varlist not allowed" after the regression output. I'm not sure what varlist this is referring to and am completely lost on what to do here.

        I'm providing the entire code set as well as sample data after the merge in the code below. I'd greatly appreciate any help here!



        Code:
        capture program drop mergerPaperFigure
            
        program define mergerPaperFigure
            
         args figure_tag  yVar  span graph
            
            
            
            
            use "${coeffdir}\coefficients_one.dta", clear  
            keep if stepone==1
            
            keep state Treated_State adopt_week cem_varlist
            
            *rename state Treated_State
            *rename weeknum adopt_week
            
            collapse (max) adopt_week cem_varlist, by (state)
            rename state Treated_State
            
            * merge 
            merge 1:m Treated_State adopt_week cem_varlist using "${outdir}\1matched_cohort.dta", keep(match)
            
            order Treated_State adopt_week weeknum state
            sort  Treated_State adopt_week weeknum state
            
            * Now set up the data for the stacked DiD
            gen t0 = adopt_week
            gen t = weeknum - adopt_week
            keep if t >= -`span' & t <= `span'
            replace t = t + `span' + 1    // to avoid negative values
            
            gen treatedt = t * (stepone == 1)
            gen controlt = t * (stepone == 0)
        
            
                *** Graph: Balanced T, with FEs to control for panel and isolate year of acq.
            areg `yVar' ib`span'.t ib5.treatedt, absorb(state) cluster(state)
            
            levelsof treatedt, local(levels)
            *local N = `span' * 2 + 1
            
                foreach i of local levels {
                local b_`i'_1 = _b[`i'.treatedt]
                local se_`i'_1 = _se[`i'.treatedt]
            }
            
            
            clear
            set obs `levels'
            
            gen diff = .
            gen diff_se = .
            
            gen t = _n - `span' - 1
            
            foreach i of local levels {
                replace diff = `b_`i'_1' in `i'
                replace diff_se = `se_`i'_1' in `i'
                }
            
            gen diff_lb = diff - 1.96*diff_se
            gen diff_ub = diff + 1.96*diff_se
            
                
            * save coefficients
            save "${coeffdir}\stacked-DiD\\`figure_tag'.dta", replace
                
            * Graph
            if "`graph'" == "YES" {
            graph set window fontface "${paperFont}"
                twoway    (connect diff t, ///
                            color(gs0) msymbol(d)) ///
                        (rspike diff_lb diff_ub t, ///
                            lcolor(gs0)), ///
                    xtitle("Years from acquisition", height(5)) xlabel(-`span'(1)`span') ///
                    ytitle("Change relative to year prior to acquisition") ///
                    ylabel(#5, format(%4.1f)) xline(0, lcolor(gs10%50) lwidth(*15)) ///
                    legend(off) name(diff, replace)
                graph export "${paperdir}\Figures\\`figure_tag'.pdf", as(pdf) replace
                }    
        end
        
        
        
        
        * Effect of treatment on outcome
        mergerPaperFigure "Figure test"    /// figure tag
                                 "confirmed_1"    /// yVar
                           "4"    /// span
                          "YES"    // graph
        
        
        
        ___________________________________________OUTPUT____________________________
        
        
        Linear regression, absorbing indicators             Number of obs     =  2,043
        Absorbed variable: state                            No. of categories =     31
                                                            F(13, 30)         =   9.50
                                                            Prob > F          = 0.0000
                                                            R-squared         = 0.0732
                                                            Adj R-squared     = 0.0533
                                                            Root MSE          = 0.1650
        
                                         (Std. err. adjusted for 31 clusters in state)
        ------------------------------------------------------------------------------
                     |               Robust
         confirmed_1 | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
        -------------+----------------------------------------------------------------
                   t |
                  1  |  -.0142434    .003264    -4.36   0.000    -.0209094   -.0075773
                  2  |  -.0103918   .0023361    -4.45   0.000    -.0151628   -.0056209
                  3  |  -.0056076   .0012019    -4.67   0.000    -.0080622    -.003153
                  5  |   .0068212   .0013957     4.89   0.000     .0039708    .0096716
                  6  |   .0148284   .0032755     4.53   0.000      .008139    .0215178
                  7  |   .0237305   .0051254     4.63   0.000      .013263     .034198
                  8  |   .0331127   .0068224     4.85   0.000     .0191795     .047046
                  9  |   .0434466   .0087628     4.96   0.000     .0255505    .0613428
                     |
            treatedt |
                  0  |   .0012244   .0068478     0.18   0.859    -.0127606    .0152094
                  6  |  -.0023025    .003365    -0.68   0.499    -.0091748    .0045698
                  7  |  -.0047965   .0068271    -0.70   0.488    -.0187393    .0091463
                  8  |  -.0069097   .0107515    -0.64   0.525    -.0288671    .0150477
                  9  |  -.0097033   .0149252    -0.65   0.521    -.0401847    .0207781
                     |
               _cons |   .0385232   .0078112     4.93   0.000     .0225705    .0544758
        ------------------------------------------------------------------------------
        0 5 6 7 8 9
        varlist not allowed
        r(101);
        
        
        ______________________SAMPLE DATA___________________________
        
        
        input float confirmed_1 str50 Treated_State byte(adopt_week weeknum) str50 state
           .015854968 "Bihar"         22 19 "Bihar"                      
             .7449847 "Chhattisgarh"   6 37 "Chhattisgarh"               
           .016927214 "Jharkhand"     11 20 "Jharkhand"                  
             .2051285 "JnK"           12 23 "JnK"                        
            1.3317966 "Karnataka"     10 38 "Karnataka"                  
             .0796539 "MadhyaPradesh"  8 26 "MadhyaPradesh"              
             1.436547 "Maharashtra"   11 37 "Maharashtra"                
             .9156209 "Manipur"       27 44 "Manipur"                    
            .12196473 "Meghalaya"      9 48 "Meghalaya"                  
                    0 "Nagaland"       7  7 "Nagaland"                   
             .3640134 "Odisha"         4 28 "Odisha"                     
             .1657122 "Punjab"         3 48 "Punjab"                     
         .00040904255 "Uttarakhand"    8  7 "Uttarakhand"                
           .001358888 "Bihar"         22 11 "Bihar"                      
            .21531174 "Bihar"         22 45 "Bihar"                      
            .19399765 "Bihar"         22 38 "Bihar"                      
             .1659875 "Bihar"         22 32 "Bihar"                      
            .21676815 "Bihar"         22 46 "Bihar"                      
             .1729872 "Bihar"         22 33 "Bihar"                      
            .06231139 "Bihar"         22 48 "Bihar"                      
           .003441026 "Bihar"         22 13 "Bihar"                      
            .11582927 "Bihar"         22 26 "Bihar"                      
          .0045832135 "Bihar"         22 14 "Bihar"             
          .000708907 "Bihar"         22 10 "Bihar"                      
             .1976383 "Bihar"         22 39 "Bihar"                      
        .000034781988 "Bihar"         22  5 "Bihar"                      
           .010706636 "Bihar"         22 18 "Bihar"                      
         .00005976286 "Bihar"         22  6 "Bihar"                      
            .15187284 "Bihar"         22 30 "Bihar"                      
        .000017450755 "Bihar"         22  4 "Bihar"                      
            .09181153 "Bihar"         22 24 "Bihar"                      
            1.2190716 "Bihar"         22 41 "Andaman and Nicobar Islands"
          .0003151148 "Bihar"         22  4 "Telangana"                  
            .07164384 "Bihar"         22 25 "Himachal Pradesh"           
          .0000501002 "Bihar"         22  7 "Tripura"                    
           .014525893 "Bihar"         22 14 "Rajasthan"                  
         .00006648936 "Bihar"         22  8 "Arunachal Pradesh"          
             2.868275 "Bihar"         22 35 "Goa"                        
            .27862486 "Bihar"         22 36 "Rajasthan"                  
            .10771196 "Bihar"         22 26 "Rajasthan"                  
            .19224593 "Bihar"         22 33 "Mizoram"                    
            .26631755 "Bihar"         22 47 "Uttar Pradesh"              
          .0026462995 "Bihar"         22 14 "Sikkim"                     
             .8793436 "Bihar"         22 32 "Tamil Nadu"                 
            .12186955 "Bihar"         22 27 "Rajasthan"                  
            .02020734 "Bihar"         22 16 "Rajasthan"                  
           .005407249 "Bihar"         22 10 "Rajasthan"                  
             .4233307 "Bihar"         22 37 "Himachal Pradesh"           
             .1723584 "Bihar"         22 30 "Rajasthan"                  
             .9296741 "Bihar"         22 46 "Haryana"                    
             .9147797 "Bihar"         22 33 "Arunachal Pradesh"          
             .1555756 "Bihar"         22 23 "Arunachal Pradesh"          
           .028770726 "Bihar"         22 15 "Haryana"

        Comment


        • #5
          I wouldn't write a program to do so much ad hoc data management and analysis in which particular files and variables are wired in. That's the role of a do-file.

          I haven't tried to follow your overall logic, if only because no-one else could run your code on your data example without hitting references to files they don't have and to other variables you don't exemplify.

          In debugging a program, it is often crucial to

          Code:
          set trace on
          to see exactly where the program fails.

          Although I can't run your code and don't really understand it, something can be said.

          This looks very puzzling:

          Code:
          levelsof treatedt, local(levels)
          
          ***      
          clear
          set obs `levels'
          levelsof will return a list of distinct values which you put in local macro levels. But then you try to set the number of observations using the same macro. For that you need some appropriate constant, perhaps just the number of distinct levels.

          Your output shows that levelsof runs as you asked and that the problem occurs after that.

          Let's try something similar:

          Code:
          . sysuse auto, clear
          (1978 automobile data)
          
          . levelsof foreign, local(levels)
          0 1
          
          . clear
          
          . set obs `levels'
          varlist not allowed
          r(101);
          There may be other bugs and other problems, but that seems to be the one biting you right now. I guess you need something more like

          Code:
          ]levelsof treatedt, local(levels)
          local nlevels = r(r)
          
          ***      
          clear
          set obs `nlevels'
          I hope that helps. Note that
          Code:
          ***
          is not literal code to be used but just an allusion to other commands that don't appear problematic in between two chunks of code.

          On a different level, there should be better way to get confidence limits than +/- 1.96 SE, which at best is a large-sample approximation.
          Last edited by Nick Cox; 07 Aug 2024, 03:54.

          Comment


          • #6
            Thanks Nick Cox. That worked perfectly. However, I've run into anew problem. I set trace on as you suggested and found that the code fails at the part bolded out below

            Code:
             - foreach i of local levels {
              - replace diff = `b_`i'_1' in `i'
              = replace diff = .0047558455355984 in 0
            '0' invalid observation number
                replace diff_se = `se_`i'_1' in `i'
                }
            My understanding is that the code is failing when replacing for the value i==0, since i is based off the levels of variable treatedt which takes on the values below

            Code:
            tab treatedt
            
               treatedt |      Freq.     Percent        Cum.
            ------------+-----------------------------------
                      0 |      1,563       96.78       96.78
                      4 |         13        0.80       97.59
                      5 |         13        0.80       98.39
                      6 |         13        0.80       99.20
                      7 |         13        0.80      100.00
            ------------+-----------------------------------
                  Total |      1,615      100.00
            I fail to understand why i==0 is being treated as an observation number and the "replace when i==0" is failing. I'd greatly appreciate your guidance on what I'm missing here. I apologise for the unusable code and sample data before. I've cleaned it up and am attaching here a usable sample.

            Code:
            *Code
            
                areg confirmed_1  ib3.t ib5.treatedt, absorb(state) cluster(state)
                
                levelsof treatedt, local(levels)
                local nlevels = r(r)
                
                foreach i of local levels {
                    local b_`i'_1 = _b[`i'.treatedt]
                    local se_`i'_1 = _se[`i'.treatedt]
                }
                
            
                clear
                set obs `nlevels'
                
                gen diff = .
                gen diff_se = .
                
                gen t = _n - 2
                
                foreach i of local levels  {
                    replace diff = `b_`i'_1' in `i'
                    replace diff_se = `se_`i'_1' in `i'
                    }
                
                gen diff_lb = diff - 1.96*diff_se
                gen diff_ub = diff + 1.96*diff_se
                
                    
                * save coefficients
                save "${coeffdir}\stacked-DiD\\`figure_tag'.dta", replace
            
            
            
            *Sample data
            
            clear
            input str50 state float(xt treatedt)
            "Andaman and Nicobar Islands"  .04303706 1 0
            "Andhra Pradesh"               .06419585 1 0
            "Arunachal Pradesh"           .030328646 1 0
            "Assam"                        .05483043 1 0
            "Bihar"                       .015854968 1 0
            "DNHnDD"                       .05648741 1 0
            "Goa"                           .1810761 1 0
            "Gujarat"                     .064387284 1 0
            "Haryana"                      .07896405 1 0
            "Himachal Pradesh"            .017772995 1 0
            "Kerala"                       .02581149 1 0
            "Mizoram"                       .0206256 1 0
            "Rajasthan"                    .03328976 1 0
            "Sikkim"                      .028700516 1 0
            "Tamil Nadu"                   .19475035 1 0
            "Telangana"                    .10167153 1 0
            "Tripura"                       .0553321 1 0
            "Uttar Pradesh"                .01774191 1 0
            "West Bengal"                 .034098875 1 0
            "Andaman and Nicobar Islands"  .05505577 2 0
            "Andhra Pradesh"                .1162458 2 0
            "Arunachal Pradesh"            .05731383 2 0
            "Assam"                        .07719485 2 0
            "Bihar"                       .024219856 2 0
            "DNHnDD"                       .07823626 2 0
            "Goa"                           .2605473 2 0
            "Gujarat"                     .074334666 2 0
            "Haryana"                       .0961351 2 0
            "Himachal Pradesh"             .02306458 2 0
            "Kerala"                       .04048846 2 0
            "Mizoram"                     .025611216 2 0
            "Rajasthan"                    .04057129 2 0
            "Sikkim"                       .05520654 2 0
            "Tamil Nadu"                    .2400485 2 0
            "Telangana"                    .12872688 2 0
            "Tripura"                      .08202834 2 0
            "Uttar Pradesh"               .023824066 2 0
            "West Bengal"                   .0485811 2 0
            "Andaman and Nicobar Islands"  .09913638 3 0
            "Andhra Pradesh"               .21597633 3 0
            "Arunachal Pradesh"             .0886968 3 0
            "Assam"                        .10281774 3 0
            "Bihar"                       .036436457 3 0
            "DNHnDD"                       .10706092 3 0
            "Goa"                           .3437755 3 0
            "Gujarat"                      .08542473 3 0
            "Haryana"                       .1144586 3 0
            "Himachal Pradesh"            .031894322 3 0
            "Kerala"                       .05916177 3 0
            "Mizoram"                      .03209492 3 0
            "Rajasthan"                    .05005602 3 0
            "Sikkim"                       .08655336 3 0
            "Tamil Nadu"                    .2998528 3 0
            "Telangana"                    .15472403 3 0
            "Tripura"                      .10869954 3 0
            "Uttar Pradesh"               .032955963 3 0
            "West Bengal"                  .06516285 3 0
            "Andaman and Nicobar Islands"  .23389708 4 0
            "Andhra Pradesh"                .3397927 4 0
            "Arunachal Pradesh"            .12131459 4 0
            "Assam"                        .14031644 4 0
            "Bihar"                        .05235824 4 4
            "DNHnDD"                       .13853717 4 0
            "Goa"                           .4600928 4 0
            "Gujarat"                      .09675283 4 0
            "Haryana"                      .13211048 4 0
            "Himachal Pradesh"             .03942661 4 0
            "Kerala"                        .0800362 4 0
            "Mizoram"                      .04174257 4 0
            "Rajasthan"                    .06043934 4 0
            "Sikkim"                       .11320999 4 0
            "Tamil Nadu"                    .3545216 4 0
            "Telangana"                    .18704766 4 0
            "Tripura"                       .1388348 4 0
            "Uttar Pradesh"                .04485377 4 0
            "West Bengal"                  .08369362 4 0
            "Andaman and Nicobar Islands"   .4445124 5 0
            "Andhra Pradesh"                .4695284 5 0
            "Arunachal Pradesh"             .1555756 5 0
            "Assam"                        .19079113 5 0
            "Bihar"                        .07272435 5 5
            "DNHnDD"                        .1721436 5 0
            "Goa"                           .6194713 5 0
            "Gujarat"                      .10784288 5 0
            "Haryana"                       .1507638 5 0
            "Himachal Pradesh"             .04876908 5 0
            "Kerala"                        .1053519 5 0
            "Mizoram"                      .05239693 5 0
            "Rajasthan"                   .071108334 5 0
            "Sikkim"                       .13962995 5 0
            "Tamil Nadu"                     .407725 5 0
            "Telangana"                    .22254586 5 0
            "Tripura"                      .15989837 5 0
            "Uttar Pradesh"                .05850908 5 0
            "West Bengal"                  .10467065 5 0
            "Andaman and Nicobar Islands"   .6372796 6 0
            "Andhra Pradesh"               .58839357 6 0
            "Arunachal Pradesh"            .19108093 6 0
            "Assam"                         .2385264 6 0
            "Bihar"                        .09181153 6 6
            end

            Comment


            • #7
              Let's be clear. The code that is failing is not code that I suggested. Your problem is a bug in your code after the bug discussed in my last post.

              You have levels of a particular variable 0 2 4 5 6 7 and your code is asking to put results for each level of those levels in observation numbers 0 2 4 5 6 7. That fails at the first hurdle as

              whatever in 0

              means do whatever in observation 0 and you have no such observation.

              As I warned earlier I don't really understand all of what you're trying to do, but my guess is that you want to put results in observations 1 up. If so, this code is more likely to run. I omit code that's not obviously relevant to this question.

              Code:
              gen diff = .
              gen diff_se = .  
              local j = 1      
              foreach i of local levels  {        
                  replace diff = `b_`i'_1' in `j'        
                  replace diff_se = `se_`i'_1' in `j'        
                  local ++j  
              }
              and indeed it seems that you're likely to want the levels themselves aligned.

              Code:
              gen diff = .
              gen diff_se = .
              gen level = .  
              local j = 1      
              foreach i of local levels  {        
                  replace diff = `b_`i'_1' in `j'        
                  replace diff_se = `se_`i'_1' in `j'        
                  replace level = `i' in `j'          
                  local ++j  
              }
              See also on loops in parallel e.g. https://journals.sagepub.com/doi/pdf...6867X211063415

              I hope that helps.
              Last edited by Nick Cox; 16 Aug 2024, 07:02.

              Comment

              Working...
              X