Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Last active April 29, 2026 11:54
Show Gist options
  • Select an option

  • Save vjcitn/071e2bf9906e9f834d754bef80743812 to your computer and use it in GitHub Desktop.

Select an option

Save vjcitn/071e2bf9906e9f834d754bef80743812 to your computer and use it in GitHub Desktop.
simple implementation of logistic regression
#> dput(dev)
alogit = function(x) exp(x)/(1+exp(x))
dev = function (y, x)
function(b) {
phat = alogit(x %*% b)
-2 * sum(y * log(phat) + (1 - y) * (log(1 - phat)))
}
Y = 1*(iris$Species == "virginica")
X = data.matrix(cbind(1., iris[,1:4]))
b = c(0,0,0,0,0)
devwdata = dev(Y, X)
devwdata(b)
numfit = optim(b, devwdata, method="BFGS", hessian=TRUE)
m1 = glm(Y~X-1, fam=binomial)
summary(m1)
numfit$par
sqrt(diag(solve(numfit$hessian/2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment