Obligatory exercise 1

For the binary regression model with grouped data and the logit choice of link function, show that the log likelihood, the score function and the Fisher information is given by the expression on p. 283 in Fahrmeir.

Write a function myglm in R that implements the Fisher scoring algorithm for this model. The function should accept a data frame and a model formula as inputs. You're allowed to use the model.matrix to construct the model matrix \(\mathbf{X}\). Assume that the input data frame contains two columns n and y with number of individuals and number of events per group.

You need to choose suitable starting values for the parameter. Your function should also print the parameter values for each iteration of the algorithm.

The function should return a list consisting of two elements:

coefficents: A matrix containing the maximum likelihood estimates of each parameter in the first column and estimates of their standard errors based on the final Fisher information matrix. Assign sensible names to each row and column (colnames( ), rownames( )).

deviance: The deviance of the model, that is twice the difference in log likelihood between the fitted and the saturated model.

Test your function with

  library(investr)
  myglm(formula= ~ldose, data=beetle)

and verify that you get the same results as

  summary(glm(cbind(y,n-y) ~ ldose, binomial, data=beetle))
  

Based on the observed deviance, does the model fit the data reasonably well?

Hints: Use debugonce(myglm) if your function don't seem to work. You may perhaps also want to make use of the apply function (you can also use for loops but these are less efficient in R), elementwise multiplication and recycling, matrix multiplication %*%, matrix inverse solve, diag to create diagonal matrices, as.vector to turn a matrix into a vector. Note also that convergence may be sensitive to the initial values you choose for \(\beta\).

Write your report in latex (with R code and output including via knitr) or R markdown and submit the report as a single pdf file into this dropbox folder. You can work on you own, or in groups of two or three. Remember to include your names on the first page of the report.

Deadline: September 30.

2019-09-30, Jarle Tufto