Announcement

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

  • Sampling with replacement and creating a new variable

    Dear All,

    If you take this data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(grp y)
    1 107
    1 115
    1 132
    1 118
    1 151
    2 219
    2 287
    2 212
    2 235
    2 241
    2 280
    3 333
    3 321
    3 372
    3 316
    3 345
    3 338
    end
    then run

    Code:
    mata: mata clear
    putmata y
    putmata grp
    mata: 
      rseed(87654321)
      yrows = rows(y)
      grpn = _mm_panels(grp)   
      grpn
      index = mm_sample(.,grpn)  
      ywr = y[index[,1],1]
    end
    getmata ywr
    list, sep(0)
    We get a new var (ywr) that samples y with replacement within its group and puts the new values in ywr. I would like to sample with replacement from group 3 only and place the new values in ywr for all y across all groups (or irrespective of group).

    Any ideas how the code could be modified to do this?

    Many thanks

    Suhail
    Regards
    Suhail Doi

  • #2
    Just at a glance, it looks like mm_sample() comes from the moremata package on github. It looks like there are a few different arguments you can give the function to get different results. You might want to read the help file for mm_sample() to figure out what arguments it needs for sampling irrespective of group.

    Comment


    • #3
      resample might work, but pretty limited and dated.

      Comment


      • #4
        Thanks Daniel, I looked it up and edited as follows:

        Code:
        mata: mata clear
        putmata y
        putmata grp
        mata:
          rseed(87654321)
          yrows = rows(y)
          grpn = _mm_panels(grp)  
          grpn
          index = mm_sample((0\0\yrows),grpn)  
          ywr = y[index[,1],1]
        end
        getmata ywr
        list, sep(0)
        It now delivers what is needed

        Many thanks

        Suhail

        NB post edited
        Last edited by Suhail Doi; 26 Feb 2025, 23:40.
        Regards
        Suhail Doi

        Comment


        • #5
          Thanks George, but resample has the same problem - I cannot resample from a group to the whole dataset otherwise resample would have been perfect. It just resamples within the same group. For example:

          Code:
          resample y if grp==3
          will only resample within group 3

          Many thanks

          Suhail
          Regards
          Suhail Doi

          Comment


          • #6
            I found that adding "sort grp" is very important (in this case the grp variable was already sorted) but for other readers I am just stating this here as well, so the code will be

            Code:
            sort grp
            mata: mata clear
            putmata y
            putmata grp
            mata: 
              rseed(888)
              yrows = rows(y)
              grpn = _mm_panels(grp)   
              grpn
              index = mm_sample((0\0\yrows),grpn)  
              ywr = y[index[,1],1]
            end
            getmata ywr
            list, sep(0)
            Thanks
            Suhail
            Regards
            Suhail Doi

            Comment


            • #7
              I've never been happy with Stata's bootstrap/resample commands. Eviews has a really convenient command that let's you resample a variable with "if", block, N, and so forth.

              This is a kluge but may work for you within a loop.

              Code:
              clear all
              
              sysuse auto, clear
              
              reg mpg weight foreign
              
              preserve
                  keep if foreign
                  gsample 74
                  mkmat weight , matrix(W)
              restore
              svmat W
              
              reg mpg W1 foreign

              Comment


              • #8
                Thanks George. That is a very interesting code and didn't know that there was a wrapper for mm_sample().
                Regards
                Suhail Doi

                Comment


                • #9
                  I just ran across it trying to replicate something in R. It's handy. Still, I wish I could :

                  egen mpg_b = resample(mpg) [ , noreplace block(#) n(#) by(varname) weight(varname) ]

                  Comment

                  Working...
                  X