Skip to contents

Fits a negative binomial regression model (for over-dispersed counts) via MASS::glm.nb().

Usage

reg_negbin(formula, data, step = FALSE, ...)

Arguments

formula

A formula, e.g. count ~ x1 + x2.

data

A data frame.

step

Logical. Always FALSE for negative binomial.

...

Additional arguments passed to MASS::glm.nb().

Value

A named list analogous to reg_poisson(), plus:

theta

Estimated dispersion parameter for the negative binomial distribution.

residuals

DataFrame of observed, fitted, Pearson residual.

Details

Note: The glm.nb family does not support stepwise selection. Set step = FALSE.

For comparison with Poisson, inspect the dispersion ratio. NB is appropriate when dispersion >> 1.

Examples

set.seed(42)
x1 = rnorm(100)
mu = exp(1 + 0.3*x1)
y = rnbinom(100, size = 1, mu = mu)
reg_negbin(y ~ x1, data = data.frame(y, x1))
#> $model
#> 
#> Call:  MASS::glm.nb(formula = formula, data = data, init.theta = 1.065048329, 
#>     link = log)
#> 
#> Coefficients:
#> (Intercept)           x1  
#>      0.9066       0.3223  
#> 
#> Degrees of Freedom: 99 Total (i.e. Null);  98 Residual
#> Null Deviance:	    118 
#> Residual Deviance: 107.8 	AIC: 426.8
#> 
#> $coefficient
#> # A tibble: 2 × 7
#>   term        estimate std.error statistic  p.value conf.low conf.high
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
#> 1 (Intercept)    0.907     0.117      7.74 1.01e-14   0.674      1.14 
#> 2 x1             0.322     0.116      2.78 5.52e- 3   0.0918     0.553
#> 
#> $theta
#> [1] 1.065
#> 
#> $residuals
#>     observed fitted_val pearson_residual
#> 1         10  3.8515662       1.45813385
#> 2          3  2.0637798       0.38022455
#> 3          1  2.7832389      -0.56232217
#> 4          0  3.0360630      -0.88795154
#> 5          1  2.8203934      -0.56751176
#> 6          1  2.3925412      -0.49966195
#> 7          0  4.0300920      -0.91783383
#> 8          2  2.4013999      -0.14357779
#> 9         33  4.7454403       5.55301204
#> 10         0  2.4262552      -0.86031816
#> 11         0  3.7703837      -0.91129668
#> 12         7  5.1739838       0.33167912
#> 13         2  1.5822978       0.21062114
#> 14         0  2.2630181      -0.85100611
#> 15         1  2.3716583      -0.49583057
#> 16         1  3.0390864      -0.59585138
#> 17         1  2.2590357      -0.47416026
#> 18         1  1.0515664      -0.03567073
#> 19         1  1.1273871      -0.08362003
#> 20        15  3.7889557       2.69786926
#> 21         0  2.2427936      -0.84978084
#> 22         2  1.3942773       0.33757935
#> 23         1  2.3423351      -0.49035498
#> 24         3  3.6623436      -0.16427734
#> 25         1  4.5606361      -0.72545730
#> 26         0  2.1550342      -0.84426394
#> 27         3  2.2787702       0.26964147
#> 28         3  1.4024563       0.88626526
#> 29         1  2.8716086      -0.57447784
#> 30         7  2.0142924       2.06595728
#> 31         2  2.8673102      -0.26656014
#> 32         1  3.1073243      -0.60399201
#> 33         0  3.4563744      -0.90231357
#> 34         0  2.0345661      -0.83611636
#> 35         2  2.9134322      -0.27688530
#> 36         5  1.4234773       1.96109809
#> 37         0  1.9226440      -0.82787765
#> 38         2  1.8819006       0.05175445
#> 39         1  1.1369703      -0.08933592
#> 40         6  2.5047980       1.20627363
#> 41         9  2.6457802       2.09283292
#> 42         3  2.2037950       0.30614483
#> 43         3  3.1611981      -0.04551360
#> 44         0  1.9587721      -0.83061279
#> 45         3  1.5928291       0.70579639
#> 46         0  2.8464685      -0.88037002
#> 47         6  1.9060239       1.77545306
#> 48         2  3.9434531      -0.45130120
#> 49         2  2.1543556      -0.06048681
#> 50         1  3.0584438      -0.59819232
#> 51         2  2.7465179      -0.23811235
#> 52         1  1.9230283      -0.39738454
#> 53         3  4.1143681      -0.24912731
#> 54         1  3.0459013      -0.59667844
#> 55         0  2.5484818      -0.86668161
#> 56         5  2.7066395       0.74075383
#> 57         0  3.0818395      -0.88966913
#> 58         4  2.5485411       0.49359889
#> 59         3  0.9434328       1.54183614
#> 60         0  2.7139188      -0.87457392
#> 61         2  2.1994111      -0.07680247
#> 62         0  2.6281275      -0.87057839
#> 63        16  2.9865226       3.86085409
#> 64         7  3.8874613       0.73207187
#> 65         4  1.9584014       0.86587018
#> 66         1  3.7675566      -0.66936135
#> 67         3  2.7588717       0.07661481
#> 68         3  3.4601674      -0.12001412
#> 69         0  3.3312663      -0.89834871
#> 70         3  3.1234326      -0.03521845
#> 71         0  1.7688413      -0.81533789
#> 72         4  2.4048645       0.56987244
#> 73         7  3.0269319       1.16504470
#> 74         2  1.8206708       0.08074079
#> 75         2  2.0783795      -0.03164632
#> 76         1  2.9857264      -0.58926414
#> 77         0  3.1714202      -0.89291385
#> 78         2  2.8750079      -0.26830323
#> 79         1  1.8608672      -0.38074327
#> 80         0  1.7368276      -0.81252843
#> 81         2  4.0316317      -0.46253599
#> 82         2  2.6904349      -0.22416264
#> 83         0  2.5473973      -0.86662725
#> 84         2  2.3811759      -0.13732277
#> 85         0  1.6846934      -0.80779124
#> 86         2  3.0157114      -0.29880618
#> 87         0  2.3084385      -0.85369807
#> 88         2  2.3341654      -0.12243090
#> 89         4  3.3448429       0.17604688
#> 90         0  3.2266845      -0.89484243
#> 91         3  3.8779239      -0.20694160
#> 92         4  2.1235179       0.74422353
#> 93         9  3.0532239       1.73073029
#> 94         1  3.8766667      -0.67827549
#> 95         1  1.7306757      -0.34281067
#> 96         2  1.8759138       0.05452016
#> 97         1  1.7190279      -0.33919417
#> 98         3  1.5468187       0.74612093
#> 99         0  2.5404619      -0.86627870
#> 100        4  3.0560358       0.27450815
#> 
#> $diagnostics
#> # A tibble: 3 × 2
#>   metric  value
#>   <chr>   <dbl>
#> 1 aic    427.  
#> 2 bic    435.  
#> 3 theta    1.06
#> 
#> $model_info
#> # A tibble: 1 × 2
#>     aic   bic
#>   <dbl> <dbl>
#> 1  427.  435.
#> 
#> $formula
#> y ~ x1
#> <environment: 0x00000224a26c3038>
#> 
#> $input
#>      y          x1
#> 1   10  1.37095845
#> 2    3 -0.56469817
#> 3    1  0.36312841
#> 4    0  0.63286260
#> 5    1  0.40426832
#> 6    1 -0.10612452
#> 7    0  1.51152200
#> 8    2 -0.09465904
#> 9   33  2.01842371
#> 10   0 -0.06271410
#> 11   0  1.30486965
#> 12   7  2.28664539
#> 13   2 -1.38886070
#> 14   0 -0.27878877
#> 15   1 -0.13332134
#> 16   1  0.63595040
#> 17   1 -0.28425292
#> 18   1 -2.65645542
#> 19   1 -2.44046693
#> 20  15  1.32011335
#> 21   0 -0.30663859
#> 22   2 -1.78130843
#> 23   1 -0.17191736
#> 24   3  1.21467470
#> 25   1  1.89519346
#> 26   0 -0.43046913
#> 27   3 -0.25726938
#> 28   3 -1.76316309
#> 29   1  0.46009735
#> 30   7 -0.63999488
#> 31   2  0.45545012
#> 32   1  0.70483734
#> 33   0  1.03510352
#> 34   0 -0.60892638
#> 35   2  0.50495512
#> 36   5 -1.71700868
#> 37   0 -0.78445901
#> 38   2 -0.85090759
#> 39   1 -2.41420765
#> 40   6  0.03612261
#> 41   9  0.20599860
#> 42   3 -0.36105730
#> 43   3  0.75816324
#> 44   0 -0.72670483
#> 45   3 -1.36828104
#> 46   0  0.43281803
#> 47   6 -0.81139318
#> 48   2  1.44410126
#> 49   2 -0.43144620
#> 50   1  0.65564788
#> 51   2  0.32192527
#> 52   1 -0.78383894
#> 53   3  1.57572752
#> 54   1  0.64289931
#> 55   0  0.08976065
#> 56   5  0.27655075
#> 57   0  0.67928882
#> 58   4  0.08983289
#> 59   3 -2.99309008
#> 60   0  0.28488295
#> 61   2 -0.36723464
#> 62   0  0.18523056
#> 63  16  0.58182373
#> 64   7  1.39973683
#> 65   4 -0.72729206
#> 66   1  1.30254263
#> 67   3  0.33584812
#> 68   3  1.03850610
#> 69   0  0.92072857
#> 70   3  0.72087816
#> 71   0 -1.04311894
#> 72   4 -0.09018639
#> 73   7  0.62351816
#> 74   2 -0.95352336
#> 75   2 -0.54282881
#> 76   1  0.58099650
#> 77   0  0.76817874
#> 78   2  0.46376759
#> 79   1 -0.88577630
#> 80   0 -1.09978090
#> 81   2  1.51270701
#> 82   2  0.25792144
#> 83   0  0.08844023
#> 84   2 -0.12089654
#> 85   0 -1.19432890
#> 86   2  0.61199690
#> 87   0 -0.21713985
#> 88   2 -0.18275671
#> 89   4  0.93334633
#> 90   0  0.82177311
#> 91   3  1.39211638
#> 92   4 -0.47617392
#> 93   9  0.65034856
#> 94   1  1.39111046
#> 95   1 -1.11078888
#> 96   2 -0.86079259
#> 97   1 -1.13173868
#> 98   3 -1.45921400
#> 99   0  0.07998255
#> 100  4  0.65320434
#>