Skip to contents

Performs an Archer-Lemeshow goodness-of-fit (GOF) test for logistic regression models fitted with complex survey data. This test is an extension of the Hosmer-Lemeshow test for survey designs.

Usage

svygof(fit, design, G = 10)

Source

The implementation is a formalized function based on the script and discussion in the R-help mailing list archives: https://stat.ethz.ch/pipermail/r-help/2016-November/443223.html

Arguments

fit

A fitted model object of class `svyglm`.

design

A survey design object of class `svydesign` or `svyrep.design` that was used to fit the model.

G

An integer specifying the number of groups to create based on fitted probabilities. Defaults to 10 (deciles).

Value

A `data.frame` containing the F-statistic, the numerator (df1) and denominator (df2) degrees of freedom, and the p-value for the test.

Details

The function automates the process of calculating residuals and fitted values, creating groups (deciles by default) based on fitted probabilities, building a new survey design with these variables, and running a final Wald test. A non-significant p-value (e.g., p > 0.05) suggests no evidence of a poor fit.

Examples

if (FALSE) { # \dontrun{
# Ensure required packages are loaded
if (requireNamespace("survey", quietly = TRUE) &&
    requireNamespace("NHANES", quietly = TRUE) &&
    requireNamespace("dplyr", quietly = TRUE)) {

  # 1. Prepare Data
  data(NHANESraw, package = "NHANES")
  nhanes_data <- NHANESraw %>%
    dplyr::filter(Age >= 20) %>%
    dplyr::mutate(ObeseStatus = factor(ifelse(BMI >= 30, "Obese", "Not Obese"),
                                       levels = c("Not Obese", "Obese"))) %>%
    dplyr::filter(complete.cases(ObeseStatus, Age, Gender, Race1,
                                 WTMEC2YR, SDMVPSU, SDMVSTRA))

  # 2. Create a replicate design object
  std_design <- survey::svydesign(
    ids = ~SDMVPSU,
    strata = ~SDMVSTRA,
    weights = ~WTMEC2YR,
    nest = TRUE,
    data = nhanes_data
  )
  rep_design <- survey::as.svrepdesign(std_design)

  # 3. Fit a survey logistic regression model using the replicate design
  fit_obesity_rep <- survey::svyglm(
    ObeseStatus ~ Age + Gender + Race1,
    design = rep_design,
    family = quasibinomial()
  )

  # 4. Calculate the design-correct AUC
  auc_results <- svyAUC(fit_obesity_rep, rep_design)
  print(auc_results)
}
} # }