















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















H. Milton Stewart School of Industrial and Systems Engineering
Data
Training Data (4,314 Observations)
Testing Data (1,000 Observations)
Objective :
Survey (NHANES) to create a model used to predict
likelihood of obesity.
Variables :
obese
Data
Training Data (4,314 Observations)
Testing Data (1,000 Observations)
Age variable
in the data
like
## Exploratory data analysis: Response vs Predictors
tb_obage = xtabs(~obesityind+agegr)
tb_obgender = xtabs(~obesityind+gender)
tb_obedu = xtabs(~obesityind+edu)
barplot(prop.table(tb_obage), axes=T, space=0.3, horiz=T,
xlab="Proportion of Not Obese (blue) vs Obese (Brown)",
col=c("blue","brown"), main="Obesity by Age Group")
barplot(prop.table(tb_obgender), axes=T, space=0.3, horiz=T,
xlab="Proportion of Not Obese (blue) vs Obese (Brown)",
col=c("blue","brown"), main="Obesity by Gender")
barplot(prop.table(tb_obedu), axes=T, space=0.3, horiz=T,
xlab="Proportion of Not Obese (blue) vs Obese (Brown)",
col=c("blue","brown"), main="Obesity by Education Level")
## Fit a logistic regression model
model = glm(Obesity~agegr+gender+edu, family=binomial)
summary(model)
…
…
Null deviance: 5739.9 on 4313 degrees of freedom
Residual deviance: 5641.3 on 4304 degrees of freedom
…
Estimate Std. Error z value Pr(>|z|)
(Intercept) - 1.20581 0.15730 - 7.666 1.78e-14 ***
agegr25to34 0.47271 0.14428 3.276 0.001052 **
agegr35to44 0.76486 0.14196 5.388 7.13e-08 ***
agegr45to64 0.84815 0.13240 6.406 1.49e-10 ***
agegr65+ 0.60086 0.13751 4.370 1.24e-05 ***
genderFemale 0.23041 0.06363 3.621 0.000293 ***
edu9to11Grade 0.05632 0.12229 0.461 0.
eduHighSchool - 0.03440 0.11436 - 0.301 0.
eduSomeCollege 0.13947 0.11036 1.264 0.
eduCollege+ - 0.40077 0.11757 - 3.409 0.000653 ***
𝜷 𝒂𝒈𝒆𝒈𝒓𝟐𝟓𝒕𝒐𝟑𝟒
= 𝟎. 𝟒𝟕𝟐𝟕
The ratio of the odds of obesity for age
group 25-34 versus the age group 18- 24
is exp(0.4727) = 1.604 (or, equivalently
the log odds ratio is 0.4727), holding all
other predicting variables fixed. Odds of
obesity for age group 25-34 are 60.4%
higher than for age group 18- 24
(baseline group).
𝜷 𝒈𝒆𝒏𝒅𝒆𝒓𝒇𝒆𝒎𝒂𝒍𝒆
= 𝟎. 𝟐𝟑𝟎𝟒
The ratio of the odds of obesity for
females versus males is 1.259, holding
all other predicting variables fixed. Odds
of obesity for females is 26% higher than
for males.
## Fit a logistic regression model
model = glm(Obesity~agegr+gender+edu, family=binomial)
summary(model)
…
…
Null deviance: 5739.9 on 4313 degrees of freedom
Residual deviance: 5641.3 on 4304 degrees of freedom
…
Estimate Std. Error z value Pr(>|z|)
(Intercept) - 1.20581 0.15730 - 7.666 1.78e-14 ***
agegr25to34 0.47271 0.14428 3.276 0.001052 **
agegr35to44 0.76486 0.14196 5.388 7.13e-08 ***
agegr45to64 0.84815 0.13240 6.406 1.49e-10 ***
agegr65+ 0.60086 0.13751 4.370 1.24e-05 ***
genderFemale 0.23041 0.06363 3.621 0.000293 ***
edu9to11Grade 0.05632 0.12229 0.461 0.
eduHighSchool - 0.03440 0.11436 - 0.301 0.
eduSomeCollege 0.13947 0.11036 1.264 0.
eduCollege+ - 0.40077 0.11757 - 3.409 0.000653 ***
## Prediction Accuracy
library(boot)
cost0.5 = function(y, pi){
ypred=rep(0,length(y))
ypred[pi>0.5] = 1
err = mean(abs(y-ypred))
return(err)}
obdata.fr = data.frame(cbind(Obesity, agegr, gender, edu))
## classification error for 10-fold cross-validation
cv.err = cv.glm(obdata.fr, model, cost=cost0.5, K=10)$delta[1]
cv.err = c(cv.err0.3, cv.err0.35, cv.err0.4, cv.err0.45, cv.err0.5,
cv.err0.55, cv.err0.6, cv.err0.65)
## Smallest prediction error is 0.
plot(c(0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65), cv.err,
type="l", lwd=3, xlab="Threshold", ylab="CV Classification Error")
Prediction accuracy is highest and equal for thresholds above 0.5. Why?
predictions with 0 (that is, predict everyone is not obese).
prediction without modeling.
accuracy if we were to predict everyone is not obese.
test data.
### Aggregate data for Logistic Regression with repetitions
obdata.agg.n = aggregate(Obesity~agegr+gender+edu, FUN=length)
obdata.agg.y = aggregate(Obesity~agegr+gender+edu, FUN=sum)
obdata.agg = data.frame(Obesity = obdata.agg.y$Obesity,
Total = obdata.agg.n$Obesity,
agegr = obdata.agg.n$agegr,
gender = obdata.agg.n$gender,
edu = obdata.agg.n$edu)
## Fit a logistic regression model
model.agg = glm(cbind(Obesity,Total-Obesity)~agegr+gender+edu,
data=obdata.agg, family=binomial)
## Test for GOF: Using deviance residuals
c(deviance(model.agg), 1-pchisq(deviance(model.agg),40))
[1] 29.0640209 0.
summary(model.agg)
Coefficients:
Null deviance: 127.701 on 49 degrees of freedom
Residual deviance: 29.064 on 40 degrees of freedom
for estimation and statistical
inference is the same with or
without replications.
output is different with
replications. Why?
Estimate Std. Error z value Pr(>|z|)
(Intercept) - 1.20581 0.15730 - 7.666 1.78e-14 ***
agegr25to34 0.47271 0.14428 3.276 0.001052 **
agegr35to44 0.76486 0.14196 5.388 7.13e-08 ***
agegr45to64 0.84815 0.13240 6.406 1.49e-10 ***
agegr65+ 0.60086 0.13751 4.370 1.24e-05 ***
genderFemale 0.23041 0.06363 3.621 0.000293 ***
edu9to11Grade 0.05632 0.12229 0.461 0.
eduHighSchool - 0.03440 0.11436 - 0.301 0.
eduSomeCollege 0.13947 0.11036 1.264 0.
eduCollege+ - 0.40077 0.11757 - 3.409 0.000653 ***
res = resid(model.agg, type="deviance")
par(mfrow=c(2,2))
boxplot(res~agegr,
xlab="Age Group",
ylab="Std residuals",
data=obdata.agg)
boxplot(res~gender,
xlab="Gender",
ylab="Std residuals",
data = obdata.agg)
qqnorm(res, ylab="Std residuals")
qqline(res, col="blue", lwd=2)
hist(res, 10, xlab="Std residuals", main="")