1.12 An application

Here we present an application of what we have learned in this chapter.

Consider the binary regression model, where we observe random variables \(Y_i\) that take on the values 0 or 1. The distribution of \(Y_i\) depends on the value of a covariate \(X_i\), \[ P(Y_i=1\mid X_i=x_i) = s(\beta x_i), \] where \(\beta\) is a parameter and \[ s(x) = \frac{e^{x}}{1+e^{x}} \] is the logistic function. We have a sample of size \(n=\) 1000 and we would like to do inference on \(\beta\).

First we plot our data.
Observed sample

Figure 1.8: Observed sample

We will estimate \(\beta\) by maximum likelihood, so we begin by writing the likelihood of observation \(i\), \[ L_i(\beta) = s(\beta x_i)^{y_i}\left( 1-s(\beta x_i)\right)^{(1-y_i)} . \] So that the log-likelihood of the \(i\)th observation is \[ l_i(\beta) = y_i\ln s(\beta x_i) + (1-y_i)\ln ( 1-s(\beta x_i)). \] Since we assume that our observations are iid, the total log-likelihood is then \[ l(\beta) = \sum_{i=1}^n l_i(\beta). \] Let us implement what we have so far.

s <- function(x) {
  exp(x) / (exp(x) + 1)
}

logLn <- function(beta, data){
  x <- data$x
  y <- data$y
  
  s <- s(x * beta)
  
  sum(y * log(s) + (1 - y) * log(1 - s))
}

To maximize the likelihood there are now two options. Either we ask the computer to solve \[ \underset{\beta}{\text{argma}x}~ l(\beta), \] or we calculate and solve \(l'(\beta)=0\). For practice we do both ways here.

optimResult <- optimize(
  logLn,
  lower = 0.0, 
  upper = 3.0,
  data = data.df,
  maximum = TRUE
)

betahat <- optimResult$maximum
betahat
## [1] 1.791579

For the second way, we need \[\begin{align} s'(x) &= \frac{e^x}{\left(1+e^x\right)^2},\\ l'(\beta) &= \frac{x y s'(x \beta )}{s(x \beta )}-\frac{x (1-y) s'(x \beta )}{1-s(x \beta )}. \end{align}\] In R:

sp <- function(x){ exp(x)/(1+exp(x))^2 }

logLp <- function(beta, data){
  x <- data$x
  y <- data$y
  
  s <- s(x * beta)
  sp <- sp(x * beta)
  
  sum( -x * (1-y) * sp / (1-s) + x * y * sp / s )
}

rootResults <- uniroot(
  logLp,
  interval = c(0,3),
  data = data.df
)
rootResults$root
## [1] 1.791589

Both methods giving the same result.

To confirm that we indeed found the MLE we plot the log-likelihood.

## Warning in geom_point(aes(x = betahat, y = logLn(betahat, data.df)), colour = palette.colors(2), : All aesthetics have length 1, but the data has 2 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.
Log likelihood of the sample

Figure 1.9: Log likelihood of the sample

Observed sample and fitted model

Figure 1.10: Observed sample and fitted model

Now we turn to hypothesis testing. Let us say we want to test \(H_0: \beta = 2\) against \(H_1:\beta \neq 2\). First we do the asymptotic likelihood ratio test. So we need to calculate \(\lambda_{\text{LR}}\):

lr <- function(beta0, data){
  2*(logLn(betahat, data) - logLn(beta0, data))
}
lr(2.0, data.df)
## [1] 2.97212

If \(H_0\) is true, this is an observation of a \(\chi_1^2\)-distributed random variable. Therefore the p-value is

1 - pchisq(lr(2.0, data.df), 1)
## [1] 0.0847108

Next we do a Wald’s test. For this we need an estimate of the standard deviation of the MLE. Perhaps the easiest way is to calculate the Fisher information, that is \(-l''(\beta)\). Here there are again two options, we can do it numerically or exactly. First we calculate it numerically:

observedFisherInfo <- function(beta, data){
  drop(-pracma::hessian(logLn, beta, data = data))
}
observedFisherInfo(betahat, data.df)
## [1] 72.6459

Calculating the second derivative exactly involves more work but is preferable whenever possible. We get, \[ l''(\beta)=(1-y) \left(-\frac{x^2 s '(x \beta )^2}{(1-s (x \beta )^2}-\frac{x^2 s ''(x \beta )}{1-s (x \beta )}\right)+y \left(-\frac{x^2 s '(x \beta )^2}{s (x \beta )^2}+\frac{x^2 s ''(x \beta )}{s (x \beta )}\right) \] Implemented in R:

spp <- function(x){ -exp(x)*(exp(x)-1)/(exp(x)+1)^3 }

logLpp <- function(beta, data){
    x <- data$x
    y <- data$y
    
    s <- s(x*beta)
    sp <- sp(x*beta)
    spp <- spp(x*beta)

    sum( (1-y)*(-x^2*sp^2/(1-s)^2 - x^2*spp/(1-s))+y*(-x^2*sp^2/s^2 + x^2*spp/s) )
}

observedFisherInfo <- function(beta, data){
  -logLpp(beta, data)
}

observedFisherInfo(betahat, data.df)
## [1] 72.64591

Recall that Wald’s test statistic is standard normal under \(H_0\). So we may calculate the p-value:

zWald <- function(beta0, data){
  abs(betahat- beta0)*sqrt(observedFisherInfo(betahat, data))
  }
2 * ( 1 - pnorm( zWald(2.0, data.df) ) )
## [1] 0.07566311

We might also do a Score test. Here, all we need is \(l'\) and \(l''\), which we have already calculated. The score statistic is again standard normal under \(H_0\).

zScore <- function(beta0, data){
  abs(pracma::grad(logLn, beta0, data = data) / sqrt(observedFisherInfo(beta0, data)))
}
2 * ( 1 - pnorm(zScore(2.0, data.df) ) )
## [1] 0.0754018

Lastly, we might calculate a CI on \(\beta\). Using the Wald’s statistic, this would be:

alpha <- 0.05
leftCILimit <- betahat - qnorm(1-alpha/2) / sqrt(observedFisherInfo(betahat, data.df))
rightCILimit <- betahat + qnorm(1-alpha/2) / sqrt(observedFisherInfo(betahat, data.df))

leftCILimit
## [1] 1.561624
rightCILimit
## [1] 2.021534
For a score based CI we first plot the score statistic.
Observed sample and fitted model

Figure 1.11: Observed sample and fitted model

We need to find the points where the score statistic is \(z_{\alpha/2}\), which are the limits of the CI.

alpha = 0.05

f <- function(beta, data){
  zScore(beta, data) - qnorm(1-alpha/2)
}

rootResults <- uniroot(f, interval = c(betahat-1,betahat), data = data.df)
leftCILimit <- rootResults$root

rootResults <- uniroot(f, interval = c(betahat+1,betahat), data = data.df)
rightCILimit <- rootResults$root

leftCILimit
## [1] 1.561916
rightCILimit
## [1] 2.021279