R - Coin toss

Code

library(ggplot2)
library(gridExtra)
set.seed(1)
coin <- c(0,1)

flipCoin <- function(num_flips=1000) {
  sample(coin, size=num_flips, replace=TRUE)
}

flipEstimate <- function(flips) {
  sum(flips==1) / length(flips)
}

plotFlips <- function(coinflips) {
  qplot(
    1:length(coinflips),
    cumsum(coinflips==1) / (1:length(coinflips)),
    geom='line',
    ylim=coin, 
    xlab = '# of flips',
    ylab = 'Freq coinflip == 1'
  )
}
g1 <- plotFlips(flipCoin(10))
g2 <- plotFlips(flipCoin(100))
g3 <- plotFlips(flipCoin(1000))
g4 <- plotFlips(flipCoin(10000))

grid.arrange(g1, g2, g3, g4, ncol=2)

Plot

Coin toss plot

Read more...

R - plotting notes

Setup library(ggplot2) library(gridExtra) Distribution size <- 100 vect <- seq(1, size, by=1) m <- mean(vect) std <- sqrt(var(vect)) distF <- data.frame( indx = vect, gaus = dnorm(vect, mean=m, sd=std), pois = dpois(vect, vect), unif = dunif(vect, min=1, max=size) ) g1 <- ggplot(data=distF, aes(x=indx, y=gaus)) + geom_point(shape=1) + guides(legend.title=element_blank()) g2 <- ggplot(data=distF, aes(x=indx, y=pois)) + geom_point(shape=1) g3 <- ggplot(data=distF, aes(x=indx, y=unif)) + geom_point(shape=1) grid.arrange(g1, g2, g3, ncol=3)` Random sampling randF <- data.
Read more...