A function to simplify simulations

Share:

Description

A simple function to repeat an expression several times as an aid to simplifying simulations.

Usage

1
pmgRepeatTrials(expr, n = 10)

Arguments

expr

An R expression, such as rnorm(1) or {x <- rnorm(10); t.test(x)\$p.value} that will be repeated n times.

n

Number of times to repeat the expressions. The default is 10.

Details

This functions aids in doing simulations. Rather than explicitly write a for loop or use sapply this function will call sapply on the expression.

A GUI for this appears in pmg under the Simluation tab. The "quick action" will call the function on the results of the simulation.

Value

The output of a sapply call can be a vector, matrix, ... If it is a vector, it is transposed/

Note

This function and GUI was suggested by Daniel Kaplan at useR!2007

Author(s)

John Verzani

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  res <- pmgRepeatTrials(rnorm(1))
  hist(res)

  g = data.frame(
   father = c(78.5, 78.5, 77.5, 76.0, 75.5),
   mother = c(67.0, 68.0, 66.0, 65.5, 62.0),
   sex    = c("M", "M",  "F",  "F",  "M"),
   nkids  = c(4,     4,    1,    2,    5)
  )
  res <- pmgRepeatTrials(coef(lm(father~ sex + sample(nkids),data=g)),100)
  print(res)