## Test Date set n<-c(0.15,0.15,0.20,0.28,0.35,0.4,0.44,0.47,0.49,0.51,0.53,0.56,0.56,0.57,0.6,0.58,0.6,0.61,0.6, 0.58,0.54, 0.58) m = c(1:length(n)) df<-as.data.frame(cbind(m,n))
library(ISwR) data(juul) juul$menarche <- factor(juul$menarche, levels=c(1, 2), labels=c("No","Yes")) juul$tanner <- factor(juul$tanner, levels=c(1:5)) juul.girl <- subset(juul, age>8 & age<20 & complete.cases(menarche)) # Note that this is a binomial data, we should use `binomial` arguments here glm_menarche_age <- glm(menarche~age, family=binomial, data=juul.girl) summary(glm_menarche_age)
Call:
glm(formula = menarche ~ age, family = binomial, data = juul.girl)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.32759 -0.18998 0.01253 0.12132 2.45922
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -20.0132 2.0284 -9.867 <2e-16 ***
age 1.5173 0.1544 9.829 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 719.39 on 518 degrees of freedom
Residual deviance: 200.66 on 517 degrees of freedom
AIC: 204.66
Number of Fisher Scoring iterations: 7
library(ggplot2)
X = juul.girl$age Y = predict(glm_menarche_age, data.frame(age=X), type="resp")
# in the level, the "No" is 1, "Yes" is 2. So, we need to add 1 in the Y ggplot(juul.girl, aes(x=age, y =menarche)) + geom_point(color="steelblue", alpha= .5) + geom_line(aes(x=X,y=Y+1), color="salmon", size=2, alpha = .7) + theme_bw()