###################################################################### # STAT 7030 - Categorical Data Analysis # Peng Zeng (Auburn University) # 2024-01-19 ###################################################################### ###################################################################### # explain iteratively reweighted least squares ###################################################################### y = c(1, 1, 1, 0, 0) x = c(3, 2, 4, 3, 1) glm(y ~ x, family = binomial) # initial value from least squares fit0 = lm(y ~ x) b = coef(fit0) cat("iter =", 0, "b =", b, "\n") for(i in 1:10) { prob = exp(b[1] + b[2] * x) / (1 + exp(b[1] + b[2] * x)) w = prob * (1 - prob) z = (b[1] + b[2] * x) + (y - prob) / w b = coef(lm(z ~ x, weights = w)) cat("iter =", i, "b =", b, "\n") } ###################################################################### # THE END ######################################################################