Skip to contents

Fits a negative-binomial GLM via glm.nb (log link, joint ML estimation of the regression coefficients and the dispersion parameter \(\theta\)), falling back to a smaller model when the fit throws an error (typically non-convergence of \(\theta\), or a singular design). On each failure, the last column of data_obj is dropped and the fit is retried against the same form_obj (which must resolve to y ~ . or similar so that its right-hand side tracks the shrinking column set); this repeats until a fit succeeds or every predictor column has been removed, at which point NA is returned. Because columns are dropped strictly from the right, callers should order data_obj's columns from most to least important a priori, or accept that this is a best-effort robustness measure rather than a principled model-selection procedure.

Usage

robust_negbinreg(form_obj, data_obj)

Arguments

form_obj

The model formula, typically y ~ . so its right-hand side automatically tracks data_obj's shrinking column set across retries.

data_obj

The data frame to run negative-binomial regression on; its last column is dropped on each retry, in order, until a fit converges or no columns remain.

Value

The fitted glm.nb model object, or NA if no column subset (down to and including the response alone) produced a successful fit.

Examples

dat = data.frame(y = rpois(10, 2), x1 = rnorm(10), x2 = rnorm(10))
robust_negbinreg(y ~ ., dat)
#> 
#> Call:  MASS::glm.nb(formula = form_obj, data = data_obj, init.theta = 299570.4464, 
#>     link = log)
#> 
#> Coefficients:
#> (Intercept)           x1           x2  
#>      0.3553       0.4050      -0.1101  
#> 
#> Degrees of Freedom: 9 Total (i.e. Null);  7 Residual
#> Null Deviance:	    1.699 
#> Residual Deviance: 0.9628 	AIC: 32.03