---
title: "The 'gfilogisreg' package"
output: 
  rmarkdown::html_vignette:
    css: vignette.css
vignette: >
  %\VignetteIndexEntry{The 'gfilogisreg' package}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

The main function of the 'gfilogisreg' package is `gfilogisreg`. It simulates 
the fiducial distribution of the parameters of a logistic regression model.

To illustrate it, we will consider a logistic dose-response model for inference
on the median lethal dose. The median lethal dose (LD50) is the amount of a 
substance, such as a drug, that is expected to kill half of its users.

The results of LD50 experiments can be modeled using the relation
$$
\textrm{logit}(p_i) = \beta_1(x_i - \mu)
$$
where $p_i$ is the probability of death at the dose administration $x_i$, and 
$\mu$ is the median lethal dose, i.e. the dosage at which the probability of 
death is $0.5$. The $x_i$ are known while $\beta_1$ and $\mu$ are fixed effects 
that are unknown. 

This relation can be written in the form 
$$
\textrm{logit}(p_i) = \beta_0 + \beta_1 x_i
$$
with $\mu = -\beta_0 / \beta_1$.

We will perform the fiducial inference in this model with the following data:

```{r data}
dat <- data.frame(
  x = c(
    -2, -2, -2, -2, -2, 
    -1, -1, -1, -1, -1, 
     0,  0,  0,  0,  0,
     1,  1,  1,  1,  1,
     2,  2,  2,  2,  2
  ),
  y = c(
    1, 0, 0, 0, 0,
    1, 1, 1, 0, 0,
    1, 1, 0, 0, 0,
    1, 1, 1, 1, 0,
    1, 1, 1, 1, 1
  )
)
```

Let's go: 

```{r logisreg, message=FALSE, results='hide'}
library(gfilogisreg)
set.seed(666L)
fidsamples <- gfilogisreg(y ~ x, data = dat, N = 500L)
```

Here are the fiducial estimates and $95\%$-confidence intervals of the 
parameters $\beta_0$ and $\beta_1$:

```{r gfisummary}
gfiSummary(fidsamples)
```

The fiducial estimates are close to the maximum likelihood estimates:

```{r glm}
glm(y ~ x, data = dat, family = binomial())
```

Now let us draw the fiducial $95\%$-confidence interval about our parameter of 
interest $\mu$:

```{r mu}
gfiConfInt(~ -`(Intercept)`/x, fidsamples)
```

