knitr::opts_chunk$set(collapse = T, comment = "#>")
library(cointoss)

Coin Tossing

The package "cointoss" is a minimal implementation for simulating tossing a coin a given number of times, and to visualize the relative frequencies in such a series of tosses.

Creating a coin

The first step is to create a "coin" object with the function coin():

mycoin <- coin()
mycoin

By default, coin() creates a fair coin with standard sides heads, and tails, and with probability values for each side of 0.5.

You can change the defatuls with arguments sides and prob:

loaded <- coin(sides = c('HEADS', 'tails'), prob = c(0.8, 0.2))
loaded

Tossing a coin

Once you have defined a "coin" object, you can use the function toss() to toss a coin a given number of times. The output will be an object of class "toss", which will contain the vector of tosses:

toss100 <- toss(mycoin, times = 100)
toss100

The summary() function gives you a nice table of frequencies:

summary(toss100)

Plot of relative frequencies

You can use the function plot() to create a graphic with the relative frequencies of a series of coin tosses:

plot(toss100)


gastonstat/cointoss documentation built on May 5, 2023, 6:46 p.m.