library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
lets create 100 numbers due to 0.1 probability and 100 trials ( suppose that we make a lottery trial ) with binomial dist
numbers <- rbinom(100, 100, 0.1)
numbers
## [1] 8 12 13 12 14 14 12 8 11 17 8 11 8 9 11 9 11 15 8 6 8 11 6
## [24] 10 9 11 8 14 10 11 14 12 12 11 17 6 9 10 14 13 10 8 12 9 8 9
## [47] 13 8 7 10 12 10 7 8 15 11 8 15 7 11 11 4 17 6 15 10 13 8 8
## [70] 4 11 10 8 13 11 8 10 13 11 10 9 15 11 9 13 11 9 7 10 5 9 7
## [93] 10 7 10 8 10 9 13 12
function to calculate probabilities of each value due to binomial distribution
binomial_dist <- function(n,p,x) {
temp_C <- (factorial(n) / (factorial(x) * factorial(n-x)))*(p^x)*((1-p)^(n-x))
return(temp_C)
}
an example
binomial_dist(100,0.1,2)
## [1] 0.001623197
lets calculate each of our observations binomial probabilities with our function
binom_df<-NULL
for (i in 1:100){
binom_df[i] <- binomial_dist(100,0.1,numbers[i])
}
make it data frame
full_df <- data.frame(numbers,binom_df)
head(full_df)
## numbers binom_df
## 1 8 0.11482303
## 2 12 0.09878801
## 3 13 0.07430209
## 4 12 0.09878801
## 5 14 0.05130383
## 6 14 0.05130383
and plot all of the probabilities !
plot(numbers,binom_df,type="h",main="Binom Distribution",ylab="Binomial Distributions of Values", xlab = "Number of Trials",col="blue")
So as expected, we created 100 treatments with 0.1 occuring probability and as seen in the graph, the probability gets top point between 9 and 11 treatments