Announcement

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

  • simulation

    Hi, I am trying to do the simulations. where I first regress the price on weight and after that, I randomly shuffle the X() variable and run regression each time.



    sysuse auto, clear
    regress price weight
    forvalues i=1/100 {
    shufflevar weight
    quietly regress price weight_shuffled

    }


    How I can save and plot the regression (slope) coefficient please

  • #2
    Code:
    matrix B = J(100,2,.)
    matrix colnames B = RUN BETA
    regress price weight
    forvalues i=1/100 {
    shufflevar weight
    quietly regress price weight_shuffled
    matrix B[`i',1] = `i'
    matrix B[`i',2] = _b[weight]
    }
    capture drop RUN BETA
    svmat B , names(col)
    scatter BETA RUN

    Comment


    • #3
      I do not use shufflevar, but here is a way using official commands. I plot using coefplot from SSC.

      Code:
      sysuse auto, clear
      set seed 12132022
      regress price weight
      gen obsno=_n
      mat res= J(100, 2, .)
      mat res[1,1]=_b[weight]
      mat res[1,2]=_se[weight]
      frame put weight obsno, into(shuffle)
      frame shuffle: rename weight weight_shuffled
      forvalues i=2/100{
          frame shuffle{
              gen rand= rnormal()
              sort rand
              replace obsno=_n
              drop rand
          }
          cap drop shuffle
          frlink 1:1 obsno, frame(shuffle)
          frget weight_shuffled, from(shuffle)  
          quietly regress price weight_shuffled
          mat res[`i',1]=_b[weight_shuffled]
          mat res[`i',2]=_se[weight_shuffled]
          drop weight_shuffled
      }
      set scheme s1mono
      mat res= res'
      coefplot mat(res), se(c2) ylab("") xtitle("Coefficient")
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	27.1 KB
ID:	1693280

      Comment


      • #4
        I like Andrew's better, but it depends on what you're interested in and why.

        Comment


        • #5
          Thanks I want to get a graph like this please

          Comment


          • #6
            Code:
            use auto, clear
            matrix B = J(100,2,.)
            matrix colnames B = RUN BETA
            regress price weight
            forvalues i=1/100 {
            shufflevar weight
            quietly regress price weight_shuffled
            matrix B[`i',1] = `i'
            matrix B[`i',2] = _b[weight]
            }
            capture drop RUN BETA
            svmat B , names(col)
            kdensity BETA

            Comment


            • #7
              Thanks

              and how I can draw a line to indicate the value of the coefficient from the original regression

              Comment


              • #8
                The trick is that the true coefficient may look nothing like the other one, since it will have a mean near zero (as it is not a determinant of the DV).
                Code:
                use auto, clear
                matrix B = J(100,2,.)
                matrix colnames B = RUN BETA
                regress price weight
                local btrue = _b[weight]
                forvalues i=1/100 {
                shufflevar weight
                quietly regress price weight_shuffled
                matrix B[`i',1] = `i'
                matrix B[`i',2] = _b[weight]
                }
                capture drop RUN BETA
                svmat B , names(col)
                kdensity BETA , xline(`btrue') xlabel(-`btrue'(1)`btrue', format(%5.1f))

                Comment


                • #9
                  In a thread a few weeks ago, I had suggested -shufflevar- for a situation involving *several* explanatory variables. In the current case, where Muhammad's example suggests he wants to shuffle only *one* variable, the built-in command -permute- will do all the hard work. George Ford's approach, however, can nicely be generalized to handle permutation of more than one predictor. For illustration, though, here's what -permute- would do with only one predictor.
                  Code:
                  sysuse auto, clear
                  set seed 8553412
                  tempfile coef
                  // -permute- does all the work
                  permute weight b = _b[weight], reps(10000) saving(`coef'):  ///
                     regress price weight
                  local btrue = el(r(b), 1, 1) 
                  // Load and plot what permute has created
                  use `coef', clear
                  kdensity b, xline(`btrue') xlabel(-`btrue'(1)`btrue', format(%5.1f))b

                  Comment


                  • #10
                    Thanks, everyone


                    I have generated a y using this formula =50+0.65*X+U , U is a normally distributed random variable.


                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input float y byte x
                    53.23553  3
                    53.54874  7
                    55.81465  9
                     50.2805  2
                    51.07527  4
                     53.2432  5
                    52.15478  5
                    51.90472  3
                    52.39089  7
                    54.28887  7
                    55.12636  7
                    54.24528  4
                    55.61297 10
                    51.61602  5
                    54.63839  9
                    54.35038  7
                    53.33028  5
                    51.59156  3
                    50.38386  3
                    53.41733  6
                    50.63123  2
                    52.46981  4
                    52.09918  6
                    54.58028  5
                    49.74194  4
                    56.95749 10
                    52.56636  2
                    52.95005  2
                    52.86676  6
                    52.16235  5
                    end

                    using the following code I am getting the graph as shown in the picture, why some of the beta values from the simulations are not near to the true beta.

                    matrix B = J(100,2,.)
                    matrix colnames B = RUN BETA
                    regress y x
                    local btrue = _b[x]
                    forvalues i=1/100 {
                    shufflevar x
                    quietly regress y x_shuffled
                    matrix B[`i',1] = `i'
                    matrix B[`i',2] = _b[x]
                    }
                    capture drop RUN BETA
                    svmat B , names(col)
                    kdensity BETA , xline(`btrue') xlabel(-`btrue'(1)`btrue', format(%5.1f))






                    Attached Files

                    Comment


                    • #11
                      There is no reason for a simulated beta to be anywhere near the real beta. The mean of the simulated beta will be 0. You are basically creating a random variables that has nothing to do with the dependent variable. In about 5% of cases, the t-stat on the beta will exceed 1.96 or fall below -1.96. The Type I error rate.

                      I don't think what you are doing serves any purpose. What are you trying to demonstrate?

                      Comment

                      Working...
                      X