my_glm: Solve Generalized Linear Model with Newton-Ralphson Method

Description Usage Arguments Examples

View source: R/my-glm.R

Description

This is a simple algorithm to solve a generalized linear regression model.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
my_glm(
  X,
  Y,
  family,
  step_option = c("constant", "momentum"),
  lambda = 0.001,
  gamma = NULL,
  maxit = 2000,
  tol = 1e-12
)

Arguments

X

design matrix

Y

a vector of outcome

family

an R 'family' object.

step_option

a character string of "constant" or "momentum", if "momentum", then gamma should be specified

lambda

a numeric number indicating the learning rate for gradient descent algorithm

gamma

a fraction for momentum step size

maxit

an integer indicating the maximum number iterations

tol

a numeric number indicating the precision of the algorithm

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
n <- 1000; p <- 3
beta <- c(0.2, 2, 1)
X <- cbind(1, matrix(rnorm(n * (p-1)), ncol = (p-1) ))
mu <- 1 - pcauchy(X %*% beta)
Y <- as.numeric(runif(n) > mu)

df <- as.data.frame(cbind(Y, X))

fit0 <- glm(Y ~ . -1, data = df, family = binomial(link = "cauchit"))
fit1 <- my_glm(X, Y, family = binomial(link = "cauchit"),
               step_option = "constant", lambda = 0.001)
fit2 <- my_glm(X, Y, family = binomial(link = "cauchit"),
               step_option = "momentum", lambda = 0.001, gamma = 0.2)

cbind(fit0$coefficients, fit1$coefficients, fit2$coefficients)

tqchen07/bis557 documentation built on Dec. 21, 2020, 3:06 a.m.