Announcement

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

  • Bootstrap error with custom program using xtdpdbc ("insufficient observations")

    Hello everyone,

    This is my first time posting here, so thank you in advance for your patience and guidance.

    I am trying to bootstrap an indirect effect in a dynamic panel model using `xtdpdbc`. My code is:

    Code:
    ****** VARIABLE SETTING
    global yvar roa
    global xvar i.envresp
    global medvar sales
    global controls lev age size capin loss
    
    global reps = 1000
    
    capture program drop btstp
    program btstp, rclass
        xtdpdbc $medvar $xvar $controls ,vce(robust) teffects
        scalar a= r(table)["b","1.envresp"]
    
        xtdpdbc $yvar $xvar $medvar $controls, vce(robust) teffects eigtolerance(0.1)
        scalar b = r(table)["b","sales"]
    
        return scalar IE = a * b  // Indirect effect
    end
    
    bootstrap Ind_Eff = r(IE), reps($reps) seed(123): btstp
        eststo roa_bootstrap: estat bootstrap, percentile bc
    However, I get the following error:
    x: Error occurred when bootstrap executed btstp.
    insufficient observations to compute bootstrap standard errors; no results will be saved

    Context:
    • Panel data is set with xtset id year, yearly.
    • Using Stata 18.5
    • The command xtdpdbc runs fine outside the bootstrap.
    • The sample size is around 579 panels and 2316 firm-year observations.
    • Any advice or alternative approaches would be greatly appreciated.
    Here is a sample of my data:

    Code:
    clear
    input long id int year double roa byte envresp float sales double lev float(age size) double capin byte loss
    1 2019   2.417122729624633 0   26.4506 20.262438608629477  3.465736 26.654966 1.2267458411349768 0
    1 2020    7.08437667668948 0 26.312157 18.299980787011723  3.496508  26.72797 1.5155983604464973 0
    1 2021    4.70437512775878 0  26.32985 17.298522588100703 3.5263605 26.754595 1.5291980159315426 0
    1 2022   5.022683132841968 0  26.47878 16.303177943459072  3.555348  26.78676   1.36067121155973 0
    2 2019  -28.43574482175618 0  24.34402  67.81047317679503 3.9318256  25.69538 3.8626545649881887 1
    2 2020 -12.584930386649724 0  23.72984 61.600364956025864 3.9512436 25.237066  4.514193264864659 1
    2 2021 -12.878327306682744 0 23.554525  59.53947964898848  3.970292  25.25149   5.45736297270399 1
    2 2022 -14.115050349616004 0  23.39154  60.63256862696945  3.988984  25.24624  6.389794985386821 1
    3 2019   3.365177374694643 0 26.258837  42.85646633123861 4.3307333  27.89051  5.112416695813176 0
    3 2020  1.0743788556747147 0 26.144075 37.357823035579706 4.3307333  27.82362  5.363105897470925 0
    3 2021  1.8351236778965072 0  26.32803 36.853748949317676 4.3307333   27.8251  4.468558044968795 0
    3 2022   .3872273416761475 0 26.320875 33.417645127444665 4.3307333 27.762243  4.226479478622616 0
    4 2019  -1.353600881019171 0  28.23542  65.45971017997765 4.1743875   28.7727 1.7113448430560212 1
    4 2020  2.7935409690617465 1 28.348515   64.8874186810189  4.189655 28.762754 1.5132218801096042 0
    4 2021    2.09165584147271 0  28.31265  67.63034559349867  4.204693  28.86397 1.7355377959413272 0
    4 2022   2.793502609553412 1   28.4295  62.60017936205068 4.2195077 28.764935 1.3985444456239964 0
    5 2019  1.8950821901815418 1  28.01169 18.522881608286134 4.3307333  28.28956 1.3203151730757687 0
    5 2020   8.897787405135656 1 28.080784 18.202540511725633 4.3307333  28.39177 1.3647704165158119 0
    5 2021   4.502650737955088 0 28.115995 15.922643977384304 4.3307333  28.42017 1.3555033205341407 0
    5 2022  3.9829953675955876 1  28.17704 14.668593376729211 4.3307333 28.452774 1.3174982339203027 0
    end
    format %ty year
    label values id id
    label def id 1 "000020", modify
    label def id 2 "000040", modify
    label def id 3 "000050", modify
    label def id 4 "000080", modify
    label def id 5 "000100", modify
    Last edited by Arturo Garciam; 13 Nov 2025, 23:47.

  • #2
    There are at least two major issues with this code.
    1. You never set xtreg, which is required for xtdpdbc. That is why the program and the data as shown do not work (for me), even without any bootstrapping.
    2. When bootstrapping this kind of data (longitudinal), you need to bootstrap entire histories of individuals and not single observations. This means (as soon as your main command works fine), you need to set cluster() and idcluster() in the bootstrap program. Something like:
    Code:
    gen newid = id
    xtset newid year
    bootstrap, cluster(id) idcluster(newid)...
    Best wishes

    Stata 18.0 MP | ORCID | Google Scholar

    Comment


    • #3
      Thank you Felix Bittmann for your quick and helpful response.
      The code you provided worked perfectly, and my bootstrap mediation analysis is now running:

      Code:
       
       gen newid = id xtset newid year, yearly     ****** VARIABLE SETTING ...
      Code:
       global reps = 20  capture program drop btstp program btstp, rclass     ... bootstrap Ind_Eff = r(IE), reps($reps) seed(123) cluster(id) idcluster(newid): btstp     eststo roa_bootstrap: estat bootstrap, percentile bc
      I wanted to politely clarify that the xtdpdbc commands actually ran fine for me after only using xtset newid year, yearly and ensuring the package was installed (outside of the bootstrap program).

      Since the solution is working, I just have a couple of follow-up questions about its implementation:
      1. Could you briefly clarify why the original ID variable (id) is specified as the cluster() variable while the new one (newid) is specified as idcluster()? I would have intuitively done the reverse.
      2. I noticed the runtime is long. It seems running 1000 repetitions will take many hours. Is this expected for bootstrapping the GMM estimator (xtdpdbc) in a dynamic panel model?

      Thank you again for your valuable help!

      Comment


      • #4
        Glad to hear your program is running fine now. Regarding your questions.
        1. I am not sure why Stata has chosen this setup, it is just syntax. For details, refer to help bootstrap.
        2. You can easily estimate how long the total bootstrap will run, just the normal runtime of your program x 1000. If this takes too long and you have a powerful CPU, you can speed things up using parallel:

        https://github.com/gvegayon/parallel
        https://journals.sagepub.com/doi/pdf...36867X19874242

        With this command installed, the syntax becomes something like:
        Code:
        parallel bs, expr(r(IE)) reps(1000) cluster(...) idcluster(...): command
        Best wishes

        Stata 18.0 MP | ORCID | Google Scholar

        Comment

        Working...
        X