forked from BruceJillis/Titanic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.logit.single.r
31 lines (22 loc) · 1.04 KB
/
example.logit.single.r
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
# See for context:
# http://ww2.coastal.edu/kingw/statistics/R-tutorials/logistic.html
# Logistic Regression: One Numerical Predictor
library("MASS")
data(menarche)
str(menarche)
summary(menarche)
plot(Menarche / Total ~ Age, data=menarche)
glm.out = glm(
cbind(Menarche, Total - Menarche) ~ Age,
family=binomial(logit),
data=menarche
)
plot(Menarche / Total ~ Age, data=menarche)
lines(menarche$Age, glm.out$fitted, type="l", col="red")
title(main="Menarche Data with Fitted Logistic Regression Line")
# Recall that the response variable is log odds, so the coefficient of "Age" can be interpreted as "for every one year increase in age the odds of having reached menarche increase by exp(1.632) = 5.11 times."
summary(glm.out)
# also available: glm.out$coef, glm.out$fitted, glm.out$resid, glm.out$effects, anova(glm.out)
# calculate p-value (NOTE. pchisq uses the cumulative distribution! so adjust accordingly)
p = 1 - pchisq(26.703, df=23) # should be ~ 0.269
# Logistic Regression: Multiple Numerical Predictors