library(dplyr)
library(ggplot2)
library(flextable)
SOLUCION A
enf <- c("I1", "I2", "I3")
p_enf <- c(0.01, 0.005, 0.02)
p_h_enf <- c(0.90, 0.95, 0.75)
df_enf <- data.frame(
enfermedad = enf,
P_enf = p_enf,
P_h_enf = p_h_enf,
stringsAsFactors = FALSE
)
knitr::kable(df_enf, digits = 4, caption = "Resumen")
| enfermedad | P_enf | P_h_enf |
|---|---|---|
| I1 | 0.010 | 0.90 |
| I2 | 0.005 | 0.95 |
| I3 | 0.020 | 0.75 |
SOLUCION B
P_H <- sum(df_enf$P_enf * df_enf$P_h_enf)
P_I1_enf_h <- df_enf$P_enf[1] * df_enf$P_h_enf[1] / P_H
df_enf$Probabilidad <- (df_enf$P_enf * df_enf$P_h_enf) / P_H
knitr::kable(df_enf, digits = 4, caption = "Probabilidad (I1, I2, I3 | H)")
| enfermedad | P_enf | P_h_enf | Probabilidad |
|---|---|---|---|
| I1 | 0.010 | 0.90 | 0.3130 |
| I2 | 0.005 | 0.95 | 0.1652 |
| I3 | 0.020 | 0.75 | 0.5217 |
De acuerdo a estpo, podremos eobservar que la probabilidad de qie I1 presente H sintomas es 0,323
x <- c("1", "2", "3", "4")
fx<- c(0.4, 0.3, 0.2, 0.1)
Df_X <- data.frame(
x = x,
fx = fx,
stringsAsFactors = FALSE
)
knitr::kable(Df_X, digits = 2, caption = "Tabla")
| x | fx |
|---|---|
| 1 | 0.4 |
| 2 | 0.3 |
| 3 | 0.2 |
| 4 | 0.1 |
SOLUCION A
ggplot(Df_X, aes(x =factor(x), y = fx))+
geom_col(fill = "steelblue")+
geom_text(aes(label = fx), vjust = -0.5)+
labs(title = "Funcion de probabilidad f(x)",
x = "x",
y = "f(x)")+
theme_minimal()
SOLUCION B
Df_X$Fx <- cumsum(Df_X$fx)
knitr::kable(Df_X, digits = 2, caption = "Función de distribución acumulada F(x)")
| x | fx | Fx |
|---|---|---|
| 1 | 0.4 | 0.4 |
| 2 | 0.3 | 0.7 |
| 3 | 0.2 | 0.9 |
| 4 | 0.1 | 1.0 |
De acuerdo a esto encontramos que F(2.5) = 0.4 + 0.3 = 0.7.
\[ f(x) = \begin{cases} Kx(1 - X), & 0 <= x <= 1, \\[6pt] 0, & \text{en otro caso.} \end{cases} \]
SOLUCION A
k <- 1 / integrate(function(x) x * (1 - x), lower = 0, upper = 1)$value
f.x <- function(x) {
ifelse(x >= 0 & x <= 1, k * x * (1 - x), 0)
}
x_vals <- seq(-0.1, 1.1, length.out = 1000)
df_fx <- data.frame(x = x_vals, fx = f.x(x_vals))
ggplot(df_fx, aes(x = x, y = fx)) +
geom_line(size = 1.2, color = "steelblue") +
geom_area(data = subset(df_fx, x >= 0 & x <= 1), aes(x = x, y = fx),
fill = "steelblue", alpha = 0.3) +
labs(title = "Función de densidad f(x)",
x = "x",
y = "f(x)") +
theme_minimal()
SOLUCION B
F.x <- function(x) {
ifelse(x < 0, 0,
ifelse(x <= 1, integrate(f.x, lower = 0, upper = x)$value, 1))
}
df_Fx <- data.frame(x = x_vals, Fx = sapply(x_vals, F.x))
ggplot(df_Fx, aes(x = x, y = Fx)) +
geom_line(size = 1.2, color = "steelblue") +
labs(title = "Función de distribución acumulada F(x)",
x = "x",
y = "F(x)") +
theme_minimal()
SOLUCION C
prob <- integrate(f.x, lower = 0.4, upper = 0.8)$value
round(prob, 3)
## [1] 0.544
x <- c(250, 150, 0, -150)
p <- c(0.22, 0.36, 0.28, 0.14)
sum(p)
## [1] 1
E_ <- sum(x * p)
cat("Valor esperado =", E_, "\n")
## Valor esperado = 88
E_XX <- sum((x^2) * p)
V_X <- E_XX - (E_XX)^2
cat("Varianza =", V_X, "\n")
## Varianza = -624975000
D_X <- sqrt(V_X)
cat("Desviacion estandar =", round(D_X, 2))
## Desviacion estandar = NaN
\[ f(x) = \begin{cases} \dfrac{2(x + 2)}{5}, & 0 < x < 1, \\[8pt] 0, & \text{en otro caso.} \end{cases} \]
f.x <- function(x){
ifelse(x <= 0 | x >= 1, 0,
2 * (x + 2) / 5)
}
a_t <- integrate(f.x, lower = 0, upper = 1)$value
esp <- integrate(function(x) x * f.x(x), lower = 0, upper = 1)$value
a_t
## [1] 1
esp
## [1] 0.5333333
cat("E[X] = ", round(esp, 6), " = 8/15 ≈ ", round(esp, 6), "\n")
## E[X] = 0.533333 = 8/15 ≈ 0.533333
x_den <- seq(-0.1, 1.1, length.out = 1001)
df <- data.frame(x = x_den, fx = f.x(x_den))
ggplot(df, aes(x = x, y = fx)) +
geom_line(size = 1.2, color = "steelblue") +
geom_area(data = subset(df, x > 0 & x < 1), aes(x = x, y = fx),
fill = "steelblue", alpha = 0.25) +
labs(title = expression(paste("Densidad ", f(x) == frac(2*(x+2),5), " en (0,1)")),
x = "x (proporción que responde)",
y = "f(x)") +
theme_minimal(base_size = 13) +
coord_cartesian(xlim = c(-0.05, 1.05))
F <- function(y) ifelse(y < 0, 0, 1 - exp(-y^2))
curve(F, from = -1, to = 3, col = "steelblue", lwd = 2,
main = "Funcion de Distribucion F(y)",
ylab = "F(y)", xlab = "y")
f <- function(y) ifelse(y >= 0, 2*y*exp(-y^2), 0)
curve(f, from = 0, to = 3, col = "pink", lwd = 2,
main = "Funcion de Densidad f(y)",
ylab = "f(y)", xlab = "y")
P <- 1 - F(2)
cat("P(Y ≥ 2) =", round(P, 4), "\n")
## P(Y ≥ 2) = 0.0183