# \donttest{
require(gkwreg)
require(gkwdist)
data(StressAnxiety)
# Example 1: Basic heteroscedastic relationship
# Mean anxiety increases with stress
# Variability in anxiety also changes with stress
fit_kw <- gkwreg(
anxiety ~ stress |
stress,
data = StressAnxiety,
family = "kw"
)
summary(fit_kw)
# Interpretation:
# - Alpha: Positive relationship between stress and mean anxiety
# - Beta: Precision changes with stress level
# (anxiety becomes more/less variable at different stress levels)
# Compare to homoscedastic model
fit_kw_homo <- gkwreg(anxiety ~ stress,
data = StressAnxiety, family = "kw"
)
anova(fit_kw_homo, fit_kw)
# Example 2: Nonlinear stress effects via polynomial
# Stress-anxiety relationship often shows threshold or saturation effects
fit_kw_poly <- gkwreg(
anxiety ~ poly(stress, 2) | # quadratic mean
poly(stress, 2), # quadratic precision
data = StressAnxiety,
family = "kw"
)
summary(fit_kw_poly)
# Interpretation:
# - Quadratic terms allow for:
# * Threshold effects (anxiety accelerates at high stress)
# * Saturation effects (anxiety plateaus at extreme stress)
# Test nonlinearity
anova(fit_kw, fit_kw_poly)
# Example 3: Exponentiated Kumaraswamy for extreme anxiety patterns
# Some individuals may show very extreme anxiety responses to stress
fit_ekw <- gkwreg(
anxiety ~ poly(stress, 2) | # alpha: quadratic mean
poly(stress, 2) | # beta: quadratic precision
stress, # lambda: linear tail effect
data = StressAnxiety,
family = "ekw"
)
summary(fit_ekw)
# Interpretation:
# - Lambda: Linear component captures asymmetry at extreme stress levels
# (very high stress may produce different tail behavior)
# Example 4: McDonald distribution for highly skewed anxiety
# Anxiety distributions are often right-skewed (ceiling effects)
fit_mc <- gkwreg(
anxiety ~ poly(stress, 2) | # gamma
poly(stress, 2) | # delta
stress, # lambda: extremity
data = StressAnxiety,
family = "mc",
control = gkw_control(method = "BFGS", maxit = 1500)
)
summary(fit_mc)
# Compare models
AIC(fit_kw, fit_kw_poly, fit_ekw, fit_mc)
# Visualization: Stress-Anxiety relationship
plot(anxiety ~ stress,
data = StressAnxiety,
xlab = "Stress Level", ylab = "Anxiety Level",
main = "Stress-Anxiety Relationship with Heteroscedasticity",
pch = 19, col = rgb(0, 0, 1, 0.3)
)
# Add fitted curve
stress_seq <- seq(min(StressAnxiety$stress), max(StressAnxiety$stress),
length.out = 100
)
pred_mean <- predict(fit_kw, newdata = data.frame(stress = stress_seq))
lines(stress_seq, pred_mean, col = "red", lwd = 2)
# Add lowess smooth for comparison
lines(lowess(StressAnxiety$stress, StressAnxiety$anxiety),
col = "blue", lwd = 2, lty = 2
)
legend("topleft",
legend = c("Kumaraswamy fit", "Lowess smooth"),
col = c("red", "blue"), lwd = 2, lty = c(1, 2)
)
# }
Run the code above in your browser using DataLab