Announcement

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

  • Saving each coefficient in every bootstrapping reps in different file

    Dear Prof. Clyde Schechter,

    I am currently using 2sls bootstrapping for my paper. I have problem in saving the coefficients.
    These are the commands that i use.


    HTML Code:
    capture postutil clear
    tempfile coefficients
    postfile handle int rep float b_x1 using `coefficients'
    forvalues i = 1/4 {
    use "C:\Users\rose\Documents\tryA_`i'.dta", clear
    bootstrap, reps(100)saving(mybootsample.dta, replace):xtpoisson D yhat_`i' X1 X2 X3 X4 X5 DM1 DM2
    }
    post handle (`i') (_b[yhat_`i'])
    estimates store one_i
    preserve
    save mybootsample_`i', replace
    restore
    }
     postclose yhat_`i'
    Where;
    D= dependent variable
    yhat_i = 100 values of bhat that I obtained from 100reps of bootstrap in the 1st stage of 2sls.
    DM = Dummies


    Stata give red notice when it comes to the 'post handle (`i') (_b[yhat_`i'])' command. It says

    invalid syntax
    post: above message corresponds to expression 1, variable rep
    r(198);


    At the end of this bootstrapping, I would need to save 100 coefficient for each variables in each bootstrapping.

    Thank you for your time Prof. Clyde Schechter,

  • #2
    The problem is that your -post- command is outside the -forvalues i = 1/4- loop. So when you reach the -post- command, you will have already done all four of the bootstrapped regressions, and lost all but the last of the results, and, `i' is now undefined. You need to move that -post- command inside the loop.

    Now, I believe you have more code that may be relevant here. I notice a closed curly brace (}) after the -restore- command--that has no corresponding opening brace. Presumably that lies before the code you show. I wonder what kind of loop structure that is. I raise that question because I notice that not only does your -post- command use an undefined local mcaro `i', but I notice that you also attempt to refer to `i' in a -save- command, and even later in a -postclose yhat_`i'- command. So I wonder if all of those things are part of some outer loop that is also indexed by a -forvalues i = ...- that is not shown. If that is the case, you will need to carefully edit all of this code: you can't have a loop nested inside another loop where both are referring to the same macro. So
    Code:
    forvalues i = ... {
             ...
                   forvalues i = ... {
             ...
             }
             ......
    }
    is not possible. You must change one of those i's to some different name (and, of course, also change every reference to `i' that corresponds to that one.

    As an aside, it is not a good idea to address a post to a specific person unless you are directly responding to that person's earlier response in your thread. The question you posed here could readily be answered by many Forum members. But some, seeing that it was addressed to me, might have passed it over. At best, this would delay your getting a response until I happened to find it. Or, you could have ended up with no response. There are days, or series of days, when I don't come on the Forum at all. Or, after seeing the mention of 2sls, a procedure I never use and know nothing about, I might have skipped over it. Your question might have gone unanswered as a result. So it is better to not specify any particular person.

    Comment


    • #3
      Dear Prof. Clyde Schechter,

      Thank you so much for the reply. And also for the advice. I will remember to keep my post open and not to be sent to a specific person.
      Actually, I edit the command so many times before I sent it. The initial command has only 1 loop but for the -post- command, Stata said

      _ ambiguous abbreviation
      post: above message corresponds to expression 1, variable rep
      r(111);

      I think it is due to `i' which as per your advice i need to change to different name.




      HTML Code:
      capture postutil clear
      tempfile coefficients
      postfile handle int rep float b_x1 using `coefficients'
      forvalues i = 1/4 {
      use "C:\Users\rose\Documents\tryA_`i'.dta", clear
      bootstrap, reps(100)saving(mybootsample.dta, replace):xtpoisson D yhat_`i' X1 X2 X3 X4 X5 DM1 DM2
      post handle (`i') (_b[yhat_`i'])
      estimates store one_i
      preserve
      save bootstrap_`i', replace
      restore
      }
       postclose yhat_`i'

      post handle (`i') (_b[yhat_`i']) , actually I
      wanted to save every 'i' result in every 'i' bootstrap. I guess this is my main difficulty with
      with constructing command.

      Thank you Prof. Clyde Schechter.




      Comment


      • #4
        That looks better. Now you will be saving results for all four bootstraps, and your -post- command should run correctly.

        But you will still have a problem when you get to -postclose yhat_`i'-. There are two problems with it: one is that, again, you are referring to `i' outside the loop that defines `i'. The other is that you have never created a yhat_`i' postfile to begin with. You need to say -postclose handle-.

        Comment


        • #5
          Dear Prof. Clyde Schechter,

          I will try it now. Originally, the reps will be 100 for 100 'i'.

          Thank you so much for the advice Prof. Clyde Schechter.

          Comment


          • #6
            Dear Prof. Clyde Schechter,

            I have tried these commands:

            HTML Code:
            capture postutil clear
            tempfile coefficients
            postfile handle int rep float b_x1 using `coefficients'
            forvalues i = 1/100 {
            use "C:\Users\rose\Documents\tryA_`i'.dta", clear
            bootstrap, reps(100) saving(mybootsample.dta, replace):xtpoisson Migration yhat_`i' X1 X2 X3 X4 X5 DM1 DM2
            post handle (`i') (_b[yhat_`i'])
            estimates store one_i
            preserve
            save bootstrap_`i', replace
            restore
            }
             postclose handle
            but Stata didnt save the results which should contain 100 coefficient for each variables used for each reps . Instead, Stata save the data with additional column labelled _est_one_i , in each 100 bootstrapping repetitions.
            Therefore, at the end Stata saved 100 sets of data with one additional column in bootstrap_1 , bootstrap_2, bootstrap_3,bootstrap_4....bootstrap_100.

            I really appreciate your time and advice.
            Thank you so much Prof. Clyde Schechter.

            Comment


            • #7
              It is difficult to understand what you are encountering.

              I believe what you are looking for will be found in the file mybootsample.dta after each time the bootstrap command is run.
              Code:
              . sysuse auto, clear
              (1978 Automobile Data)
              
              . bootstrap, saving(gnxl, replace): regress mpg weight gear_ratio foreign
              (running regress on estimation sample)
              
              Bootstrap replications (50)
              ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
              ..................................................    50
              
              Linear regression                               Number of obs     =         74
                                                              Replications      =         50
                                                              Wald chi2(3)      =     178.62
                                                              Prob > chi2       =     0.0000
                                                              R-squared         =     0.6670
                                                              Adj R-squared     =     0.6527
                                                              Root MSE          =     3.4096
              
              ------------------------------------------------------------------------------
                           |   Observed   Bootstrap                         Normal-based
                       mpg |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |   -.006139   .0005077   -12.09   0.000    -.0071342   -.0051439
                gear_ratio |   1.457113   1.451109     1.00   0.315    -1.387008    4.301235
                   foreign |  -2.221682   1.527374    -1.45   0.146     -5.21528    .7719166
                     _cons |   36.10135   4.969828     7.26   0.000     26.36067    45.84204
              ------------------------------------------------------------------------------
              
              . describe using gnxl
              
              Contains data                                 bootstrap: regress
                obs:            50                          13 May 2019 11:13
               vars:             4                          
               size:         1,000                          
              -------------------------------------------------------------------------------
                            storage   display    value
              variable name   type    format     label      variable label
              -----------------------------------------------------------------------------------------------
              _b_weight       float   %9.0g                 _b[weight]
              _b_gear_ratio   float   %9.0g                 _b[gear_ratio]
              _b_foreign      float   %9.0g                 _b[foreign]
              _b_cons         float   %9.0g                 _b[_cons]
              -------------------------------------------------------------------------------
              Sorted by:

              Comment


              • #8
                Dear Prof.William Lisowski,

                Thank you for the reply and example. I really appreciate you time and help.
                I tried again today, and after the -preserve- command was dropped, only then Stata save coefficients.

                Thank you Prof.William Lisowski.
                Thank you Prof. Clyde Schechter.

                Comment

                Working...
                X