-
Notifications
You must be signed in to change notification settings - Fork 82
/
newton_irls.R
180 lines (144 loc) · 3.8 KB
/
newton_irls.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#' ---
#' title: " GLM estimation"
#' subtitle: "Newton and IRLS"
#' author: "Michael Clark"
#' css: '../other.css'
#' highlight: pygments
#' date: ""
#' ---
#'
#' # GLM estimation examples
#' Examples of maximum likelihood estimation via a variety of means. See the
#' gradientdescent.R script for that approach. Here we demonstrate Newton's and
#' Iterated Reweighted Least Squares approaches via logistic regression.
#'
#'
#' For the following, I had Murphy's PML text open and more or less followed the
#' algorithms in chapter 8. Note that for Newton's method, this doesn't
#' implement a line search to find a more optimal stepsize at a given iteration.
#'
#' # Data Prep
#'
#' Predict graduate school admission based on gre, gpa, and school rank
#' (higher=more prestige). See corresponding demo here:
#' https://stats.idre.ucla.edu/stata/dae/logistic-regression/. The only
#' difference is that I treat rank as numeric rather than categorical.
admit = haven::read_dta('https://stats.idre.ucla.edu/stat/stata/dae/binary.dta')
comparison_model = glm(admit ~ gre + gpa + rank, data = admit, family = binomial)
summary(comparison_model)
X = model.matrix(comparison_model)
y = comparison_model$y
#' # Newton's method
newton <- function(
X,
y,
tol = 1e-12,
iter = 500,
stepsize = .5
) {
# Args:
# X: model matrix
# y: target
# tol: tolerance
# iter: maximum number of iterations
# stepsize: (0, 1)
# intialize
int = log(mean(y) / (1 - mean(y))) # intercept
beta = c(int, rep(0, ncol(X) - 1))
currtol = 1
it = 0
ll = 0
while (currtol > tol && it < iter) {
it = it +1
ll_old = ll
mu = plogis(X %*% beta)[,1]
g = crossprod(X, mu-y) # gradient
S = diag(mu*(1-mu))
H = t(X) %*% S %*% X # hessian
beta = beta - stepsize * solve(H) %*% g
ll = sum(dbinom(y, prob = mu, size = 1, log = TRUE))
currtol = abs(ll - ll_old)
}
list(
beta = beta,
iter = it,
tol = currtol,
loglik = ll
)
}
newton_result = newton(
X = X,
y = y,
stepsize = .9,
tol = 1e-8 # tol set to 1e-8 as in glm default
)
newton_result
comparison_model
rbind(
newton = unlist(newton_result),
glm_default = c(
beta = coef(comparison_model),
comparison_model$iter,
tol = NA,
loglik = -logLik(comparison_model)
)
)
#' # IRLS
#' Note that `glm` is actually using IRLS, so the results from this should be
#' fairly spot on.
irls <- function(X, y, tol = 1e-12, iter = 500) {
# intialize
int = log(mean(y) / (1 - mean(y))) # intercept
beta = c(int, rep(0, ncol(X) - 1))
currtol = 1
it = 0
ll = 0
while (currtol > tol && it < iter) {
it = it + 1
ll_old = ll
eta = X %*% beta
mu = plogis(eta)[,1]
s = mu * (1 - mu)
S = diag(s)
z = eta + (y-mu)/s
beta = solve(t(X) %*% S %*% X) %*% (t(X) %*% (S %*% z))
ll = sum(
dbinom(
y,
prob = plogis(X %*% beta),
size = 1,
log = T
)
)
currtol = abs(ll - ll_old)
}
list(
beta = beta,
iter = it,
tol = currtol,
loglik = ll,
weights = plogis(X %*% beta) * (1 - plogis(X %*% beta))
)
}
#' `tol` set to 1e-8 as in `glm` default.
irls_result = irls(X = X, y = y, tol = 1e-8)
str(irls_result)
comparison_model
#' # Comparison
#'
#' Compare all results.
rbind(
newton = unlist(newton_result),
irls = unlist(irls_result[-length(irls_result)]),
glm_default = c(
beta = coef(comparison_model),
comparison_model$iter,
tol = NA,
loglik = logLik(comparison_model)
)
)
#' compare weights
head(cbind(irls_result$weights,
comparison_model$weights))
#' # Source
#' Base R source code found at https://github.com/m-clark/Miscellaneous-R-Code/blob/master/ModelFitting/newton_irls.R