Datos: Definición formal de las muestras, variables, tamaños muestrales y niveles de confianza
# ============================================================
# 1. Intervalo Z para una media con sigma conocida
# ============================================================
ic_media_z <- function(media, sigma, n, conf = 0.95,
digitos = 4) {
alpha <- 1 - conf
z <- qnorm(1 - alpha / 2)
error_estandar <- sigma / sqrt(n)
margen_error <- z * error_estandar
inferior <- media - margen_error
superior <- media + margen_error
resultado <- data.frame(
Medida = c(
"Media",
"\u03c3",
"Error estándar",
"n",
"Grado de confianza",
"Valor crítico",
"Margen de error",
"Extremo inferior",
"Extremo superior",
"Intervalo"
),
Resultado = c(
round(media, digitos),
round(sigma, digitos),
round(error_estandar, digitos),
n,
round(conf, digitos),
round(z, digitos),
round(margen_error, digitos),
round(inferior, digitos),
round(superior, digitos),
paste0(
round(media, digitos),
" ± ",
round(margen_error, digitos)
)
),
check.names = FALSE
)
knitr::kable(
resultado,
format = "html",
col.names = c("Medida", "Valor"),
align = c("l", "r")
)
}
# ============================================================
# 2. Intervalo t para una media con sigma desconocida
# ============================================================
ic_media_t <- function(media, s, n, conf = 0.95,
digitos = 4) {
alpha <- 1 - conf
gl <- n - 1
t_critico <- qt(1 - alpha / 2, df = gl)
error_estandar <- s / sqrt(n)
margen_error <- t_critico * error_estandar
inferior <- media - margen_error
superior <- media + margen_error
resultado <- data.frame(
Medida = c(
"Media",
"s",
"Error estándar",
"n",
"Grados de libertad",
"Grado de confianza",
"Valor crítico",
"Margen de error",
"Extremo inferior",
"Extremo superior",
"Intervalo"
),
Resultado = c(
round(media, digitos),
round(s, digitos),
round(error_estandar, digitos),
n,
gl,
round(conf, digitos),
round(t_critico, digitos),
round(margen_error, digitos),
round(inferior, digitos),
round(superior, digitos),
paste0(
round(media, digitos),
" ± ",
round(margen_error, digitos)
)
),
check.names = FALSE
)
knitr::kable(
resultado,
format = "html",
col.names = c("Medida", "Valor"),
align = c("l", "r")
)
}
# ============================================================
# 3. Intervalo Z para una proporción
# ============================================================
ic_proporcion <- function(exitos, n, conf = 0.95,
digitos = 4) {
if (exitos < 0 || exitos > n) {
stop("El número de éxitos debe estar entre 0 y n.")
}
alpha <- 1 - conf
z <- qnorm(1 - alpha / 2)
proporcion <- exitos / n
error_estandar <- sqrt(proporcion * (1 - proporcion) / n)
margen_error <- z * error_estandar
inferior <- max(0, proporcion - margen_error)
superior <- min(1, proporcion + margen_error)
resultado <- data.frame(
Medida = c(
"Éxitos",
"n",
"Proporción",
"Error estándar",
"Grado de confianza",
"Valor crítico",
"Margen de error",
"Extremo inferior",
"Extremo superior",
"Intervalo"
),
Resultado = c(
exitos,
n,
round(proporcion, digitos),
round(error_estandar, digitos),
round(conf, digitos),
round(z, digitos),
round(margen_error, digitos),
round(inferior, digitos),
round(superior, digitos),
paste0(
round(proporcion, digitos),
" ± ",
round(margen_error, digitos)
)
),
check.names = FALSE
)
knitr::kable(
resultado,
format = "html",
col.names = c("Medida", "Valor"),
align = c("l", "r")
)
}
library(lsm) # Para acceder a la base de datos survey
library(knitr) # Para construir tablas
library(kableExtra)
datosCompleto <- lsm::survey
Muestra1 <-datosCompleto[1:100,27:34]
Muestra1
## # A tibble: 100 × 8
## Definitive Expense Income Gas Course Law Economic Race
## <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
## 1 4 48.9 1.61 27.4 Face-to-Face Agree Regular Ethnic
## 2 3.55 72.1 2.07 24.2 Virtual Agree Good Ethnic
## 3 2.72 85.2 2.84 22.3 Face-to-Face Agree Regular Ethnic
## 4 3.55 56.6 1.55 23.1 Virtual Agree Bad Ethnic
## 5 3.65 64.6 2.32 27.3 Face-to-Face In disagreement Bad None
## 6 4.35 63 2.1 17.2 Virtual Agree Good Ethnic
## 7 3.78 40.8 1.69 27.0 Virtual Agree Regular Ethnic
## 8 3.8 65.4 2.18 25.0 Face-to-Face In disagreement Regular None
## 9 2.88 37.3 1.71 25.1 Virtual In disagreement Regular Ethnic
## 10 3.58 63 2.1 21.8 Virtual In disagreement Bad Ethnic
## # ℹ 90 more rows
Muestra2 <- datosCompleto[1:29,27:34]
Muestra2
## # A tibble: 29 × 8
## Definitive Expense Income Gas Course Law Economic Race
## <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
## 1 4 48.9 1.61 27.4 Face-to-Face Agree Regular Ethnic
## 2 3.55 72.1 2.07 24.2 Virtual Agree Good Ethnic
## 3 2.72 85.2 2.84 22.3 Face-to-Face Agree Regular Ethnic
## 4 3.55 56.6 1.55 23.1 Virtual Agree Bad Ethnic
## 5 3.65 64.6 2.32 27.3 Face-to-Face In disagreement Bad None
## 6 4.35 63 2.1 17.2 Virtual Agree Good Ethnic
## 7 3.78 40.8 1.69 27.0 Virtual Agree Regular Ethnic
## 8 3.8 65.4 2.18 25.0 Face-to-Face In disagreement Regular None
## 9 2.88 37.3 1.71 25.1 Virtual In disagreement Regular Ethnic
## 10 3.58 63 2.1 21.8 Virtual In disagreement Bad Ethnic
## # ℹ 19 more rows
dim(Muestra1)
## [1] 100 8
dim(Muestra2)
## [1] 29 8
str(Muestra1)
## tibble [100 × 8] (S3: tbl_df/tbl/data.frame)
## $ Definitive: num [1:100] 4 3.55 2.73 3.55 3.65 ...
## $ Expense : num [1:100] 48.9 72.1 85.2 56.6 64.6 63 40.8 65.4 37.3 63 ...
## $ Income : num [1:100] 1.61 2.07 2.84 1.55 2.32 2.1 1.69 2.18 1.71 2.1 ...
## $ Gas : num [1:100] 27.4 24.2 22.3 23.1 27.3 ...
## $ Course : chr [1:100] "Face-to-Face" "Virtual" "Face-to-Face" "Virtual" ...
## $ Law : chr [1:100] "Agree" "Agree" "Agree" "Agree" ...
## $ Economic : chr [1:100] "Regular" "Good" "Regular" "Bad" ...
## $ Race : chr [1:100] "Ethnic" "Ethnic" "Ethnic" "Ethnic" ...
str(Muestra2)
## tibble [29 × 8] (S3: tbl_df/tbl/data.frame)
## $ Definitive: num [1:29] 4 3.55 2.73 3.55 3.65 ...
## $ Expense : num [1:29] 48.9 72.1 85.2 56.6 64.6 63 40.8 65.4 37.3 63 ...
## $ Income : num [1:29] 1.61 2.07 2.84 1.55 2.32 2.1 1.69 2.18 1.71 2.1 ...
## $ Gas : num [1:29] 27.4 24.2 22.3 23.1 27.3 ...
## $ Course : chr [1:29] "Face-to-Face" "Virtual" "Face-to-Face" "Virtual" ...
## $ Law : chr [1:29] "Agree" "Agree" "Agree" "Agree" ...
## $ Economic : chr [1:29] "Regular" "Good" "Regular" "Bad" ...
## $ Race : chr [1:29] "Ethnic" "Ethnic" "Ethnic" "Ethnic" ...
Variables dentro de nuestras bases de datos: Definitive
Expense
Income
Gas
Course
Law
Economic
Race
Mi razon de escoger aquellas 4 variantes que porque mi compañero (ahora retiro la materia) acepto conmigo hacer esas 4 variables, con ese punto en mente realizare la actividad con aquellas variantes.