forked from yixuan/RcppNumerical
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastLR.Rd
More file actions
64 lines (58 loc) · 1.88 KB
/
fastLR.Rd
File metadata and controls
64 lines (58 loc) · 1.88 KB
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
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fastLR.R
\name{fastLR}
\alias{fastLR}
\title{Fast Logistic Regression Fitting Using L-BFGS Algorithm}
\usage{
fastLR(x, y, start = rep(0, ncol(x)), eps_f = 1e-08, eps_g = 1e-05,
maxit = 300)
}
\arguments{
\item{x}{The model matrix.}
\item{y}{The response vector.}
\item{start}{The initial guess of the coefficient vector.}
\item{eps_f}{Iteration stops if \eqn{|f-f'|/|f|<\epsilon_f}{|f-f'|/|f|<eps_f},
where \eqn{f} and \eqn{f'} are the current and previous value
of the objective function (negative log likelihood) respectively.}
\item{eps_g}{Iteration stops if
\eqn{||g|| < \epsilon_g * \max(1, ||\beta||)}{||g|| < eps_g * max(1, ||beta||)},
where \eqn{\beta}{beta} is the current coefficient vector and
\eqn{g} is the gradient.}
\item{maxit}{Maximum number of iterations.}
}
\value{
\code{fastLR()} returns a list with the following components:
\item{coefficients}{Coefficient vector}
\item{fitted.values}{The fitted probability values}
\item{linear.predictors}{The fitted values of the linear part, i.e.,
\eqn{X\hat{\beta}}{X * beta_hat}}
\item{loglikelihood}{The maximized log likelihood}
\item{converged}{Whether the optimization algorithm has converged}
}
\description{
\code{fastLR()} uses the L-BFGS algorithm to efficiently fit logistic
regression. It is in fact an application of the C++ function
\code{optim_lbfgs()} provided by \pkg{RcppNumerical} to perform L-BFGS
optimization.
}
\examples{
set.seed(123)
n = 1000
p = 100
x = matrix(rnorm(n * p), n)
beta = runif(p)
xb = c(x \%*\% beta)
p = 1 / (1 + exp(-xb))
y = rbinom(n, 1, p)
system.time(res1 <- glm.fit(x, y, family = binomial()))
system.time(res2 <- fastLR(x, y))
max(abs(res1$coefficients - res2$coefficients))
}
\seealso{
\code{\link[stats]{glm.fit}()}
}
\author{
Yixuan Qiu \url{https://statr.me}
}
\keyword{models}
\keyword{regression}