Announcement

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

  • Bootstrap for regression parametrs

    preserve
    local var1 = "price"
    local var2 = "weight"
    local n=r(N)
    local B=1000
    local alfa = 0.05 //b to liczba próbek
    forvalues i=1/`B'{
    generate sampl`i'=`var1'[ceil(`n'*runiform())]
    generate sampl1`i'=`var2'[ceil(`n'*runiform())]
    reg var1 var 2
    }

  • #2
    Welcome to the Stata Forum/Statalist.

    Please read the FAQ. There you will find advice on how to post an insightful thread.

    You might well read about the best way to share command/data/output, which are: 1) under code delimiters; 2) with - dataex - command, if your version is new, or the SSC dataex otherwise.

    To end, I can see you have written a code. That is all.

    Your post seems to lack communication skills, hence I fail to understood what kind of help you wish from the Stata community.

    Best regards,

    Marcos

    Comment


    • #3
      Hi everyone. I need to make bootstrap for regression paramatr for price and weight in new sample from general population, but i don't know how to make regression in new sample.
      I want to make 1000 new samples, make regression price weight, save parametrs of regression for each sample and according to parameters make bootstrap.

      Comment


      • #4
        Stata has convenient command that bootstraps for you. What you want can be achieved automatically by

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . bootstrap _b[weight], reps(1000) saving(mybootsample, replace) nodots: reg price weight
        
        Linear regression                               Number of obs     =         74
                                                        Replications      =      1,000
        
              command:  regress price weight
                _bs_1:  _b[weight]
        
        ------------------------------------------------------------------------------
                     |   Observed   Bootstrap                         Normal-based
                     |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
               _bs_1 |   2.044063   .3982494     5.13   0.000     1.263508    2.824617
        ------------------------------------------------------------------------------
        
        
        . use mybootsample
        (bootstrap: regress)
        
        . summ
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
               _bs_1 |      1,000    2.035768    .3982494   .3991753   3.156375

        Comment


        • #5
          Karol:
          the -bootstrap- entry in Stata .pdf manual includes interesting examples that are worth reading.
          Kind regards,
          Carlo
          (StataNow 18.5)

          Comment


          • #6
            Originally posted by Joro Kolev View Post
            Stata has convenient command that bootstraps for you. What you want can be achieved automatically by

            Code:
            . sysuse auto, clear
            (1978 Automobile Data)
            
            . bootstrap _b[weight], reps(1000) saving(mybootsample, replace) nodots: reg price weight
            
            Linear regression Number of obs = 74
            Replications = 1,000
            
            command: regress price weight
            _bs_1: _b[weight]
            
            ------------------------------------------------------------------------------
            | Observed Bootstrap Normal-based
            | Coef. Std. Err. z P>|z| [95% Conf. Interval]
            -------------+----------------------------------------------------------------
            _bs_1 | 2.044063 .3982494 5.13 0.000 1.263508 2.824617
            ------------------------------------------------------------------------------
            
            
            . use mybootsample
            (bootstrap: regress)
            
            . summ
            
            Variable | Obs Mean Std. Dev. Min Max
            -------------+---------------------------------------------------------
            _bs_1 | 1,000 2.035768 .3982494 .3991753 3.156375
            Thank you very much for reply, but i need to generate new 1000 sample, make regression to 1000 sample, save 2 regression parametrs for each sample and next make bootstrap i think its other case but probably i be wrong because my knowladge about stata is under amator and i cant read syntax on this program. Say me please if this the same.

            Comment


            • #7
              Originally posted by Karol Podolski View Post

              Thank you very much for reply, but i need to generate new 1000 sample, make regression to 1000 sample, save 2 regression parametrs for each sample and next make bootstrap i think its other case but probably i be wrong because my knowladge about stata is under amator and i cant read syntax on this program. Say me please if this the same.
              Karol,

              I don't understand what you are asking. Most of the time, we would use bootstrap to estimate the variance of some quantity (e.g. a regression parameter) after a regression. Generate 1000 samples sounds a lot like the bootstrapping process itself. Can you ask someone whose English is better to translate?
              Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

              When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

              Comment


              • #8
                Why do you want to do that? You may think we only need to know what you want, but your question is unclear without knowing the why. Right now, we could give you very different answers, which will help you attain different goals. Do you want to program the bootstrap by hand (maybe for educational purposes) or do you want the most efficient way to get the bootstrap standard errors/confidence intervals?

                If you want the latter, then just do: reg price weight, vce(bootstrap, reps(20000))

                If you want the former, then you need to be even more precise about what you want to do (and what not). Just give us a step by step guide of what you want to do. It is not clear to me from your question how far you want to go. For example, are you happy with using a Stata program to do the sampling for you, or do you want to program that yourself as well?
                ---------------------------------
                Maarten L. Buis
                University of Konstanz
                Department of history and sociology
                box 40
                78457 Konstanz
                Germany
                http://www.maartenbuis.nl
                ---------------------------------

                Comment


                • #9
                  I need to progam the bootstrap by hand and make it most efficient way to minimalize standard errors but bootstrap should be by hand. I want to do the sampling by hand .For example (for mean value)
                  preserve
                  [code]
                  local zmienna = "price"
                  quietly summ `zmienna'
                  local n=r(N)
                  local B=1000
                  local alfa = 0.05 //b to liczba próbek
                  forvalues i=1/`B'{
                  generate proba`i'=`zmienna'[ceil(`n'*runiform())]
                  quietly summ proba`i'
                  }
                  if `B'>`n'{
                  set obs `B'
                  generate srednie =.

                  forvalues i=1/`B'{
                  quietly summ proba`i' //
                  quietly replace srednie=r(mean) in `i'
                  }
                  sort srednie
                  local lb=srednie[ceil((`alfa'/2)*`B')]
                  di `lb'
                  local ub=srednie[ceil((1-(`alfa'/2))*`B')]
                  di `ub'
                  display "przedział ufnosci obliczony metodą bootstrap dla `B' iteracji"
                  display "`lb' `ub'"
                  restore
                  [code]

                  Bootstrap doesn't must be perfect, i need program which reminds bootstrap and minimalize standards errors for regression.

                  Comment


                  • #10
                    Originally posted by Karol Podolski View Post
                    I need to progam the bootstrap by hand and make it most efficient way to minimalize standard errors but bootstrap should be by hand. I want to do the sampling by hand. ...
                    Again, why? You are not trying to estimate some strange statistic where we don't know the theoretical variance of its estimator. Also, you still haven't explained why you need to do the bootstrap by hand, instead of letting Stata do it for you from your existing data.
                    Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

                    When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

                    Comment


                    • #11
                      To copy Weiwen: Why? We really need to know why. We want to help you, but you need to help us help you. I think you wanted to answer my questions in #9, but there you only gave us what you want but not why you want it.
                      ---------------------------------
                      Maarten L. Buis
                      University of Konstanz
                      Department of history and sociology
                      box 40
                      78457 Konstanz
                      Germany
                      http://www.maartenbuis.nl
                      ---------------------------------

                      Comment


                      • #12
                        Because its my homework :/

                        Comment


                        • #13
                          We are obviously not going to do your homework for you. So I can only give you some hints. You could look at the series of blog posts starting here: https://blog.stata.com/2012/07/18/us...rators-part-1/
                          ---------------------------------
                          Maarten L. Buis
                          University of Konstanz
                          Department of history and sociology
                          box 40
                          78457 Konstanz
                          Germany
                          http://www.maartenbuis.nl
                          ---------------------------------

                          Comment


                          • #14
                            Ok. I understand. Can you give me answer how i can save my single observation in matrix ? I try this way but this is bad why.
                            Code:
                            preserve
                            local zmienna = "price"
                            quietly summ `zmienna'
                            local n=r(N)
                            local B=1000
                            local alfa = 0.05 //b to liczba próbek
                            forvalues i=1/`B'{
                            generate proba`i'=`zmienna'[ceil(`n'*runiform())]
                            quietly summ proba`i'
                            matix A[1,i]=proba`i'
                            }

                            Comment

                            Working...
                            X