Learn R Programming

pmg (version 0.9-43)

pmgRepeatTrials: A function to simplify simulations

Description

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

Usage

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.

Value

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

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.

Examples

Run this code
  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)

Run the code above in your browser using DataLab