Created
June 4, 2015 11:43
-
-
Save jaehyeon-kim/0e35709d609eec74f35f to your computer and use it in GitHub Desktop.
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
## linear regression | |
ols <- lm(speed ~ dist, data = cars) | |
olsCoef <- ols$coefficients | |
# prediction is made by X * beta or beta_0 + beta_1*x_1+ ... + beta_k*x_k | |
olsX <- model.matrix(speed ~ dist, data = cars) | |
olsPred <- olsX %*% olsCoef | |
as.vector(head(olsPred)) | |
[1] 8.615041 9.939581 8.946176 11.926392 10.932987 9.939581 | |
head(predict(ols, newdata = cars)) | |
1 2 3 4 5 6 | |
8.615041 9.939581 8.946176 11.926392 10.932987 9.939581 | |
## logistic regression | |
library(logistf) | |
data(sex2) | |
lg <- logistf(case ~ age+oc+vic+vicl+vis+dia, data = sex2) | |
lgCoef <- lg$coefficients | |
# prediction is made by g(X * beta) or g(beta_0 + beta_1*x_1+ ... + beta_k*x_k) - g() is a link function, search canonical link function of logistic regression | |
lgX <- model.matrix(case ~ age+oc+vic+vicl+vis+dia, data = sex2) | |
lgPred <- 1/(1+exp(-lgX %*% lgCoef)) | |
as.vector(head(lgPred)) | |
[1] 0.3389307 0.9159945 0.9159945 0.9159945 0.9159945 0.9159945 | |
head(lg$predict) | |
[1] 0.3389307 0.9159945 0.9159945 0.9159945 0.9159945 0.9159945 | |
# prediction on new data - different prediction if different data is added | |
newSex <- sex2[3:4,] | |
lgXNew <- model.matrix(case ~ age+oc+vic+vicl+vis+dia, data = newSex) | |
lgPredNew <- 1/(1+exp(-lgXNew %*% lgCoef)) | |
as.vector(lgPredNew) | |
[1] 0.9159945 0.9159945 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment