##### Lesson 39: Discrete distributions in R: Part I ##### # Setting the working directory setwd(“provide the path to your folder“) list.files() # Read the data to the workspace # parking_violations = read.table("parking_data.txt",header=T) # Create a Date Series # days = seq(from=as.Date('2017/1/1'), to=as.Date('2017/6/30'), by="day") y = as.numeric(format(days,"%y")) m = as.numeric(format(days,"%m")) d = as.numeric(format(days,"%d")) binary_code = matrix(0,nrow=length(m),ncol=1) for (i in 1:nrow(parking_violations)) { dummy = which(m == parking_violations[i,1] & d == parking_violations[i,2]) binary_code[dummy,1] = 1 } plot(days,binary_code,type="h",font=2,font.lab=2,xlab="Days",ylab="(0,1)") #### Bernoulli Trials #### # The generalized Case # p = 0.5 # probability of success -- user defined rbinom(1,1,p) # create 1 random Bernoulli trial rbinom(10,1,p) # create 10 random Bernoulli trials rbinom(100,1,p) # create 100 random Bernoulli trials # For the example case # n = 1 # it is the number of trials p = 0.15 # probability of the event nobs = 181 rbinom(nobs,n,p) ######## Animation ######### # Create png files for Bernoulli sequence # library(animation) png(file="bernoulli%02d.png", width=600, height=300) for (i in 1:5) { plot(rbinom(nobs,n,p),type="h",font=2,font.lab=2,xlab="Days",ylab="(0,1)") } dev.off() # Combine the png files saved in the folder into a GIF # library(magick) bernoulli_png1 <- image_read("bernoulli01.png","x150") bernoulli_png2 <- image_read("bernoulli02.png","x150") bernoulli_png3 <- image_read("bernoulli03.png","x150") bernoulli_png4 <- image_read("bernoulli04.png","x150") bernoulli_png5 <- image_read("bernoulli05.png","x150") frames <- image_morph(c(bernoulli_png1, bernoulli_png2, bernoulli_png3, bernoulli_png4, bernoulli_png5), frames = 10) animation <- image_animate(frames) image_write(animation, "bernoulli.gif") ########## Binomial Distribution ###### px = dbinom(20,181,p) n = 181 # define the number of trials p = 0.15 # probability of the event x = 0:181 # number of successes varying from 0 to 181 px = dbinom(x,n,p) plot(x,px,type="h",xlab="Random Variable X (Number of tickets in 181 days)",ylab="Probability P(X=k)",font=2,font.lab=2) n = 30 # define the number of trials p = 0.15 # probability of the event x = 5:30 # number of successes varying from 5 to 30 px = dbinom(x,n,p) sum(px) ######## Animation ######### # Create png files for Binomial distribution # png(file="binomial%02d.png", width=600, height=300) n = 181 x = 0:181 p = 0.1 for (i in 1:5) { px = dbinom(x,n,p) plot(x,px,type="h",xlab="Random Variable X (Number of tickets in 181 days)",ylab="Probability P(X=k)",font=2,font.lab=2) txt = paste("p=",p,sep="") text(150,0.04,txt,col="red",cex=2) p = p+0.2 } dev.off() # Combine the png files saved in the folder into a GIF # library(magick) binomial_png1 <- image_read("binomial01.png","x150") binomial_png2 <- image_read("binomial02.png","x150") binomial_png3 <- image_read("binomial03.png","x150") binomial_png4 <- image_read("binomial04.png","x150") binomial_png5 <- image_read("binomial05.png","x150") frames <- image_morph(c(binomial_png1, binomial_png2, binomial_png3, binomial_png4, binomial_png5), frames = 15) animation <- image_animate(frames) image_write(animation, "binomial.gif")