---
title: "Detecting separation and infinite estimates in binomial response GLMs"
author: "[Ioannis Kosmidis](http://www.ikosmidis.com)"
date: "16 May 2017"
output: rmarkdown::html_vignette
bibliography: brglm2.bib
vignette: >
  %\VignetteIndexEntry{Detecting separation and infinite estimates in binomial response GLMs}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 6
)
```


# The **brglm2** package

Along with methods for improving the estimation of generalized linear models (see [`iteration`](https://cran.r-project.org/package=brglm2/brglm2.pdf) vignette), [**brglm2**](https://github.com/ikosmidis/brglm2) provides *pre-fit* and *post-fit* methods for the detection of separation and of infinite maximum likelihood estimates in binomial response generalized linear models.

The key methods are `detect_separation` and `check_infinite_estimates` and this vignettes describes their use.

# Checking for infinite estimates

@heinze:02 used a logistic regression model to analyze data from a
study on endometrial cancer. @agresti:15[, Section 5.7] provide details on the data set. Below, we
fit a probit regression model with the same linear predictor as the
logistic regression model in @heinze:02.
```{r, echo = TRUE, eval = TRUE}
library("brglm2")
data("endometrial", package = "brglm2")
modML <- glm(HG ~ NV + PI + EH, family = binomial("probit"), data = endometrial)
theta_mle <- coef(modML)
summary(modML)
```
As is the case for the logistic regression in @heinze:02, the maximum
likelihood (ML) estimate of the parameter for `NV` is actually
infinite. The reported, apparently finite value is merely due to false
convergence of the iterative estimation procedure. The same is true
for the estimated standard error, and, hence the value `r
round(coef(summary(modML))["NV", "z value"], 3)` for the $z$-statistic
cannot be trusted for inference on the size of the effect for `NV`.

‘@lesaffre:89[, Section 4] describe a procedure that can hint on the occurrence of infinite estimates. In particular, the model is successively refitted, by increasing the maximum number of allowed IWLS iterations at east step. At east step the estimated asymptotic standard errors are divided to the corresponding ones from the first fit. If the sequence of ratios diverges, then the maximum likelihood estimate of the corresponding parameter is minus or plus infinity. The following code chunk applies this process to `modML`.
```{r, echo = TRUE, eval = TRUE}
check_infinite_estimates(modML)
```
Clearly, the ratios of estimated standard errors diverge for `NV`.

# Detecting separation
`detect_separation` tests for the occurrence of complete or
quasi-complete separation in datasets for binomial response
generalized linear models, and finds which of the parameters will have
infinite maximum likelihood estimates. `detect_separation` relies on
the linear programming methods developed in @konis:07.

`detect_separation` is *pre-fit* method, in the sense that it does not need to estimate the model to detect separation and/or identify infinite estimates. For example
```{r, eval = TRUE, echo = TRUE}
endometrial_sep <- glm(HG ~ NV + PI + EH, data = endometrial,
                       family = binomial("logit"),
                       method = "detect_separation")
endometrial_sep
```
The `detect_separation` method reports that there is separation in the data, that the estimates for `(Intercept)`, `PI` and `EH` are finite (coded 0), and that the estimate for  `NV` is plus infinity. So, the actual maximum likelihood estimates are
```{r, echo = TRUE,  eval = TRUE}
coef(modML) + endometrial_sep$betas
```
and the estimated standard errors are
```{r, echo = TRUE,  eval = TRUE}
coef(summary(modML))[, "Std. Error"] + abs(endometrial_sep$betas)
```

# Citation
If you found this vignette or **brglm2**, in general, useful, please consider citing **brglm2** and the associated paper. You can find information on how to do this by typing `citation("brglm2")`.

# References
