Dear Statalisters,

I am using Stata 15.1 and I would like to address the sample selection problem in a panel data set. The approach seems not complicated (as explained in the textbook of Wooldridge: Econometric Analysis of Cross Section and Panel Data. ch. 19. You can also check: Wooldridge, J. M. (1995), ‘‘Selection Corrections for Panel Data Models under Conditional Mean Independence Assumptions,’’ Journal of Econometrics 68, 115-132.).
Very briefly: I have some firms that innovate or not (y1=1 or y1=0), and those that innovate will report their sales from innovations (y2), and thus will show a missing for those firms reporting y1=0. For accounting for sample selection I must to do first a probit model for y1 with respect to the independent variables in the second stage plus some exclusion restrictions (for each year as Wooldridge advice) and then calculate the yearly inverse Mill`s ratios and include them into the second stage. Since these ratios are generated in a previous step, the standard error must to be corrected in the second stage. However, since I am working with a panel data, I cannot just put "bootstrap" before the regression, but to do it in a program.
After two days trying by my self, I cannot understand what am I doing wrong or even how to continue. I will show you the program I created following the guide of Professor Clyde Schechter in post #2:
HTML Code:
https://www.statalist.org/forums/forum/general-stata-discussion/general/1477399-boostrap-for-xtqreg
as well as a short example (dataex) for if it is helpful to see the kind of dataset I am working with.
From the program I need the table (with correction for the standard errors) from the second stage (do not know how to do it). And also to do some Wald tests for the IMR`year' to check if they are jointly zero (also do not know how to do it).
This is the process I want to implement in the program:
Code:
    forvalues year=2005/2015 {
    probit y1 main1 main2 x1 x2 x3 z1 z2 z3 if year==`year'    /* selection equation */
    predict acltxb1_`year' , xb
    predict acltpr1_`year', pr
    gen acltndenxb1_`year' = normalden(acltxb1_`year')
    gen acltnxb1_`year' = normprob(acltxb1_`year')
    gen acltlambda1`year' = acltndenxb1_`year' / acltnxb1_`year'
          }
 forvalues i = 2005/2015 {
          gen year`i' = year==`i'
          }

    forvalues i = 2005/2015 {
          generate IMR`i' = acltlambda1`i'*year`i'  /* generating IMR*time dumies */
          }
  
 xtreg y2 main1 main2 x1 x2 x3 i.year IMR2005-IMR2015 if y1==1, fe /*main equation */
test IMR2005 IMR2006 IMR2007 IMR2008 IMR2009 IMR2010 IMR2011 IMR2012 IMR2013 IMR2014 IMR2015
And this is what I try to build (of course it is wrong but do not know why)
Code:
xtset, clear
capture program drop xtq_diff
program define xtq_diff, rclass
    xtset id
    forvalues year=2005/2015 {
    probit y1 main1 main2 x1 x2 x3 z1 z2 z3 if year==`year'    /* selection equation */
    local predict acltxb1_`year' , xb
    local predict acltpr1_`year', pr
    local gen acltndenxb1_`year' = normalden(acltxb1_`year')
    local gen acltnxb1_`year' = normprob(acltxb1_`year')
    local gen IMR`year' = acltndenxb1_`year' / acltnxb1_`year'
          }
 xtreg y2 main1 main2 x1 x2 x3 i.year `IMR2005' `IMR2006' `IMR2007' `IMR2008' `IMR2009' `IMR2010' `IMR2011' `IMR2012' `IMR2013' `IMR2014' `IMR2015' if y1==1, fe /*main equation */
    return scalar diff = `IMR2005'-`IMR2006'-`IMR2007'-`IMR2008'
    exit
end

bootstrap diff = r(diff), reps(50) seed(10101) cluster(id) idcluster(newid): xtq_diff
After several tries with a more simple program, I realized that the condition
Code:
if y1==1
in the main equation is problematic (do not why). And also, the fact that the variable y2 present missings for when y1==0 is also problematic (the most simple program does not run, as it did without the missings).

Please, I am very sorry for posting this long post, any help will be much much appreciated.

Here you have a descriptive of the data (I just realize that for the dataex below, if you run the first code --what I want to implement-- will not run properly with such small amount of data)
Code:
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
          y1 |    117,559    .4539168    .4978739          0          1
          y2 |     53,720    16.03345    27.13275          0        100
       main1 |    117,555    177.5277    3387.195          0   513079.3
       main2 |    117,555    978.8756    19434.76          0    5731453
          x1 |     83,642    .3838383    .4863222          0          1
-------------+---------------------------------------------------------
          x2 |    117,462    .0698244    .2497444          0          2
          x3 |    117,555    4.151557    1.718149          0   10.63367
          z1 |    117,559     .548206     .344792          0          1
          z2 |    117,559    .4623664    .3309483          0          1
          z3 |    117,559    .3635735    .2682159          0          1
-------------+---------------------------------------------------------
          id |    117,559    6243.006    3690.662          1      12844
        year |    117,559    2009.019    3.316198       2004       2015
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int(id year) byte y1 double y2 byte x1 double(x3 x2) float(z1 z2 z3 main1 main2)
 1 2004 1   15 0 2.9444389791664403   .11628050303189412 .4444444        0        0         0         0
 1 2005 1   25 0  3.091042453358316   .11155810978566506 .1111111        0        0         0         0
 1 2006 1   30 0 3.1780538303479458    .2181349936661966 .1111111        0        0         0         0
 1 2007 1   25 0 3.1780538303479458   .06591904929668757 .1111111        0        0         0         0
 1 2008 1   25 0 3.2188758248682006   .08327321246305641 .1111111        0        0         0         0
 1 2009 1   25 0  3.258096538021482   .09046644551097324 .1111111        0        0         0         0
 1 2010 1   10 0  3.295836866004329   .20395702599256194        1 .3333333 .3333333         0         0
 1 2011 1   20 0  2.995732273553991   .31468851173081386        1 .6666666 .4166667         0         0
 1 2012 1   25 0  2.995732273553991     .694693505873705        1 .8333333 .4166667         0         0
 1 2013 1   33 0 2.9444389791664403   1.4777636434095467        1 .8333333 .1666667         0         0
 1 2014 1   70 0 2.6390573296152584    1.465341156936412        1 .6666666        0         0         0
 1 2015 1   40 0 2.5649493574615367   1.2855846532976578        1 .6666666        0         0         0
 2 2004 0    . . 2.3978952727983707                    0        1 .6666666 .8333333         0         0
 2 2005 0    . .  2.302585092994046                    0 .8888889 .8333333 .8333333         0         0
 2 2006 0    . .  2.302585092994046                    0 .8888889 .8333333 .6666666         0         0
 2 2007 1    0 0  2.302585092994046                    0 .7777778        1 .5833334         0         0
 2 2008 1    0 0  2.302585092994046                    0 .8888889 .8333333 .5833334         0         0
 2 2009 1    0 0 2.0794415416798357                    0 .3333333 .6666666        0         0         0
 2 2010 0    . . 2.0794415416798357                    0 .6666666 .6666666      .25         0         0
 2 2011 0    . . 2.0794415416798357                    0 .3333333        1       .5         0         0
 2 2012 0    . 1 2.0794415416798357                    0        1        1 .8333333         0       373
 2 2013 0    . 0 2.0794415416798357                    0 .5555556 .8333333       .5         0         0
 2 2014 0    . 0 2.0794415416798357                    0 .5555556 .6666666 .5833334         0         0
 2 2015 0    . . 2.0794415416798357                    0 .5555556 .6666666 .5833334         0         0
 3 2004 1    0 0   3.58351893845611  .057618833395396696 .7777778 .8333333 .6666666         0         0
 3 2005 0    . 0  3.912023005428146                    0        1 .6666666 .5833334         0         0
 3 2006 0    . .  3.912023005428146                    0        0        0        0         0         0
 3 2007 0    . .   4.07753744390572                    0 .6666666 .8333333        1         0         0
 3 2008 0    . .  2.995732273553991                    0 .6666666 .8333333 .8333333         0         0
 3 2009 0    . .  3.295836866004329                    0        1 .6666666      .75         0         0
 3 2010 0    . . 3.6888794541139363                    0 .6666666 .6666666 .6666666         0         0
 3 2011 0    . .  2.772588722239781                    0 .6666666 .8333333 .8333333         0         0
 3 2012 0    . . 3.9512437185814275                    0        1        0       .5         0         0
 3 2013 0    . .  3.044522437723423                    0        1 .8333333       .5         0         0
 4 2004 0    . 0  .6931471805599453   .14064971291086337 .6666666       .5       .5         0         0
 4 2005 1    0 1  .6931471805599453                 .087        1        1        1         0         0
 4 2006 1    0 0                  0                    0        1        1        1         0       994
 4 2007 0    . 0                  0                    0        1        1      .75         0         0
 4 2008 0    . .                  0                    0 .8888889 .8333333      .75         0         0
 4 2009 0    . .                  0                    0        1        1        1         0         0
 4 2010 0    . .                  0                    0        1        1        1         0         0
 4 2011 0    . .                  0                    0        1        1        1         0         0
 4 2012 0    . .                  0                    0        1        1        1         0         0
 4 2013 1   30 0  .6931471805599453   .07507884950336581        1        1        1         0         0
 5 2004 1   80 0 1.9459101490553132   .17195091086838932 .2222222       .5        0         0         0
 5 2005 1   20 0 1.9459101490553132   .09699191448540496 .2222222       .5        0         0         0
 5 2006 1 29.5 0  1.791759469228055   .17754350477437011 .2222222       .5        0         0  913.9235
 5 2007 1  4.6 0  1.791759469228055    .3837559064739411 .2222222       .5        0         0  1359.132
 5 2008 1    5 1 1.6094379124341003    .2130664092965216 .8888889       .5 .6666666         0    4371.1
 5 2009 1   10 1 1.0986122886681098   .07197761143795689 .7777778       .5 .6666666         0  6209.525
 5 2010 1   10 1 1.3862943611198906    .1939692474419783 .7777778       .5 .6666666         0         0
 5 2011 1   10 1 1.6094379124341003   1.0774121445424065 .7777778       .5 .6666666         0         0
 5 2012 1   80 1  1.791759469228055   .30190804450969133 .8888889 .8333333       .5         0  712.4927
 5 2013 1    6 0 1.6094379124341003                    0        1 .6666666       .5         0         0
 5 2015 1   95 1 1.3862943611198906                    0        1 .6666666       .5         0         0
 6 2004 1    0 0  3.044522437723423  .023363215803786804        1 .6666666 .3333333         0         0
 6 2005 1    0 0  3.091042453358316   .02488418510019133        1 .6666666 .3333333         0 36.946365
 6 2006 1    0 0 3.1780538303479458   .02203491203379804        1 .6666666 .3333333         0  38.22866
 6 2007 1    0 0 3.2188758248682006   .02197197542778446        1 .6666666 .3333333         0         0
 6 2008 1    0 0  3.258096538021482  .024768402432117524        1 .6666666 .3333333         0         0
 6 2009 1    0 0 2.8903717578961645  .043628161799596756        1 .6666666 .3333333         0         0
 6 2010 1    0 0 2.8903717578961645  .047617787730093966        1 .6666666 .3333333         0         0
 6 2011 0    . 0 2.3978952727983707                    0        1 .6666666 .3333333         0         0
 6 2012 0    . . 2.0794415416798357                    0        1        0 .3333333         0         0
 7 2004 0    . .  5.783825182329737                    0        0        0        0         0         0
 7 2005 0    . .  5.730099782973574                    0 .1111111        0        0         0         0
 7 2006 1    0 1  5.181783550292085                    0        0        0        0         0         0
 7 2007 1    0 0  4.844187086458591                    0 .4444444       .5       .5         0         0
 7 2008 1    0 0  4.762173934797756                    0 .8888889 .8333333      .75         0         0
 7 2009 0    . .  4.762173934797756                    0 .8888889 .8333333      .75         0         0
 7 2010 0    . .  4.727387818712341                    0 .5555556 .3333333 .3333333         0         0
 7 2011 0    . .  4.727387818712341                    0 .8888889        0        0         0         0
 7 2012 0    . .  4.700480365792417                    0        0        0        0         0         0
 7 2013 0    . .  4.727387818712341                    0 .2222222        0        0         0         0
 7 2014 0    . .  4.770684624465665                    0 .7777778        1       .5         0         0
 7 2015 0    . . 4.7535901911063645                    0        0 .1666667        0         0         0
 8 2004 1   77 1 1.9459101490553132   .23924395665200565        1 .3333333 .3333333         0         0
 9 2004 1    0 0  2.772588722239781                    0 .1111111        0        0         0  1527.875
 9 2005 0    . 0 2.4849066497880004   .08102188765002487 .3333333 .3333333 .3333333         0  406.1347
 9 2006 0    . 0 2.4849066497880004                    0 .3333333        0      .25         0         0
 9 2007 0    . 0 2.5649493574615367                    0 .2222222        0        0         0         0
 9 2008 1    1 0 2.5649493574615367 .0037338232374995446 .3333333        0        0         0         0
11 2004 1    0 1 3.2188758248682006  .020868634055714878 .5555556       .5 .3333333  4717.745         0
11 2005 1    0 1  3.295836866004329     .022795597436482 .5555556       .5 .3333333 4528.9175         0
11 2006 1   20 1 3.4965075614664802  .012718997364693722 .7777778 .3333333 .4166667  3297.949         0
11 2007 1    0 1 3.5553480614894135  .008183939809835146 .5555556 .3333333      .25 2851.6885  58.19772
11 2008 1   20 1 3.4011973816621555   .01709749583382931 .5555556 .3333333      .25         0 3072.7476
11 2009 1   20 1 3.4011973816621555   .01871897821983031 .5555556 .3333333      .25         0  3206.669
20 2004 1   .5 1  4.204692619390966  .017469072190368716 .6666666       .5 .4166667 1461.2487  186.1568
20 2005 0    . 1  4.174387269895637  .046790002310655526 .6666666 .6666666 .5833334 1339.4368         0
20 2006 1    0 1 4.1588830833596715   .03908659000351231 .6666666 .8333333 .6666666 1520.8528         0
20 2007 1  4.6 1  4.143134726391533   .04142966294820746 .6666666 .8333333 .6666666 2200.8137         0
20 2008 1  4.6 1  4.143134726391533    .0453161594348968 .6666666 .8333333 .6666666   2288.92         0
20 2010 1   20 1  4.330733340286331   .02959460778214954 .6666666        0       .5  8027.119  4961.746
20 2011 1   20 1 5.0238805208462765   .10545263729220362 .6666666        0       .5         0         0
20 2012 1   20 1  4.543294782270004   .06503342386711894 .5555556 .6666666 .6666666  6628.726  17475.73
20 2013 1   20 1   4.61512051684126    .0751187815657481 .6666666 .6666666 .6666666 21016.836  34786.49
20 2014 0    . 1   4.61512051684126   .07284496273083095 .6666666 .6666666 .6666666  22037.18  36475.33
21 2004 0    . .  7.774015077250727                    0 .1111111        0        0         0         0
21 2005 0    . .  7.419979923661835                    0        0        0        0         0         0
end