Last active
April 29, 2026 11:54
-
-
Save vjcitn/071e2bf9906e9f834d754bef80743812 to your computer and use it in GitHub Desktop.
simple implementation of logistic regression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #> 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