Announcement

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

  • Export bootstrap foreach loop in Excel

    I apologize if this is a trivial question but I have never performed any bootstrap operation in Stata before and I could not find an answer to my question.

    I would like to bootstrap some operation performed over correlation coefficients, such as a mean of two correlation coefficients:

    Code:
    program mean_r1r2, rclass
    version 14
    corr var1 var2
    local corr12 = r(rho)
    corr var2 var3
    local corr23 = r(rho)
    return scalar mean = (`corr12' + `corr23') /2
    end
    And then loop it over a list of countries (which takes the values of 1,3,8,56,...,99).

    Code:
    levelsof country, local(levels)
    foreach l of local levels {
    preserve
    keep if country == `l'
    bootstrap r(mean), reps(100) seed(1234): mean_r1r2
    restore
    }
    This does the job, however I could not find a way to store, for each country, the value of the coefficient, the standard errors and the number of observations.

    Many thanks for your help!
    Last edited by Marco Colagrossi; 04 Oct 2018, 11:04.

  • #2
    Briefly, you can create a -postfile- to hold the bootstrap results. See -help postfile-. As for what to put in the postfile, -bootstrap- saves the estimates in e(b), the standard errors n e(se), and the sample size in e(N).
    Last edited by Clyde Schechter; 04 Oct 2018, 11:24.

    Comment


    • #3
      Your preserve / restore cycle inevitably means that -- given the rest of your code -- at the end you have the original dataset back with nothing added. More positively, you can retrieve results from the e-class results left behind by bootstrap. Much depends on whether you want the results stored alongside the original data (at the cost of repetition of values for all the observations in each country) or in a different dataset (in which case postfile is arguably the command of choice).

      Despite some residual fondness for levelsof I would be more inclined to do something like this:

      Code:
      egen group = group(country)
      su group, meanonly
      forval g = 1/`r(max)' {
          bootstrap r(mean), reps(100) seed(1234): mean_r1r2 if group == `g'
          * save results here
      }
      where the all-important replacement for the comment depends on what you want to do ("store" is too vague a description).

      All that aside, I am queasy about averaging correlations, except perhaps on an atanh scale.

      Comment


      • #4
        Many thanks for you answer. I would like to store my result in a different dataset to export in excel, something like:
        Country Coef. Std. Err. N
        1 0.4785 0.0589 1148
        4 0.1325 0.2234 2538
        I will have a look at the postfile guide and get back to you. Again, many thanks for your help!

        Comment


        • #5
          I had a look at the postfile command you suggested. I managed to save the bootstrapped standard errors and the number of observations, such as:

          Code:
          egen group = group(country)
          su group, meanonly
          tempname mysim
          postfile `mysim' se obs using results, replace
          forval g = 1/`r(max)' {
              bootstrap r(mean), reps(100) seed(1234): mean_r1r2 if group == `g'
              local se = _se[_bs_1]
              local obs = e(N)
              post `mysim' (`se') (`obs')
          
          }
          postclose `mysim'
          However, I still have a problem. The command return the same observed coefficient _b(_bs_1) for all the countries - the 'average' coefficient that I would get pooling all countries, such as:

          Code:
          bootstrap r(mean), reps(100) seed(1234): mean_r1r2

          The behaviour is different than

          Code:
          levelsof country, local(levels)
          foreach l of local levels {
          preserve
          keep if country == `l'
          bootstrap r(mean), reps(100) seed(1234): mean_r1r2
          restore
          }
          where each country has its own estimated coefficient. Is there a way to obtain them?
          Last edited by Marco Colagrossi; 05 Oct 2018, 04:47.

          Comment


          • #6
            The problem is not with the part of the code you are showing now. The problem is that your program -mean_r1r2- does not honor the -if- condition. You can modify that program to do so as follows:

            Code:
            program mean_r1r2, rclass
                version 14
                syntax [if]
                marksample touse
                corr var1 var2 if `touse'
                local corr12 = r(rho)
                corr var2 var3
                local corr23 = r(rho)
                return scalar mean = (`corr12' + `corr23') /2
            end
            The reason your -bootstrap- was successful before is that you -preserve-d the data, kept only the single group of observations, and then called mean_r1r2, and then -restore-d the data each time through the loop. But that wastes a lot of time thrashing the hard drive. It's better to use -if- conditioning on the command: but then the command has to know how to recognize -if-.

            Comment


            • #7
              Hi
              I am trying to flag repeated courses by student for example

              std_ID course
              1 c1
              1 c2
              1 c1
              2 c2
              2 c3
              2 c4
              3 c1
              3 c2
              3 c2
              3 c3
              3 c1

              a student can repeat more than one course
              would you please help me with that?
              Thank you

              Comment


              • #8
                Hayfa Faisal #7 has nothing to do with the topic of this thread. It is important to keep threads coherent so that people can effectively search the Forum. Please repost this as a New Topic. Also, please read the Forum FAQ before posting. Among the things you will learn there is the preferred way to show example data, that is, by using the -dataex- command, along with instructions about where you will find that command.

                Comment

                Working...
                X