@@ -42,19 +42,19 @@ vdist_binom_plot <- function(n = 10, p = 0.3, print_plot = TRUE) {
4242 xn <- n / 40
4343 bm <- round(n * p , 2 )
4444 bsd <- round(sqrt((1 - p ) * bm ) , 2 )
45- data <- stats :: dbinom(x , n , p )
45+ data <- dbinom(x , n , p )
4646
4747 plot_data <- data.frame (n = seq(0 , n ), df = data )
4848
4949 pp <-
50- ggplot2 :: ggplot(plot_data ) +
51- ggplot2 :: geom_col(ggplot2 :: aes(x = n , y = df ), fill = " blue" ) +
52- ggplot2 :: ylab(" Probability" ) + ggplot2 :: xlab(" No. of success" ) +
53- ggplot2 :: ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
50+ ggplot(plot_data ) +
51+ geom_col(aes(x = n , y = df ), fill = " blue" ) +
52+ ylab(" Probability" ) + xlab(" No. of success" ) +
53+ ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
5454 subtitle = paste(" Mean =" , bm , " , Std. Dev. =" , bsd )) +
55- ggplot2 :: theme(plot.title = ggplot2 :: element_text(hjust = 0.5 ),
56- plot.subtitle = ggplot2 :: element_text(hjust = 0.5 )) +
57- ggplot2 :: scale_x_continuous(breaks = seq(0 , n ))
55+ theme(plot.title = element_text(hjust = 0.5 ),
56+ plot.subtitle = element_text(hjust = 0.5 )) +
57+ scale_x_continuous(breaks = seq(0 , n ))
5858
5959 if (print_plot ) {
6060 print(pp )
@@ -95,49 +95,49 @@ vdist_binom_prob <- function(n = 10, p = 0.3, s = 4,
9595 bsd <- round(sqrt((1 - p ) * bm ), 2 )
9696
9797 if (method == " lower" ) {
98- k <- round(stats :: pbinom(s , n , p ), 3 )
99- cols <- ifelse(cumsum(round(stats :: dbinom(x , n , p ), 3 )) < = k , " #0000CD" , " #6495ED" )
98+ k <- round(pbinom(s , n , p ), 3 )
99+ cols <- ifelse(cumsum(round(dbinom(x , n , p ), 3 )) < = k , " #0000CD" , " #6495ED" )
100100 } else if (method == " upper" ) {
101- k <- round(1 - stats :: pbinom((s - 1 ), n , p ), 3 )
102- cols <- ifelse(cumsum(round(stats :: dbinom(x , n , p ), 3 )) > = k , " #0000CD" , " #6495ED" )
101+ k <- round(1 - pbinom((s - 1 ), n , p ), 3 )
102+ cols <- ifelse(cumsum(round(dbinom(x , n , p ), 3 )) > = k , " #0000CD" , " #6495ED" )
103103 } else if (method == " exact" ) {
104- k <- stats :: pbinom(s , n , p ) - stats :: pbinom((s - 1 ), n , p )
105- cols <- ifelse(round(stats :: dbinom(x , n , p ), 5 ) == round(k , 5 ), " #0000CD" , " #6495ED" )
104+ k <- pbinom(s , n , p ) - pbinom((s - 1 ), n , p )
105+ cols <- ifelse(round(dbinom(x , n , p ), 5 ) == round(k , 5 ), " #0000CD" , " #6495ED" )
106106 } else {
107- k1 <- stats :: pbinom((s [1 ] - 1 ), n , p )
108- k2 <- stats :: pbinom(s [2 ], n , p )
109- k <- stats :: pbinom(s [2 ], n , p ) - stats :: pbinom((s [1 ] - 1 ), n , p )
110- cols <- ifelse((round(cumsum(stats :: dbinom(x , n , p )), 6 ) > round(k1 , 6 ) &
111- round(cumsum(stats :: dbinom(x , n , p )), 6 ) < = round(k2 , 6 )), " #0000CD" , " #6495ED" )
107+ k1 <- pbinom((s [1 ] - 1 ), n , p )
108+ k2 <- pbinom(s [2 ], n , p )
109+ k <- pbinom(s [2 ], n , p ) - pbinom((s [1 ] - 1 ), n , p )
110+ cols <- ifelse((round(cumsum(dbinom(x , n , p )), 6 ) > round(k1 , 6 ) &
111+ round(cumsum(dbinom(x , n , p )), 6 ) < = round(k2 , 6 )), " #0000CD" , " #6495ED" )
112112 }
113113
114- data <- stats :: dbinom(x , n , p )
114+ data <- dbinom(x , n , p )
115115 plot_data <- data.frame (n = seq(0 , n ), df = data )
116116
117117 pp <-
118- ggplot2 :: ggplot(plot_data ) +
119- ggplot2 :: geom_col(ggplot2 :: aes(x = n , y = df ), fill = cols ) +
120- ggplot2 :: ylab(" Probability" ) +
121- ggplot2 :: xlab(paste(" No. of success\n " , " Mean =" , bm , " , Std. Dev. =" , bsd )) +
122- ggplot2 :: scale_x_continuous(breaks = seq(0 , n )) +
123- ggplot2 :: theme(plot.title = ggplot2 :: element_text(hjust = 0.5 ),
124- plot.subtitle = ggplot2 :: element_text(hjust = 0.5 ))
118+ ggplot(plot_data ) +
119+ geom_col(aes(x = n , y = df ), fill = cols ) +
120+ ylab(" Probability" ) +
121+ xlab(paste(" No. of success\n " , " Mean =" , bm , " , Std. Dev. =" , bsd )) +
122+ scale_x_continuous(breaks = seq(0 , n )) +
123+ theme(plot.title = element_text(hjust = 0.5 ),
124+ plot.subtitle = element_text(hjust = 0.5 ))
125125
126126 if (method == " lower" ) {
127127 pp +
128- ggplot2 :: ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
128+ ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
129129 subtitle = paste(" P(X) <=" , s , " =" , round(k , 3 )))
130130 } else if (method == " upper" ) {
131131 pp +
132- ggplot2 :: ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
132+ ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
133133 subtitle = paste(" P(X) >=" , s , " =" , round(k , 3 )))
134134 } else if (method == " exact" ) {
135135 pp +
136- ggplot2 :: ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
136+ ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
137137 subtitle = paste(" P(X) =" , s , " =" , round(k , 3 )))
138138 } else {
139139 pp +
140- ggplot2 :: ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
140+ ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
141141 subtitle = paste0(" P(" , s [1 ], " <= X <= " , s [2 ], " )" , " = " , round(k , 3 )))
142142 }
143143
@@ -166,33 +166,33 @@ vdist_binom_perc <- function(n = 10, p = 0.5, tp = 0.05, type = c("lower", "uppe
166166 x <- seq(0 , n , 1 )
167167
168168 if (method == " lower" ) {
169- k <- round(stats :: qbinom(tp , n , p ), 3 )
170- cols <- ifelse(cumsum(stats :: dbinom(x , n , p )) < = stats :: pbinom(k , n , p ), " #0000CD" , " #6495ED" )
169+ k <- round(qbinom(tp , n , p ), 3 )
170+ cols <- ifelse(cumsum(dbinom(x , n , p )) < = pbinom(k , n , p ), " #0000CD" , " #6495ED" )
171171 } else {
172- k <- round(stats :: qbinom(tp , n , p , lower.tail = F ), 3 )
173- cols <- ifelse(cumsum(stats :: dbinom(x , n , p )) > stats :: pbinom((k + 1 ), n , p ), " #0000CD" , " #6495ED" )
172+ k <- round(qbinom(tp , n , p , lower.tail = F ), 3 )
173+ cols <- ifelse(cumsum(dbinom(x , n , p )) > pbinom((k + 1 ), n , p ), " #0000CD" , " #6495ED" )
174174 }
175175
176- data <- stats :: dbinom(x , n , p )
176+ data <- dbinom(x , n , p )
177177 plot_data <- data.frame (n = seq(0 , n ), df = data )
178178
179179 pp <-
180- ggplot2 :: ggplot(plot_data ) +
181- ggplot2 :: geom_col(ggplot2 :: aes(x = n , y = df ), fill = cols ) +
182- ggplot2 :: ylab(" Probability" ) + ggplot2 :: xlab(" No. of success" ) +
183- ggplot2 :: scale_x_continuous(breaks = seq(0 , n )) +
184- ggplot2 :: theme(plot.title = ggplot2 :: element_text(hjust = 0.5 ),
185- plot.subtitle = ggplot2 :: element_text(hjust = 0.5 ))
180+ ggplot(plot_data ) +
181+ geom_col(aes(x = n , y = df ), fill = cols ) +
182+ ylab(" Probability" ) + xlab(" No. of success" ) +
183+ scale_x_continuous(breaks = seq(0 , n )) +
184+ theme(plot.title = element_text(hjust = 0.5 ),
185+ plot.subtitle = element_text(hjust = 0.5 ))
186186
187187
188188 if (method == " lower" ) {
189189 pp +
190- ggplot2 :: ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
190+ ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
191191 subtitle = paste0(" P(X <= " , k , " ) <= " , tp , " , but P(X <= " , (k + 1 ),
192192 " ) > " , tp ))
193193 } else {
194194 pp +
195- ggplot2 :: ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
195+ ggtitle(label = paste(" Binomial Distribution: n =" , n , " , p =" , p ),
196196 subtitle = paste0(" P(X >= " , (k + 1 ), " ) <= " , tp , " , but P(X >= " , k ,
197197 " ) > " , tp ))
198198 }
0 commit comments