Simular probabilidades binomiales
Imaginemos que un 80% de personas en el mundo ha visto el partido de la final del último mundial de fútbol. Tras el evento, 4 amigos se reúnen a conversar,
library(gtools)
library(ggplot2)
library(plotly)
p <- 0.80
q <- 0.20
n <- 4 # Tamaño de la muestra o total de la muestra
x <- "?" # Cualquier valor entre 1 y 4 puede ser para uno, dos , tres o cuatro, incluyendo el cero.
\[ P(x)=\binom{n}{x}\cdot p^x\cdot(1-p)^{n-x} \]
x <- 3
combinaciones <- combinations(n = n, r = x, v = 1:4)
n.combinaciones <- nrow(combinaciones)
combinaciones
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 1 2 4
## [3,] 1 3 4
## [4,] 2 3 4
n.combinaciones
## [1] 4
p.binom.x <- n.combinaciones * (p^x) * (1-p)^(n-x)
p.binom.x * 100
## [1] 40.96
p.binom.x3 <- dbinom(x = x, size = n, prob = p)
p.binom.x3
## [1] 0.4096
\[ P(x)=\binom{n}{x}\cdot p^x\cdot(1-p)^{n-x} \]
x=2
combinaciones <- combinations(n = n, r = x, v = 1:4)
n.combinaciones <- nrow(combinaciones)
combinaciones
## [,1] [,2]
## [1,] 1 2
## [2,] 1 3
## [3,] 1 4
## [4,] 2 3
## [5,] 2 4
## [6,] 3 4
n.combinaciones
## [1] 6
p.binom.x <- n.combinaciones * (p^x) * (1-p)^(n-x)
p.binom.x
## [1] 0.1536
p.binom.x2 <- dbinom(x = x, size = n, prob = p)
p.binom.x2
## [1] 0.1536
tabla <- data.frame(x=0:n, p.binom = dbinom(x = 0:n, size = n, prob = p), p.acum = pbinom(q = 0:4, size = n, prob = p))
tabla
## x p.binom p.acum
## 1 0 0.0016 0.0016
## 2 1 0.0256 0.0272
## 3 2 0.1536 0.1808
## 4 3 0.4096 0.5904
## 5 4 0.4096 1.0000
ggplot(data = tabla, mapping = aes(x = x, y = p.binom)) +
geom_bar(stat = "identity")
figura <- plot_ly(
x = tabla$x,
y = tabla$p.binom,
type = "bar",
color = "blue"
)
figura <- figura %>% layout(title = "Probabilidad binomial desde 0 a 4 con 40% Exito",
xaxis = list(title = "x"),
yaxis = list (title = "Probabilidad"))
figura
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels