Learn R Programming

flexCountReg (version 0.1.1)

regCompTest: Compare Regression Models with Likelihood Ratio Test, AIC, and BIC

Description

This function compares a given regression model to a base model using the Likelihood Ratio (LR) test, Akaike Information Criterion (AIC), and Bayesian Information Criterion (BIC).

Usage

regCompTest(
  model,
  data = NULL,
  basemodel = "Poisson",
  variables = FALSE,
  print = FALSE,
  ...
)

Value

A list containing the following components:

LL

Log-likelihood of the provided model.

LLbase

Log-likelihood of the base model.

LR

Likelihood Ratio statistic.

LRdof

Degrees of freedom for the Likelihood Ratio test.

AIC

Akaike Information Criterion for the provided model.

AICbase

Akaike Information Criterion for the base model.

BIC

Bayesian Information Criterion for the provided model.

BICbase

Bayesian Information Criterion for the base model.

LR_pvalue

P-value for the Likelihood Ratio test.

PseudoR2

McFadden's Pseudo R^2.

statistics

A tibble format summary of the results.

gtTable

A gt table object summarizing the results.

latexTable

Latex code for a table summarizing the results.

htmlTable

HTML table summarizing the results.

Arguments

model

A fitted regression model object.

data

An options data frame containing the variables in the model. If not supplied, the original data used to estimate the model will be used.

basemodel

A character string specifying the family of base model to compare against (options include the family from countreg or "Poisson"). Default is "Poisson".

variables

Logical. If TRUE, the base model will include the same variables as the provided model. If FALSE, the base model will be an intercept-only model. Default is FALSE.

print

Logical. If TRUE, a table of the results will be shown. If FALSE, the table of results will not be printed to the console.

...

Additional arguments to be passed to the base model fitting function - options are any argument from the countreg function.

Details

The function performs the following steps:

  1. Fits the base model, either a Poisson regression or another specified model.

  2. Computes the log-likelihoods of both the provided model and the base model.

  3. Calculates the AIC and BIC for both models.

  4. Conducts a Likelihood Ratio test to compare the models (if the provided model has more parameters than the base model).

  5. Computes McFadden's Pseudo R^2.

The Likelihood-Ratio test is computed as $$LR = -2 (LL_{base \ model}-LL_{model})$$. The test is chi-squared with degrees of freedom $$dof=N_{model \ params}-N_{base \ mode \ params}$$. The AIC is calculated as $$AIC = -2 \cdot LL + 2 \cdot nparam$$, and the BIC is calculated as $$BIC = -2 \cdot LL + nparam \cdot \log(n)$$.

Examples

Run this code

# Comparing the NBP model with the NB2 model
data("washington_roads")
washington_roads$AADTover10k <- ifelse(washington_roads$AADT>10000,1,0)

nbp.base <- countreg(Total_crashes ~ lnaadt + lnlength + speed50 +
                    ShouldWidth04 + AADTover10k,
                    data=washington_roads, family = 'NBP', method = 'NM',
                    max.iters=3000)
regCompTest(nbp.base, washington_roads, basemodel="NB2", print=TRUE)

Run the code above in your browser using DataLab