# \donttest{
# Assume 'mydata' exists with response 'y' and predictors 'x1', 'x2'
# and that rgkw() is available and data is appropriate (0 < y < 1).
set.seed(456)
n <- 150
x1 <- runif(n, -1, 1)
x2 <- rnorm(n)
alpha <- exp(0.5 + 0.2 * x1)
beta <- exp(0.8 - 0.3 * x1 + 0.1 * x2)
gamma <- exp(0.6)
delta <- plogis(0.0 + 0.2 * x1)
lambda <- exp(-0.2 + 0.1 * x2)
# Use stats::rbeta as placeholder if rgkw is not available
y <- stats::rbeta(n, shape1 = gamma * alpha, shape2 = delta * beta) # Approximation
y <- pmax(pmin(y, 1 - 1e-7), 1e-7)
mydata <- data.frame(y = y, x1 = x1, x2 = x2)
# Fit a GKw model
model <- gkwreg(y ~ x1 | x1 + x2 | 1 | x1 | x2, data = mydata, family = "gkw")
# --- Extract different types of residuals ---
resp_res <- residuals(model, type = "response")
pearson_res <- residuals(model, type = "pearson")
quant_res <- residuals(model, type = "quantile")
cs_res <- residuals(model, type = "cox-snell")
# --- Diagnostic Plots ---
# QQ-plot for quantile residuals (should be approx. normal)
stats::qqnorm(quant_res, main = "QQ Plot: GKw Quantile Residuals")
stats::qqline(quant_res, col = "blue")
# Cox-Snell residuals plot (should be approx. exponential -> linear on exp-QQ)
plot(stats::qexp(stats::ppoints(length(cs_res))), sort(cs_res),
xlab = "Theoretical Exponential Quantiles", ylab = "Sorted Cox-Snell Residuals",
main = "Cox-Snell Residual Plot", pch = 1
)
abline(0, 1, col = "red")
# --- Compare residuals using a different family assumption ---
# Calculate quantile residuals assuming underlying Beta dist
quant_res_beta <- residuals(model, type = "quantile", family = "beta")
# Compare QQ-plots
stats::qqnorm(quant_res, main = "GKw Quantile Residuals")
stats::qqline(quant_res, col = "blue")
stats::qqnorm(quant_res_beta, main = "Beta Quantile Residuals (from GKw Fit)")
stats::qqline(quant_res_beta, col = "darkgreen")
# --- Partial Residuals ---
# Examine effect of x1 on the alpha parameter's linear predictor
if ("x1" %in% colnames(model$x$alpha)) { # Check if x1 is in alpha model matrix
# Find index for 'x1' (could be 2 if intercept is first)
x1_idx_alpha <- which(colnames(model$x$alpha) == "x1")
if (length(x1_idx_alpha) == 1) {
part_res_alpha_x1 <- residuals(model,
type = "partial",
parameter = "alpha", covariate_idx = x1_idx_alpha
)
# Plot partial residuals against the predictor
plot(mydata$x1, part_res_alpha_x1,
xlab = "x1", ylab = "Partial Residual (alpha predictor)",
main = "Partial Residual Plot for alpha ~ x1"
)
# Add a smoother to see the trend
lines(lowess(mydata$x1, part_res_alpha_x1), col = "red")
}
}
# Examine effect of x2 on the beta parameter's linear predictor
if ("x2" %in% colnames(model$x$beta)) {
x2_idx_beta <- which(colnames(model$x$beta) == "x2")
if (length(x2_idx_beta) == 1) {
part_res_beta_x2 <- residuals(model,
type = "partial",
parameter = "beta", covariate_idx = x2_idx_beta
)
plot(mydata$x2, part_res_beta_x2,
xlab = "x2", ylab = "Partial Residual (beta predictor)",
main = "Partial Residual Plot for beta ~ x2"
)
lines(lowess(mydata$x2, part_res_beta_x2), col = "red")
}
}
# }
Run the code above in your browser using DataLab