Skip to contents

stats::predict doesn't offer confidence intervals. This function will add the Wald confidence limits on the logit (log-odds) scale or back-transformed to the probability scale.

Usage

predict_glm(
  object,
  newdata = NULL,
  type = c("link", "response"),
  conf.level = 0.95
)

Arguments

object

A glm object with family binomial and link function logit.

newdata

optionally, a data frame in which to look for variables with which to predict. If omitted, the fitted linear predictors are used.

type

The type of prediction required. The default, "link", is on the scale of the linear predictors; the alternative "response" is on the scale of the response variable. Thus for a default binomial model the default predictions are of log-odds (probabilities on logit scale) and type = "response" gives the predicted probabilities.

conf.level

The confidence level to use.

Value

data.frame

See also

stats::predict

Examples

#----------------------------------------------------------------------------
# predict_glm() examples.
#----------------------------------------------------------------------------
library(bkstat)

mod <- glm(
  formula = am ~ disp + vs,
  data = mtcars,
  family = binomial
)

predict_glm(
  object = mod,
  type = "link"
)
#>                       estimate     lower_ci     upper_ci
#> Mazda RX4            4.2484820  -0.18067453  8.677638439
#> Mazda RX4 Wag        4.2484820  -0.18067453  8.677638439
#> Datsun 710           0.6679392  -0.73690578  2.072784254
#> Hornet 4 Drive      -5.1480090  -9.67954180 -0.616476198
#> Hornet Sportabout   -3.5061157  -6.23169857 -0.780532829
#> Valiant             -3.8685004  -7.42825690 -0.308743878
#> Duster 360          -3.5061157  -6.23169857 -0.780532829
#> Merc 240D           -0.8325754  -2.42820559  0.763054772
#> Merc 230            -0.6038148  -2.11024449  0.902614938
#> Merc 280            -1.6429309  -3.66147474  0.375613011
#> Merc 280C           -1.6429309  -3.66147474  0.375613011
#> Merc 450SE          -0.2414301  -1.97802794  1.495167763
#> Merc 450SL          -0.2414301  -1.97802794  1.495167763
#> Merc 450SLC         -0.2414301  -1.97802794  1.495167763
#> Cadillac Fleetwood  -7.8486904 -13.74862138 -1.948759387
#> Lincoln Continental -7.3834145 -12.92425527 -1.842573781
#> Chrysler Imperial   -6.6079548 -11.55571020 -1.660199320
#> Fiat 128             1.8039878  -0.06430272  3.672278303
#> Honda Civic          1.9203068  -0.01377675  3.854390267
#> Toyota Corolla       2.0986625   0.05942390  4.137901103
#> Toyota Corona        0.1987861  -1.15676133  1.554333492
#> Dodge Challenger    -1.8776502  -3.74648931 -0.008811068
#> AMC Javelin         -1.3348284  -3.05177958  0.382122872
#> Camaro Z28          -3.1183858  -5.60401643 -0.632755203
#> Pontiac Firebird    -5.0570352  -8.85023091 -1.263839544
#> Fiat X1-9            1.7923559  -0.06948770  3.654199489
#> Porsche 914-2        5.7877696   0.18848499 11.387054179
#> Lotus Europa         1.1681108  -0.39213164  2.728353208
#> Ford Pantera L      -3.1571588  -5.66606172 -0.648255885
#> Ferrari Dino         4.8300768  -0.03700294  9.697156491
#> Maserati Bora       -1.2185094  -2.91590041  0.478881627
#> Volvo 142E           0.1638904  -1.19214795  1.519928733

predict_glm(
  object = mod,
  type = "response"
)
#>                         estimate     lower_ci  upper_ci
#> Mazda RX4           0.9859153084 4.549538e-01 0.9998297
#> Mazda RX4 Wag       0.9859153084 4.549538e-01 0.9998297
#> Datsun 710          0.6610415664 3.236811e-01 0.8882297
#> Hornet 4 Drive      0.0057773906 6.254625e-05 0.3505833
#> Hornet Sportabout   0.0291387207 1.962251e-03 0.3142051
#> Valiant             0.0204622232 5.938695e-04 0.4234214
#> Duster 360          0.0291387207 1.962251e-03 0.3142051
#> Merc 240D           0.3031007892 8.104701e-02 0.6820166
#> Merc 230            0.3534714185 1.081051e-01 0.7114866
#> Merc 280            0.1620666540 2.505092e-02 0.5928146
#> Merc 280C           0.1620666540 2.505092e-02 0.5928146
#> Merc 450SE          0.4399339584 1.215292e-01 0.8168527
#> Merc 450SL          0.4399339584 1.215292e-01 0.8168527
#> Merc 450SLC         0.4399339584 1.215292e-01 0.8168527
#> Cadillac Fleetwood  0.0003901105 1.069176e-06 0.1246887
#> Lincoln Continental 0.0006210892 2.438182e-06 0.1367472
#> Chrysler Imperial   0.0013477706 9.581084e-06 0.1597352
#> Fiat 128            0.8586336735 4.839299e-01 0.9752116
#> Honda Civic         0.8721726372 4.965559e-01 0.9792530
#> Toyota Corolla      0.8907731132 5.148516e-01 0.9842943
#> Toyota Corona       0.5495335136 2.392563e-01 0.8255387
#> Dodge Challenger    0.1326590106 2.305631e-02 0.4977972
#> AMC Javelin         0.2083618200 4.514071e-02 0.5943850
#> Camaro Z28          0.0423551968 3.669526e-03 0.3468861
#> Pontiac Firebird    0.0063241511 1.433281e-04 0.2203136
#> Fiat X1-9           0.8572158735 4.826351e-01 0.9747708
#> Porsche 914-2       0.9969445540 5.469822e-01 0.9999887
#> Lotus Europa        0.7628033616 4.032043e-01 0.9386791
#> Ford Pantera L      0.0408101262 3.449530e-03 0.3433827
#> Ferrari Dino        0.9920773613 4.907503e-01 0.9999385
#> Maserati Bora       0.2281988764 5.137312e-02 0.6174838
#> Volvo 142E          0.5408811326 2.328750e-01 0.8205280

predict_glm(
  object = mod,
  newdata = data.frame(disp = c(150, 300), vs = c(0, 0)),
  type = "link"
)
#>   disp vs  estimate    lower_ci  upper_ci
#> 1  150  0  4.636212 -0.08422335 9.3566470
#> 2  300  0 -1.179736 -2.87171455 0.5122417

predict_glm(
  object = mod,
  newdata = data.frame(disp = c(150, 300), vs = c(0, 0)),
  type = "response"
)
#>   disp vs  estimate   lower_ci  upper_ci
#> 1  150  0 0.9903987 0.47895660 0.9999136
#> 2  300  0 0.2350996 0.05356966 0.6253318