Announcement

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

  • Help debugging bootstrap program

    Dear All,
    I am trying to estimate the 95% CI of the cumulative incidence function after stccreg. Here is the dataex:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(COVID deaths_rate cum_num_vacpct tavg lnprcp grpatidtreat monthlydate) str24 state byte(Female age80plus) float(CumMonthsSAH lnnursing_visits) byte(_st _d _t _t0) float(stop start)
    0 0 0 26.89387 4.625463 4436744 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4315043 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2937359 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2615431 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3845592 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2716008 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3933713 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3630074 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3513849 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3941552 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2666122 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4438548 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2478047 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2406805 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4907557 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2582299 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3351107 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2368421 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2598118 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2733528 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4512910 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3970394 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4124404 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2596517 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4369274 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4881949 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4340860 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4796518 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4737993 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4852387 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2359942 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2939381 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2713157 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2931233 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3441568 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4597357 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2627631 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3030906 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3060035 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4841560 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2432266 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4855171 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4702746 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2520379 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2700808 714 "AL" 0 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2705306 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2575344 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4659349 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3230359 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4636934 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3102996 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3186045 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3687858 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2947824 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3135530 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3241614 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4344014 714 "AL" 0 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4350716 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2464095 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3238279 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2609232 714 "AL" 0 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3141305 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4622302 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3161244 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2599746 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2488380 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2742700 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4604622 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2738875 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3601099 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4153228 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3102923 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3225875 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2616427 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3592304 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4336762 714 "AL" 0 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3801665 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3300843 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3792777 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3011217 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4063063 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4623914 714 "AL" 0 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3714557 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3130700 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3092223 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3510609 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3808970 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2672092 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4364307 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3788605 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3097706 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2492527 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 3844655 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2461308 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4320074 714 "AL" 0 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2959691 714 "AL" 0 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4509034 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4696082 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 2473039 714 "AL" 1 1 0 11.36908 1 0 1 0 1 0
    0 0 0 26.89387 4.625463 4136783 714 "AL" 1 0 0 11.36908 1 0 1 0 1 0
    end
    format %tm monthlydate
    label values COVID COVID
    label def COVID 0 "No confirmed COVID-19", modify

    Here is my code, with the error it produces:

    Code:
    . program define cif_boot_covid, rclass
      1.     version 17.0
      2. 
    .         * linear first stage
    .         reghdfe COVID deaths_rate cum_num_vacpct tavg lnprcp, absorb(grpatid monthlydate state, save) cluster(grpatid) residuals(r
    > esid) 
      3.         predict double COVID_fe, r
      4. 
    .     * 2nd stage stcrreg with fitted value control
    .     stcrreg Female age80plus CumMonthsSAH lnnursing_visits COVID COVID_fe, compete(d2=2)
      5. 
    .         * VERSION 1
    .     * Estimate CIF at COVID=1 and COVID=0 using stcurve and save
    .         tempfile cifout
      6.     stcurve, cif at(COVID=(0 1)) outfile(`cifout', replace)
      7.         drop resid COVID_fe
      8. 
    .         preserve
      9. 
    .     * Load saved CIF data
    .     use `cifout', clear
     10. 
    .     * Choose a specific time point (e.g., t = 30)
    .     keep if _t==30
     11.     gen cif_diff = ci2 - ci1
     12.         return scalar ci1 = ci1
     13.         return scalar ci2 = ci2
     14.     return scalar diff30 = cif_diff[1]
     15.     restore
     16.         
    . end
    
    . 
    . bootstrap r(diff30) r(ci1) r(ci2), reps(10) seed(12345): cif_boot_covid
    (running cif_boot_covid on estimation sample)
    
    Bootstrap replications (10): 
    type mismatch:  exp.exp:  transmorphic found where struct expected
    r(3000);
    
    end of do-file
    
    r(3000);
    Thank you in advance for your help.
    Sincerely,
    Sumedha

  • #2
    The error you're encountering, `r(3000): type mismatch: exp.exp: transmorphic found where struct expected`, typically arises during `bootstrap` when the program being bootstrapped returns nothing, or attempts to return a value from an invalid scope.

    You're using `return scalar` in a program that includes:

    Code:
    preserve
    use `cifout', clear
    ...
    restore
    But you're trying to `return` values after you've already loaded a new dataset. Once you `use` the temporary file (`cifout`), the return values are no longer in the memory from the original estimation step, and may not be evaluated in the expected context by `bootstrap`.

    Also: `ci1`, `ci2`, and `cif_diff` are variables — so `return scalar ci1 = ci1` tries to assign a variable (not a scalar) to a scalar return, outside of a `scalar` context, which causes issues.



    You should capture the `ci1` and `ci2` values explicitly as scalars before trying to return them. Here's how to revise the program:

    Code:
    program define cif_boot_covid, rclass
    version 17.0
    
    // First stage regression with residuals
    reghdfe COVID deaths_rate cum_num_vacpct tavg lnprcp, absorb(grpatid monthlydate state) ///
    cluster(grpatid) residuals(resid)
    predict double COVID_fe, r
    
    // Competing risks regression
    stcrreg Female age80plus CumMonthsSAH lnnursing_visits COVID COVID_fe, compete(d2=2)
    
    // Estimate CIF at COVID=0 and COVID=1
    tempfile cifout
    stcurve, cif at(COVID=(0 1)) outfile(`cifout', replace)
    
    // Load the CIF results
    preserve
    use `cifout', clear
    
    // Focus on specific time point (e.g., t = 30)
    keep if _t == 30
    
    // Store values into scalars first
    scalar s_ci1 = ci1
    scalar s_ci2 = ci2
    scalar s_diff = ci2 - ci1
    
    // Return them
    return scalar ci1 = s_ci1
    return scalar ci2 = s_ci2
    return scalar diff30 = s_diff
    
    restore

    Then call:

    Code:
    bootstrap r(diff30) r(ci1) r(ci2), reps(1000) seed(12345): cif_boot_covid
    Last edited by Josh Zweig; 08 May 2025, 10:57.

    Comment


    • #3
      Thank you for your input Josh. I tried your version, and resolved a couple of more issues, but now seem to be running into another problem of "repeated time values within panel
      the most likely cause for this error is misspecifying the cluster(), idcluster(), or group() option" . Any thoughts?

      Thanks so much!

      Code:
      . program define cif_boot_covid, rclass
        1. version 17.0
        2. 
      . // First stage regression with residuals
      . reghdfe COVID deaths_rate cum_num_vacpct tavg lnprcp, absorb(grpatid monthlydate state) ///
      > cluster(grpatid) residuals(resid)
        3. predict double COVID_fe, r
        4. 
      . // Competing risks regression
      . stcrreg Female age80plus CumMonthsSAH lnnursing_visits COVID COVID_fe, compete(d2=2)
        5. 
      . // Estimate CIF at COVID=0 and COVID=1
      . tempfile cifout
        6. stcurve, cif at(COVID=(0 1)) outfile(`cifout', replace)
        7. drop resid COVID_fe
        8. 
      . // Load the CIF results
      . preserve
        9. use `cifout', clear
       10. 
      . // Focus on specific time point (e.g., t = 30)
      . keep if _t == 30
       11. 
      . // Store values into scalars first
      . scalar s_ci1 = ci1
       12. scalar s_ci2 = ci2
       13. scalar s_diff = ci2 - ci1
       14. 
      . // Return them
      . return scalar ci1 = s_ci1
       15. return scalar ci2 = s_ci2
       16. return scalar diff30 = s_diff
       17. 
      . restore
       18. end
      
      . 
      . bootstrap r(diff30) r(ci1) r(ci2), cluster(grpatid)  idcluster(newid)  reps(1000) seed(12345) nodots: cif_boot_covid
      repeated time values within panel
      the most likely cause for this error is misspecifying the cluster(), idcluster(), or group() option
      r(451);
      
      end of do-file
      
      r(451);

      Comment


      • #4
        Try dropping idcluster(newid) from your bootstrap command:

        <CODE>
        bootstrap r(diff30) r(ci1) r(ci2), cluster(grpatid) reps(1000) seed(12345) nodots: cif_boot_covid
        </CODE>
        This avoids creating fake panel IDs that can conflict with monthlydate, which seems to be your time variable in reghdfe.

        Comment

        Working...
        X