Friday, December 12, 2025

Calculating energy utilizing Monte Carlo simulations, half 2: Working your simulation utilizing energy


In my final publish, I confirmed you how one can calculate energy for a t check utilizing Monte Carlo simulations. On this publish, I’ll present you how one can combine your simulations into Stata’s energy command so that you could simply create customized tables and graphs for a spread of parameter values.

Statisticians hardly ever compute energy for a single set of assumptions when planning a scientific research. We usually calculate energy for a spread of parameter values and select a practical set of assumptions that’s financially and logistically possible. For instance, beneath I’ve used energy onemean to calculate energy for pattern sizes starting from 50 to 100 in increments of 10. The desk shows the assumed parameter values, together with the alpha stage, the means beneath the null and different hypotheses, the standardized distinction between the means (delta), the usual deviation, and energy for every pattern measurement.

. energy onemean 70 75, n(50(10)100) sd(15) alpha(0.05)

Estimated energy for a one-sample imply check
t check
Ho: m = m0  versus  Ha: m != m0

  +---------------------------------------------------------+
  |   alpha   energy       N   delta      m0      ma      sd |
  |---------------------------------------------------------|
  |     .05   .6371      50   .3333      70      75      15 |
  |     .05    .719      60   .3333      70      75      15 |
  |     .05   .7852      70   .3333      70      75      15 |
  |     .05   .8377      80   .3333      70      75      15 |
  |     .05   .8786      90   .3333      70      75      15 |
  |     .05     .91     100   .3333      70      75      15 |
  +---------------------------------------------------------+

I’ve additionally used the graph possibility beneath to plot energy over the vary of pattern sizes. I can then use the desk and graph to pick out a pattern measurement that meets the facility necessities for my research.

. energy onemean 70 75, n(50(10)100) sd(15) alpha(0.05) graph

Determine 1: Estimated energy for n = 50 to 100 in increments of 10

Along with pattern measurement, the energy instructions help you enter a spread of values for different parameters corresponding to the usual deviation, the means, or the alpha stage. And energy will create tables and graphs of the outcomes.

It’s also possible to add your individual strategies to the energy suite of instructions. Let’s add the t check simulation program from my final publish to energy to see how this works.

Recall from my final publish that we created a program referred to as simttest to calculate energy for a t check. This system accepts 5 enter parameters, creates a pseudo-random dataset assuming the choice speculation, exams the null speculation, and returns the outcomes of the speculation check.

seize program drop simttest
program simttest, rclass
    model 15.1
    syntax, n(integer)          ///  Pattern measurement
          [ alpha(real 0.05)    ///  Alpha level
            m0(real 0)          ///  Mean under the null
            ma(real 1)          ///  Mean under the alternative
            sd(real 1) ]        //   Normal deviation
    drawnorm y, n(`n') means(`ma') sds(`sd') clear
    ttest y = `m0'
    return scalar reject = (r(p)<`alpha') 
finish

We used simulate to run this system many instances and save the outcomes to a variable named reject.

. simulate reject=r(reject), reps(100) seed(12345):       
>         simttest, n(100) m0(70) ma(75) sd(15) alpha(0.05)

      command:  simttest, n(100) m0(70) ma(75) sd(15) alpha(0.05)
       reject:  r(reject)

Simulations (100)
----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
..................................................    50
..................................................   100

Then, we calculated energy because the proportion of instances that the null speculation was rejected.

. summarize reject

    Variable |        Obs        Imply    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
      reject |        100         .91    .2876235          0          1

. native energy = r(imply)

. show "energy = `energy'"
energy = .91

You may add this simulation methodology to energy by making a program named power_cmd_mymethod, the place mymethod is the title of the facility command. Let’s name this system power_cmd_simttest.

The code block beneath defines power_cmd_simttest. Word how related it’s to our simttest program. It begins with seize program drop, then program, and model 15.1. Subsequent, we outline the enter parameters utilizing syntax simply as we did in simttest. Right here I’ve added a brand new enter parameter referred to as reps(), which is the variety of repetitions for the simulation.

seize program drop power_cmd_simttest
program power_cmd_simttest, rclass
    model 15.1
        
    // DEFINE THE INPUT PARAMETERS AND THEIR DEFAULT VALUES
    syntax, n(integer)          ///  Pattern measurement
          [ alpha(real 0.05)    ///  Alpha level
            m0(real 0)          ///  Mean under the null
            ma(real 1)          ///  Mean under the alternative
            sd(real 1)          ///  Standard deviation
            reps(integer 100)]  //   Variety of repetitions
                          
    // GENERATE THE RANDOM DATA AND TEST THE NULL HYPOTHESIS
    quietly simulate reject=r(reject), reps(`reps'):    ///
                     simttest, n(`n') m0(`m0') ma(`ma') sd(`sd') alpha(`alpha')
    quietly summarize reject
        
    // RETURN RESULTS
    return scalar energy = r(imply)
    return scalar N = `n'
    return scalar alpha = `alpha'
    return scalar m0 = `m0'
    return scalar ma = `ma'
    return scalar sd = `sd'
finish

The center part of this system runs the simulation and summarizes the outcomes. Each simulate and summarize are preceded by quietly, which suppresses the show of their output. Right here simulate runs this system simttest and saves the outcomes to the variable reject simply because it did earlier than. Word that every one the enter parameters in each simulate and simttest are native macros outlined with syntax. summarize calculates the imply of reject and shops it within the scalar r(imply).

The underside part of the code block returns energy and the opposite parameters. The scalar energy returns the imply of the variable reject, and the opposite parameters are merely native macros handed via from syntax.

Now you possibly can run the simulation by typing energy simttest.

. energy simttest, n(100) m0(70) ma(75) sd(15)

Estimated energy
Two-sided check

  +-------------------------------------------------+
  |   alpha   energy       N      m0      ma      sd |
  |-------------------------------------------------|
  |     .05     .91     100      70      75      15 |
  +-------------------------------------------------+

It labored! You may even make a desk and graph for a spread of pattern sizes.

. energy simttest, n(50(10)100) m0(70) ma(75) sd(15) desk graph

Estimated energy
Two-sided check

  +-------------------------------------------------+
  |   alpha   energy       N      m0      ma      sd |
  |-------------------------------------------------|
  |     .05     .67      50      70      75      15 |
  |     .05     .73      60      70      75      15 |
  |     .05     .73      70      70      75      15 |
  |     .05     .77      80      70      75      15 |
  |     .05     .85      90      70      75      15 |
  |     .05     .91     100      70      75      15 |
  +-------------------------------------------------+

Determine 2: Simulated energy for n = 50 to 100 in increments of 10
graph1

You may cease right here when you wished to think about solely a spread of pattern sizes. However you will want to jot down yet another small program when you want to enter a spread of values for the opposite parameters corresponding to m0, ma, and sd. This system have to be named power_cmd_mymethod_init, so we’ll title our program power_cmd_simttest_init.

The code block beneath defines power_cmd_simttest_init and begins with seize program drop and program simply as our different applications. Word that this system definition begins with the choice sclass. The road sreturn native pss_colnames initializes columns within the output desk for the parameters listed in double quotes. The road sreturn native pss_numopts lets you specify numlists for the parameters positioned in double quotes.

seize program drop power_cmd_simttest_init
program power_cmd_simttest_init, sclass
    sreturn native pss_colnames "m0 ma sd"
    sreturn native pss_numopts  "m0 ma sd"
finish

Now you should utilize energy simttest to calculate energy for a spread of means assuming totally different different hypotheses. You may even do that for various pattern sizes.

. energy simttest, n(75 100) m0(70) ma(72(1)75) sd(15) reps(1000)

Estimated energy
Two-sided check

  +-------------------------------------------------+
  |   alpha   energy       N      m0      ma      sd |
  |-------------------------------------------------|
  |     .05    .212      75      70      72      15 |
  |     .05    .411      75      70      73      15 |
  |     .05    .611      75      70      74      15 |
  |     .05    .789      75      70      75      15 |
  |     .05    .265     100      70      72      15 |
  |     .05     .51     100      70      73      15 |
  |     .05    .761     100      70      74      15 |
  |     .05    .905     100      70      75      15 |
  +-------------------------------------------------+

And you may plot the outcomes of your energy evaluation by specifying xdimension within the graph possibility.

energy simttest, n(75 100) m0(70) ma(72(1)75) sd(15) reps(1000) ///
   graph(xdimension(ma))

Determine 3: Simulated energy for ma=72(1)75 given n=75 and n=100
graph1

Thus far I’ve proven you how one can calculate energy utilizing Monte Carlo simulations and how one can combine these simulations into energy. I began with a easy t check instance in order that we might deal with the programming and verify our work with energy onemean. Subsequent time, I’ll present you how one can write energy instructions to calculate energy for linear and logistic regression fashions utilizing Monte Carlo simulations. See [PSS] energy usermethod for extra details about including your individual strategies to energy.



Related Articles

Latest Articles