B0305 – Lab Eco Gral
https://ucr-ecologia.github.io/B0305-lab-eco-gral/
Escuela de Biologia, Universidad de Costa Rica
04 Sep 2025
¿Por qué tratamiento especial?
Ejemplos comunes:
¿Por qué esto importa?
¡Las distribuciones normales y modelos lineales pueden no ser apropiados!
La distribución “por defecto” para datos de conteo
Propiedad clave: La media es igual a la varianza
Dos problemas principales:
¿Por qué ocurren en ecología?
¿Qué es?
Causas en ecología:
Consecuencias estadísticas:
Recordatorio: Tipos de Error Estadístico
Realidad vs Decisión | No rechazar H₀ | Rechazar H₀ |
---|---|---|
H₀ es verdadera | ✓ Correcto | ❌ Error Tipo I |
H₀ es falsa | ❌ Error Tipo II | ✓ Correcto |
Error Tipo I (α): Rechazar H₀ cuando es verdadera (“falso positivo”)
Error Tipo II (β): No rechazar H₀ cuando es falsa (“falso negativo”)
En contexto de sobredispersión: Ignorar la sobredispersión eleva Error Tipo I
Paso 1: Calcular parámetro de dispersión
Paso 2: Pruebas formales
Código R:
# Simular datos sobredispersos
set.seed(123)
library(MASS)
# Generar datos con NB
# (media=5, sobredispersión)
counts <- rnegbin(40, mu = 5, theta = 1)
# Dividir en 2 "hábitats" aleatoriamente
habitat <- rep(c("Mar", "Rio"), each = 20)
data <- data.frame(counts, habitat)
# Modelo Poisson (INCORRECTO)
m_poisson <- glm(counts ~ habitat,
family = poisson, data = data)
Call:
glm(formula = counts ~ habitat, family = poisson, data = data)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.67710 0.09667 17.35 <2e-16 ***
habitatRio -0.35534 0.15060 -2.36 0.0183 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 232.43 on 39 degrees of freedom
Residual deviance: 226.78 on 38 degrees of freedom
AIC: 333.12
Number of Fisher Scoring iterations: 6
[1] 5.967796
Call:
glm.nb(formula = counts ~ habitat, data = data, init.theta = 0.825913691,
link = log)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.6771 0.2644 6.344 2.24e-10 ***
habitatRio -0.3553 0.3792 -0.937 0.349
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Negative Binomial(0.8259) family taken to be 1)
Null deviance: 45.513 on 39 degrees of freedom
Residual deviance: 44.637 on 38 degrees of freedom
AIC: 213.93
Number of Fisher Scoring iterations: 1
Theta: 0.826
Std. Err.: 0.231
2 x log-likelihood: -207.925
Lección
Mismo coeficiente, pero error estándar realista → conclusión correcta
¿Qué es?
Dos tipos de ceros:
Ejemplos:
Evaluación visual:
Pruebas formales:
Regla práctica:
Si >60-70% son ceros, considerar modelos de inflación de ceros
Código R:
# Simular peces en Mar vs. Río
set.seed(456)
library(pscl)
# Generar datos zero-inflated
n <- 40
habitat <- rep(c("Mar", "Rio"), each = 20)
# Crear inflación de ceros estructural
# Mar: más peces, Rio: muchos ceros
prob_zero <- ifelse(habitat == "Rio", 0.7, 0.2)
counts <- ifelse(runif(n) < prob_zero, 0,
rpois(n, lambda = 8))
data <- data.frame(counts, habitat)
Mar Rio
0 2 9
1 0 1
3 0 1
4 3 0
5 2 0
6 1 2
7 4 4
8 2 1
9 1 1
10 1 0
11 1 0
12 3 0
13 0 1
Datos observados:
- 65% de ceros (mucho más que Poisson)
- Mar: Pocos ceros, conteos altos
- Río: Muchos ceros (condiciones no aptas)
Modelo Binomial Negativa:
Call:
glm.nb(formula = counts ~ habitat, data = data, init.theta = 1.236507457,
link = log)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.9315 0.2184 8.845 <2e-16 ***
habitatRio -0.6232 0.3188 -1.955 0.0506 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Negative Binomial(1.2365) family taken to be 1)
Null deviance: 54.486 on 39 degrees of freedom
Residual deviance: 50.687 on 38 degrees of freedom
AIC: 222.98
Number of Fisher Scoring iterations: 1
Theta: 1.237
Std. Err.: 0.443
2 x log-likelihood: -216.979
Modelo ZINB (correcto):
Call:
zeroinfl(formula = counts ~ habitat | habitat, data = data, dist = "negbin")
Pearson residuals:
Min 1Q Median 3Q Max
-1.92830 -0.95007 0.02794 0.84733 2.38795
Count model coefficients (negbin with log link):
Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.03622 0.08925 22.815 <2e-16 ***
habitatRio -0.13161 0.15079 -0.873 0.383
Log(theta) 4.39992 3.36360 1.308 0.191
Zero-inflation model coefficients (binomial with logit link):
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.2039 0.7499 -2.939 0.00329 **
habitatRio 1.9997 0.8747 2.286 0.02224 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Theta = 81.4445
Number of iterations in BFGS optimization: 23
Log-likelihood: -91.42 on 5 Df
Call:
zeroinfl(formula = counts ~ habitat | habitat, data = data, dist = "negbin")
Count model coefficients (negbin with log link):
(Intercept) habitatRio
2.0362 -0.1316
Theta = 81.4445
Zero-inflation model coefficients (binomial with logit link):
(Intercept) habitatRio
-2.204 2.000
Lección
ZINB permite separar presencia/ausencia vs. abundancia dado presencia - dos procesos biológicos distintos
Modelo | Cuándo usar | Pregunta típica | Ventaja principal | Desventaja principal |
---|---|---|---|---|
Poisson | Media ≈ varianza Sin exceso de ceros |
¿Cómo afecta X la abundancia? | Sencillo e interpretable | Asume equidispersión (rara en ecología) |
Binomial Negativa | Sobredispersión presente |
¿Qué factores afectan abundancia con variabilidad realista? | Maneja sobredispersión Interpretación similar a Poisson |
No maneja inflación de ceros |
ZIP/ZINB | Exceso de ceros ± sobredispersión |
¿Qué determina presencia vs. abundancia? | Modela dos procesos biológicos distintos |
Más complejo Requiere justificación biológica |
Criterios de información:
LRT:
Leer más sobre AIC
Burnham, K.P. & Anderson, D.R. (2002). Model Selection and Multimodel Inference: A Practical Information-Theoretic Approach
Coeficientes Poisson/Binomial Negativa:
Ejemplo:
Recordar
Siempre reportar intervalos de confianza - La incertidumbre es parte fundamental del resultado
Mensajes clave:
Recordar:
Recursos adicionales:
Burnham, K.P. & Anderson, D.R. (2002). Model Selection and Multimodel Inference: A Practical Information-Theoretic Approach. 2nd ed. Springer-Verlag.
Zuur, A.F., Ieno, E.N., Walker, N.J., Saveliev, A.A. & Smith, G.M. (2009). Mixed Effects Models and Extensions in Ecology with R. Springer.
Bolker, B.M. (2008). Ecological Models and Data in R. Princeton University Press.
B0305 – Lab Eco Gral