Announcement

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

  • How to randomly sampling and regression, and save the output 500 times, and draw kdensity of 500 coeffients?

    for example, I have a sample of 2000 students from 50 schools.

    I want to randomly drop 2 schools and run a regression. Repeat this process 500 times. (I can do this)

    What I don't know is that how to save 500 outputs (or coefficient) of regression? And draw the kdensity of 500 coefficients?

    Thank you very much!

  • #2
    you can do a program that "wraps" the process to later simulate:

    Code:
    program my_sim, eclass
    preserve
        *** Drop 2 schools
       regress y x 
    restore
    end
    
    simulate, reps(500): my_sim
    kdensity "coefficient of interest"

    Comment


    • #3
      Thank you very much , I can understand your code! But I don't know how to save the coefficient of x?

      Comment


      • #4
        program my_sim, eclass
        use "D:\stata\xxxx.dta",clear
        preserve
        tempfile temp
        bys school: keep if _n==1
        sample 48,count
        sort school
        save `temp'
        restore
        merge m:1 school using `temp'
        keep if _merge == 3
        drop _merge
        reg y x1 x2
        end
        simulate, reps(500): my_sim

        but can not run the code " simulate, reps(500): my_sim"?
        how to save the coefficient of x1?
        Last edited by Eric Cheung; 22 Oct 2021, 07:39.

        Comment


        • #5
          Because regress was the last command within the program, after you do simulate, it should store ALL the coefficients fo rALL variables.
          Otherwise, you can do something like
          matrix b = _b[x1]
          ereturn post b

          HTH

          Comment


          • #6
            Some time ago I presented a tutorial on how to use simulate, which may be helpful: http://maartenbuis.nl/presentations/chicago08.html
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              Originally posted by FernandoRios View Post
              Because regress was the last command within the program, after you do simulate, it should store ALL the coefficients fo rALL variables.
              Otherwise, you can do something like
              matrix b = _b[x1]
              ereturn post b

              HTH
              Thank you very much! ! I succeeded.

              Comment


              • #8
                Originally posted by Maarten Buis View Post
                Some time ago I presented a tutorial on how to use simulate, which may be helpful: http://maartenbuis.nl/presentations/chicago08.html
                Thank you very much! I'm going to learn this.

                Comment

                Working...
                X