Learn R Programming

animation (version 0.2-0)

flip.coin: Probability in flipping coins

Description

In the first class of learning probability theory, we usually begin with flipping coins or tossing dice. This function provides a simulation to such a process and computes the frequency for `heads' and `tails'.

Usage

flip.coin(faces = 2, prob = NULL, border = "white", grid = "white",
    col = 1:2, type = "p", pch = 21, bg = "transparent",
    digits = 3, control = ani.control(interval = 0.2, nmax = 100),
    ...)

Arguments

faces
an integer or a character vector. See details below.
prob
the probability vector of showing each face. If NULL, each face will be shown in the same probability.
border
The border style for the rectangles which stand for probabilities.
grid
the color for horizontal grid lines in these rectangles
col
The colors to annotate different faces of the `coin'.
type, pch, bg
See points.
digits
integer indicating the precision to be used in the annotation of frequencies in the plot
control
control parameters for the animation; see ani.control
...
other arguments passed to ani.control

Value

  • A list containing
  • freqA vector of frequencies (simulated probabilities)
  • nmaxthe total number of tosses

Details

If faces is a single integer, say 2, a sequence of integers from 1 to faces will be used to denote the faces of a coin; otherwise this character vector just gives the names of each face. When the i-th face shows up, a colored thin rectangle will be added to the corresponding place (the i-th bar), and there will be corresponding annotations for the number of tosses and frequencies. The special argument grid is for consideration of a too large number of flipping, in which case if you still draw horizontal lines in these rectangles, the rectangles will be completely covered by these lines, thus we should specify it as NA. At last the frequency for each face will be computed and shown in the header of the plot -- this shall be close to prob if nmax is large enough.

See Also

ani.start, ani.stop

Examples

Run this code
# a coin would stand on the table?? just kidding :) 
flip.coin(faces = c("Head", "Stand", "Tail"), interval = 0.2, 
    nmax = 50, type = "n", prob = c(0.45, 0.1, 0.45), col =c(1, 2, 4)) 

# HTML animation page
ani.start()
flip.coin(saveANI = TRUE, interval = 0.1, nmax = 30, bg = "yellow",
    width = 600, height = 500) 
ani.stop()

Run the code above in your browser using DataLab