Skip to contents

Fits a binary logistic regression model via glm(family = binomial()), with optional stepwise selection.

Usage

reg_logistic(
  formula,
  data,
  step = FALSE,
  direction = c("both", "forward", "backward"),
  ...
)

Arguments

formula

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

data

A data frame containing the variables in formula.

step

Logical. Whether to perform stepwise selection (TRUE/FALSE). Default is FALSE.

direction

Character: "both" (default), "forward", or "backward".

...

Additional arguments passed to lm() and step().

Value

A named list analogous to reg_lm(), plus:

odds_ratio

Tibble with odds ratios and 95% CIs.

residuals

DataFrame of observed, fitted probability, residual.

Details

Same stepwise logic as reg_lm, but uses GLM with the binomial family. A Hosmer-Lemeshow goodness-of-fit test is included in diagnostics.

Examples

set.seed(42)
x1 = rnorm(100)
logit = 0.5 + x1
prob = 1 / (1 + exp(-logit))
y = rbinom(100, 1, prob)
reg_logistic(y ~ x1, data = data.frame(y, x1))
#> $model
#> 
#> Call:  glm(formula = formula, family = binomial(), data = data)
#> 
#> Coefficients:
#> (Intercept)           x1  
#>      0.2855       1.0744  
#> 
#> Degrees of Freedom: 99 Total (i.e. Null);  98 Residual
#> Null Deviance:	    136.7 
#> Residual Deviance: 114.9 	AIC: 118.9
#> 
#> $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.285     0.226      1.26 0.207       -0.163     0.734
#> 2 x1             1.07      0.270      3.98 0.0000682    0.539     1.61 
#> 
#> $odds_ratio
#> # A tibble: 2 × 4
#>   term        odds_ratio conf.low conf.high
#>   <chr>            <dbl>    <dbl>     <dbl>
#> 1 (Intercept)       1.33    0.850      2.08
#> 2 x1                2.93    1.71       5.00
#> 
#> $residuals
#>     observed fitted_val pearson_residual
#> 1          0 0.85302076       -2.4090833
#> 2          1 0.42037540        1.1742343
#> 3          0 0.66276736       -1.4018959
#> 4          1 0.72421389        0.6170961
#> 5          1 0.67257463        0.6977276
#> 6          1 0.54276332        0.9178364
#> 7          0 0.87096500       -2.5980445
#> 8          1 0.54581885        0.9122005
#> 9          1 0.92086425        0.2931491
#> 10         1 0.55431325        0.8966795
#> 11         1 0.84389311        0.4300979
#> 12         1 0.93947888        0.2538107
#> 13         0 0.23027901       -0.5469662
#> 14         1 0.49648925        1.0070463
#> 15         1 0.53550294        0.9313449
#> 16         0 0.72487602       -1.6231836
#> 17         0 0.49502164       -0.9900924
#> 18         0 0.07118175       -0.2768337
#> 19         0 0.08813579       -0.3108931
#> 20         0 0.84603861       -2.3441704
#> 21         1 0.48901030        1.0222264
#> 22         0 0.16405055       -0.4429950
#> 23         1 0.52517431        0.9508573
#> 24         0 0.83069724       -2.2150798
#> 25         1 0.91066207        0.3132127
#> 26         0 0.45586194       -0.9152972
#> 27         1 0.50226943        0.9954714
#> 28         0 0.16674171       -0.4473344
#> 29         1 0.68564503        0.6771120
#> 30         0 0.40079986       -0.8178579
#> 31         1 0.68456784        0.6788045
#> 32         1 0.73938854        0.5936909
#> 33         1 0.80180919        0.4971715
#> 34         0 0.40884248       -0.8316228
#> 35         0 0.69593901       -1.5128826
#> 36         1 0.17374596        2.1807177
#> 37         0 0.36416087       -0.7567859
#> 38         1 0.34779535        1.3693992
#> 39         0 0.09042977       -0.3153099
#> 40         1 0.58037344        0.8503108
#> 41         1 0.62406218        0.7761472
#> 42         0 0.47441364       -0.9500720
#> 43         1 0.75027655        0.5769245
#> 44         0 0.37864650       -0.7806342
#> 45         0 0.23422162       -0.5530469
#> 46         1 0.67929364        0.6871080
#> 47         0 0.35748685       -0.7459145
#> 48         1 0.86260292        0.3991014
#> 49         1 0.45560155        1.0931150
#> 50         0 0.72907653       -1.6404509
#> 51         1 0.65280257        0.7292851
#> 52         0 0.36431514       -0.7570380
#> 53         1 0.87852142        0.3718550
#> 54         1 0.72636249        0.6137777
#> 55         1 0.59434016        0.8261587
#> 56         0 0.64167223       -1.3381857
#> 57         1 0.73406445        0.6018955
#> 58         1 0.59435887        0.8261266
#> 59         0 0.05067269       -0.2310356
#> 60         1 0.64372801        0.7439430
#> 61         0 0.47275899       -0.9469244
#> 62         1 0.61881286        0.7848550
#> 63         1 0.71312774        0.6342502
#> 64         0 0.85685528       -2.4466175
#> 65         0 0.37849806       -0.7803880
#> 66         1 0.84356345        0.4306359
#> 67         0 0.65618528       -1.3815004
#> 68         1 0.80238950        0.4962636
#> 69         0 0.78155524       -1.8915117
#> 70         0 0.74269583       -1.6989558
#> 71         0 0.30253370       -0.6586054
#> 72         0 0.54700989       -1.0988875
#> 73         1 0.72220413        0.6202016
#> 74         0 0.32322341       -0.6910807
#> 75         1 0.42611116        1.1605195
#> 76         1 0.71294588        0.6345321
#> 77         1 0.75228731        0.5738287
#> 78         1 0.68649436        0.6757782
#> 79         0 0.33934643       -0.7166956
#> 80         0 0.28984421       -0.6388597
#> 81         1 0.87110802        0.3846599
#> 82         1 0.63705707        0.7547967
#> 83         1 0.59399807        0.8267449
#> 84         0 0.53882188       -1.0809068
#> 85         0 0.26938878       -0.6072207
#> 86         1 0.71971381        0.6240522
#> 87         1 0.51304557        0.9742405
#> 88         1 0.52226935        0.9564104
#> 89         1 0.78386092        0.5251062
#> 90         1 0.76286149        0.5575428
#> 91         1 0.85584809        0.4104043
#> 92         0 0.44370976       -0.8930973
#> 93         1 0.72795042        0.6113264
#> 94         1 0.85571470        0.4106261
#> 95         0 0.28741581       -0.6350929
#> 96         0 0.34539013       -0.7263797
#> 97         1 0.28282792        1.5923940
#> 98         0 0.21715454       -0.5266795
#> 99         1 0.59180471        0.8305099
#> 100        1 0.72855764        0.6103892
#> 
#> $diagnostics
#> # A tibble: 3 × 2
#>   metric           value
#>   <chr>            <dbl>
#> 1 HLR_chisq  44100000513
#> 2 HLR_df               8
#> 3 HLR_pvalue           0
#> 
#> $model_info
#> # A tibble: 1 × 2
#>     aic   bic
#>   <dbl> <dbl>
#> 1  119.  124.
#> 
#> $formula
#> y ~ x1
#> <environment: 0x00000224a4206d98>
#> 
#> $input
#>     y          x1
#> 1   0  1.37095845
#> 2   1 -0.56469817
#> 3   0  0.36312841
#> 4   1  0.63286260
#> 5   1  0.40426832
#> 6   1 -0.10612452
#> 7   0  1.51152200
#> 8   1 -0.09465904
#> 9   1  2.01842371
#> 10  1 -0.06271410
#> 11  1  1.30486965
#> 12  1  2.28664539
#> 13  0 -1.38886070
#> 14  1 -0.27878877
#> 15  1 -0.13332134
#> 16  0  0.63595040
#> 17  0 -0.28425292
#> 18  0 -2.65645542
#> 19  0 -2.44046693
#> 20  0  1.32011335
#> 21  1 -0.30663859
#> 22  0 -1.78130843
#> 23  1 -0.17191736
#> 24  0  1.21467470
#> 25  1  1.89519346
#> 26  0 -0.43046913
#> 27  1 -0.25726938
#> 28  0 -1.76316309
#> 29  1  0.46009735
#> 30  0 -0.63999488
#> 31  1  0.45545012
#> 32  1  0.70483734
#> 33  1  1.03510352
#> 34  0 -0.60892638
#> 35  0  0.50495512
#> 36  1 -1.71700868
#> 37  0 -0.78445901
#> 38  1 -0.85090759
#> 39  0 -2.41420765
#> 40  1  0.03612261
#> 41  1  0.20599860
#> 42  0 -0.36105730
#> 43  1  0.75816324
#> 44  0 -0.72670483
#> 45  0 -1.36828104
#> 46  1  0.43281803
#> 47  0 -0.81139318
#> 48  1  1.44410126
#> 49  1 -0.43144620
#> 50  0  0.65564788
#> 51  1  0.32192527
#> 52  0 -0.78383894
#> 53  1  1.57572752
#> 54  1  0.64289931
#> 55  1  0.08976065
#> 56  0  0.27655075
#> 57  1  0.67928882
#> 58  1  0.08983289
#> 59  0 -2.99309008
#> 60  1  0.28488295
#> 61  0 -0.36723464
#> 62  1  0.18523056
#> 63  1  0.58182373
#> 64  0  1.39973683
#> 65  0 -0.72729206
#> 66  1  1.30254263
#> 67  0  0.33584812
#> 68  1  1.03850610
#> 69  0  0.92072857
#> 70  0  0.72087816
#> 71  0 -1.04311894
#> 72  0 -0.09018639
#> 73  1  0.62351816
#> 74  0 -0.95352336
#> 75  1 -0.54282881
#> 76  1  0.58099650
#> 77  1  0.76817874
#> 78  1  0.46376759
#> 79  0 -0.88577630
#> 80  0 -1.09978090
#> 81  1  1.51270701
#> 82  1  0.25792144
#> 83  1  0.08844023
#> 84  0 -0.12089654
#> 85  0 -1.19432890
#> 86  1  0.61199690
#> 87  1 -0.21713985
#> 88  1 -0.18275671
#> 89  1  0.93334633
#> 90  1  0.82177311
#> 91  1  1.39211638
#> 92  0 -0.47617392
#> 93  1  0.65034856
#> 94  1  1.39111046
#> 95  0 -1.11078888
#> 96  0 -0.86079259
#> 97  1 -1.13173868
#> 98  0 -1.45921400
#> 99  1  0.07998255
#> 100 1  0.65320434
#>