Skip to content
Statistical RethinkingStatistical Rethinking

4.7 Práctica

Los problemas de práctica del Capítulo 4 le piden ajustar regresiones lineales y polinomiales, interpretar intervalos a posteriori y comparar modelos con diferentes predictores.

Practice

Problemas fáciles

4E1. En la definición del modelo a continuación, ¿qué línea es la verosimilitud?

yi∼Normal(μ,σ)y_i \sim \text{Normal}(\mu, \sigma) μ=α+βxi\mu = \alpha + \beta x_i α∼Normal(0,10)\alpha \sim \text{Normal}(0, 10) β∼Normal(0,10)\beta \sim \text{Normal}(0, 10) σ∼Uniform(0,50)\sigma \sim \text{Uniform}(0, 50)

4E2. En el modelo anterior, ¿cuántos parámetros hay en la distribución a posteriori?

4E3. Usando la simulación predictiva a priori, ¿qué rango de estaturas implica la distribución a priori α∼Normal(178,20)\alpha \sim \text{Normal}(178, 20) para una persona de peso promedio?

# 4E3: Prior predictive for alpha
set.seed(42)
alpha_prior <- rnorm(1e4, 178, 20)
cat("Prior for alpha:\n")
cat("  Mean:", round(mean(alpha_prior), 2), "\n")
cat("  95% interval: [", round(quantile(alpha_prior, 0.025), 2), ",",
  round(quantile(alpha_prior, 0.975), 2), "]\n")
Click Run to execute this code in your browser with WebR

4E4. ¿Qué implica la distribución a priori β∼Log-Normal(0,1)\beta \sim \text{Log-Normal}(0, 1) sobre la pendiente?

Problemas medios

4M1. Simule estaturas observadas desde la distribución predictiva a priori para el siguiente modelo:

hi∼Normal(μi,σ)h_i \sim \text{Normal}(\mu_i, \sigma) μi=α+βxi\mu_i = \alpha + \beta x_i α∼Normal(0,10)\alpha \sim \text{Normal}(0, 10) β∼Normal(0,1)\beta \sim \text{Normal}(0, 1) σ∼Uniform(0,10)\sigma \sim \text{Uniform}(0, 10)

# 4M1: Prior predictive simulation
set.seed(42)
n <- 100
x <- runif(n, 0, 10)

# Draw from priors

alpha <- rnorm(1, 0, 10)
beta <- rnorm(1, 0, 1)
sigma <- runif(1, 0, 10)

# Simulate data

mu <- alpha + beta * x
y <- rnorm(n, mu, sigma)

cat("Prior draw:\n")
cat(" alpha:", round(alpha, 2), "\n")
cat(" beta:", round(beta, 2), "\n")
cat(" sigma:", round(sigma, 2), "\n")

plot(x, y, pch = 16, col = "steelblue",
main = "4M1: Prior predictive simulation",
xlab = "x", ylab = "y")
abline(a = alpha, b = beta, lwd = 2, col = "firebrick")
Click Run to execute this code in your browser with WebR

4M2. Traduzca el modelo anterior a una aproximación cuadrática usando lm o quap.

4M3. Un investigador dice "la pendiente es 2.3, así que un aumento de 1 kg en peso causa un aumento de 2.3 cm en estatura." ¿Es correcta esta interpretación? ¿Qué falta?

Problemas difíciles

4H1. Ajuste una regresión lineal de estatura sobre peso usando los datos simulados de Howell. Calcule la distribución a posteriori de la estatura esperada para una persona que pesa 50 kg.

# 4H1: Posterior prediction at weight = 50 kg
set.seed(42)
n <- 352
weight <- rnorm(n, mean = 35, sd = 5)
height <- 100 + 2.5 * weight + rnorm(n, sd = 4)

fit <- lm(height ~ weight)
library(MASS)
post_samples <- mvrnorm(1e4, mu = coef(fit), Sigma = vcov(fit))

# Predict height at weight = 50

pred_50 <- post_samples[, 1] + post_samples[, 2] * 50
cat("Posterior prediction at 50 kg:\n")
cat(" Mean:", round(mean(pred_50), 2), "cm\n")
cat(" 95% interval: [", round(quantile(pred_50, 0.025), 2), ",",
round(quantile(pred_50, 0.975), 2), "] cm\n")
Click Run to execute this code in your browser with WebR

4H2. Ajuste una regresión cuadrática de estatura sobre peso. Compárela con el modelo lineal usando AIC. ¿Qué modelo es preferido?

# 4H2: Compare linear and quadratic models with AIC
set.seed(42)
n <- 352
weight <- rnorm(n, mean = 35, sd = 5)
height <- 100 + 2.5 * weight + rnorm(n, sd = 4)

m_linear <- lm(height ~ weight)
m_quad <- lm(height ~ weight + I(weight^2))

cat("AIC comparison:\n")
cat("Linear:", round(AIC(m_linear), 2), "\n")
cat("Quadratic:", round(AIC(m_quad), 2), "\n")
cat("Difference:", round(AIC(m_linear) - AIC(m_quad), 2), "\n")
cat("\nIf difference > 2, the quadratic model is preferred\n")
Click Run to execute this code in your browser with WebR

4H3. Ajuste una regresión cúbica y grafique las líneas predictivas a posteriori. ¿Mejora el término cúbico el modelo? Use verificaciones predictivas a posteriori para decidir.