# Define degrees of freedom for the theoretical t-distribution
t_df <- 5 # Adjust this value as appropriate for your model
# ------------------------------------------------------------------------------
# Calculate Randomized Quantile Residuals & Predictions
# ------------------------------------------------------------------------------
# Extract "mle-mvn" randomized quantile residuals for GLMMs
df_clean$resids_m5 <- residuals(fit_m5_svc, type = "mle-mvn")
df_clean$resids_m7 <- residuals(fit_m7_svc, type = "mle-mvn")
df_clean$resids_m8 <- residuals(fit_m8_svc, type = "mle-mvn")
df_clean$fitted_m5 <- predict(fit_m5_svc)$est
df_clean$fitted_m7 <- predict(fit_m7_svc)$est
df_clean$fitted_m8 <- predict(fit_m8_svc)$est
# Filter out non-finite values across all models
df_resids <- df_clean %>%
filter(
is.finite(resids_m5),
is.finite(resids_m7),
is.finite(resids_m8)
)
# Define a consistent color palette for the 3 models
model_colors <- c(
"M5: SVC Base" = "#2b5c8f",
"M7: SVC Model 7" = "#2e7d32",
"M8: SVC Model 8" = "#d95f02"
)
# ------------------------------------------------------------------------------
# Compute AIC Model Comparison Table
# ------------------------------------------------------------------------------
aic_table <- data.frame(
Model = c("M5: SVC Base", "M7: SVC Model 7", "M8: SVC Model 8"),
df = c(attr(logLik(fit_m5_svc), "df"),
attr(logLik(fit_m7_svc), "df"),
attr(logLik(fit_m8_svc), "df")),
AIC = c(AIC(fit_m5_svc), AIC(fit_m7_svc), AIC(fit_m8_svc))
) %>%
mutate(
delta_AIC = AIC - min(AIC),
weight = exp(-0.5 * delta_AIC) / sum(exp(-0.5 * delta_AIC))
) %>%
arrange(AIC)
# ------------------------------------------------------------------------------
# Q-Q Plot Comparisons (Student's t-Distribution)
# ------------------------------------------------------------------------------
qq_m5 <- ggplot(df_resids, aes(sample = resids_m5)) +
stat_qq(distribution = qt, dparams = list(df = t_df), alpha = 0.2, color = model_colors["M5: SVC Base"], size = 0.7) +
stat_qq_line(distribution = qt, dparams = list(df = t_df), color = "firebrick", linewidth = 0.8) +
theme_minimal() +
labs(title = "M5: Q-Q Plot (t-dist)", x = paste0("Theoretical Quantiles (t, df = ", t_df, ")"), y = "Sample Quantiles")
qq_m7 <- ggplot(df_resids, aes(sample = resids_m7)) +
stat_qq(distribution = qt, dparams = list(df = t_df), alpha = 0.2, color = model_colors["M7: SVC Model 7"], size = 0.7) +
stat_qq_line(distribution = qt, dparams = list(df = t_df), color = "firebrick", linewidth = 0.8) +
theme_minimal() +
labs(title = "M7: Q-Q Plot (t-dist)", x = paste0("Theoretical Quantiles (t, df = ", t_df, ")"), y = "Sample Quantiles")
qq_m8 <- ggplot(df_resids, aes(sample = resids_m8)) +
stat_qq(distribution = qt, dparams = list(df = t_df), alpha = 0.2, color = model_colors["M8: SVC Model 8"], size = 0.7) +
stat_qq_line(distribution = qt, dparams = list(df = t_df), color = "firebrick", linewidth = 0.8) +
theme_minimal() +
labs(title = "M8: Q-Q Plot (t-dist)", x = paste0("Theoretical Quantiles (t, df = ", t_df, ")"), y = "Sample Quantiles")
# ------------------------------------------------------------------------------
# Residuals vs. Fitted Values
# ------------------------------------------------------------------------------
fit_m5 <- ggplot(df_resids, aes(x = fitted_m5, y = resids_m5)) +
geom_point(alpha = 0.15, size = 0.7, color = model_colors["M5: SVC Base"]) +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
geom_smooth(method = "gam", color = "black", se = TRUE, linewidth = 0.8) +
theme_minimal() +
labs(title = "M5: Resids vs Fitted", x = "Predicted Log MW", y = "Quantile Residuals")
fit_m7 <- ggplot(df_resids, aes(x = fitted_m7, y = resids_m7)) +
geom_point(alpha = 0.15, size = 0.7, color = model_colors["M7: SVC Model 7"]) +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
geom_smooth(method = "gam", color = "black", se = TRUE, linewidth = 0.8) +
theme_minimal() +
labs(title = "M7: Resids vs Fitted", x = "Predicted Log MW", y = "Quantile Residuals")
fit_m8 <- ggplot(df_resids, aes(x = fitted_m8, y = resids_m8)) +
geom_point(alpha = 0.15, size = 0.7, color = model_colors["M8: SVC Model 8"]) +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
geom_smooth(method = "gam", color = "black", se = TRUE, linewidth = 0.8) +
theme_minimal() +
labs(title = "M8: Resids vs Fitted", x = "Predicted Log MW", y = "Quantile Residuals")
# ------------------------------------------------------------------------------
# Residuals vs. Depth (Covariate Check Example)
# ------------------------------------------------------------------------------
cov_m5 <- ggplot(df_resids, aes(x = Depth, y = resids_m5)) +
geom_point(alpha = 0.15, size = 0.7, color = model_colors["M5: SVC Base"]) +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
geom_smooth(method = "gam", color = "black", se = TRUE, linewidth = 0.8) +
theme_minimal() +
labs(title = "M5: Resids vs Depth", x = "Depth (m)", y = "Quantile Residuals")
cov_m7 <- ggplot(df_resids, aes(x = Depth, y = resids_m7)) +
geom_point(alpha = 0.15, size = 0.7, color = model_colors["M7: SVC Model 7"]) +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
geom_smooth(method = "gam", color = "black", se = TRUE, linewidth = 0.8) +
theme_minimal() +
labs(title = "M7: Resids vs Depth", x = "Depth (m)", y = "Quantile Residuals")
cov_m8 <- ggplot(df_resids, aes(x = Depth, y = resids_m8)) +
geom_point(alpha = 0.15, size = 0.7, color = model_colors["M8: SVC Model 8"]) +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
geom_smooth(method = "gam", color = "black", se = TRUE, linewidth = 0.8) +
theme_minimal() +
labs(title = "M8: Resids vs Depth", x = "Depth (m)", y = "Quantile Residuals")
# ------------------------------------------------------------------------------
# Spatial Empirical Variograms
# ------------------------------------------------------------------------------
v_m5 <- variogram(resids_m5 ~ 1, locations = ~ X + Y, data = df_resids, cutoff = 100)
v_m5$Model <- "M5: SVC Base"
v_m7 <- variogram(resids_m7 ~ 1, locations = ~ X + Y, data = df_resids, cutoff = 100)
v_m7$Model <- "M7: SVC Model 7"
v_m8 <- variogram(resids_m8 ~ 1, locations = ~ X + Y, data = df_resids, cutoff = 100)
v_m8$Model <- "M8: SVC Model 8"
vario_df <- rbind(v_m5, v_m7, v_m8)
p_vario <- ggplot(vario_df, aes(x = dist, y = gamma, color = Model)) +
geom_point(size = 2) +
geom_line(linewidth = 1) +
scale_color_manual(values = model_colors) +
theme_minimal() +
labs(
title = "Empirical Residual Variogram Comparison across M5, M7, M8",
subtitle = "Flatter curves indicate better removal of spatial autocorrelation",
x = "Distance (km)",
y = "Semi-variance"
)
# ------------------------------------------------------------------------------
# Display Model Comparison & Diagnostic Plots
# ------------------------------------------------------------------------------
# Print AIC Table
cat("\n=== AIC Model Comparison Table ===\n")