R Markdown

library(ggplot2)
data("quakes")
str(quakes)
## 'data.frame':    1000 obs. of  5 variables:
##  $ lat     : num  -20.4 -20.6 -26 -18 -20.4 ...
##  $ long    : num  182 181 184 182 182 ...
##  $ depth   : int  562 650 42 626 649 195 82 194 211 622 ...
##  $ mag     : num  4.8 4.2 5.4 4.1 4 4 4.8 4.4 4.7 4.3 ...
##  $ stations: int  41 15 43 19 11 12 43 15 35 19 ...
summary(quakes$mag)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    4.00    4.30    4.60    4.62    4.90    6.40
N <- nrow(quakes);N
## [1] 1000
n <- 150

Including Plots

set.seed(456)
muestra <- quakes[sample(1:N, n, replace = FALSE), ]
head(muestra)
##        lat   long depth mag stations
## 749 -22.14 180.64   591 4.5       18
## 485 -17.90 181.30   593 4.1       13
## 931 -17.63 185.13   219 4.5       28
## 550 -21.16 181.41   543 4.3       17
## 469 -17.68 181.11   568 4.4       22
## 252 -14.85 167.24    97 4.5       26
media_muestral <- mean(muestra$mag)
media_muestral
## [1] 4.642667
s_muestral <- sd(muestra$mag)
n <- 150

# Error estándar clásico

EE <- s_muestral / sqrt(n)
EE
## [1] 0.03211203
z <- qnorm(0.95)  # 90% --> 5% cola superior
IC_inf <- media_muestral - z * EE
IC_sup <- media_muestral + z * EE

c(IC_inf, IC_sup)
## [1] 4.589847 4.695486
# Definición

quakes$grave <- ifelse(quakes$mag >= 5, 1, 0)
muestra$grave <- ifelse(muestra$mag >= 5, 1, 0)

# Poblacional

prop_poblacional <- mean(quakes$grave)
prop_poblacional
## [1] 0.198
# Muestral

prop_muestral <- mean(muestra$grave)
prop_muestral
## [1] 0.2266667
EE_prop <- sqrt(prop_muestral * (1 - prop_muestral) / n)
EE_prop
## [1] 0.03418468
ggplot(quakes, aes(mag)) +
geom_histogram(bins = 30, fill = "steelblue") +
theme_minimal() +
ggtitle("Distribución de Magnitudes — Población quakes")

list(
media_muestral = media_muestral,
error_estandar_media = EE,
intervalo_90 = c(IC_inf, IC_sup),
proporcion_poblacional_graves = prop_poblacional,
proporcion_muestral_graves = prop_muestral,
error_estandar_proporcion = EE_prop
)
## $media_muestral
## [1] 4.642667
## 
## $error_estandar_media
## [1] 0.03211203
## 
## $intervalo_90
## [1] 4.589847 4.695486
## 
## $proporcion_poblacional_graves
## [1] 0.198
## 
## $proporcion_muestral_graves
## [1] 0.2266667
## 
## $error_estandar_proporcion
## [1] 0.03418468