# Set data sets
data <- MASS::birthwt
X <- as.matrix(cbind(1, data[, -1]))
Y <- data[, 1]
n <- dim(X)[1]
p <- dim(X)[2]
beta <- rep(0, p) # Initialize beta
beta1 <- rep(1, p) # Initialize beta1
# fit logit regression using Fisher scoring
while (sum(abs(beta - beta1)) > 0.01) {
beta1 <-beta
# Calculate mu and eta
eta <- X %*% beta1
mu <- exp(eta) / (1 + exp(eta))
# Calculate the derivatives
dmu_deta <- exp(eta) / (( 1 + exp(eta)) ^ 2)
deta_dmu <- 1 / mu + 1 / (1 - mu)
V <- mu * (1 - mu) # Calculate the variance function
W <- diag((dmu_deta ^ 2 / V)[,]) # Calculate the weight matrix
Z <- eta + (Y - mu) * deta_dmu # Calculate Z
beta <- solve(t(X) %*% W %*% X) %*% (t(X) %*% W %*% Z)
}
I am using Iteratively Reweighted Least Square method. The X
and Y
come from the built-in dataset birthwt
. I do not understand why this method does not converge. It always returns a NaN
. But when I remove the intercept, it converges. I know that I can simply use glm
, but I would like to understand the implementation.