R-help to exercise 13 in BSS

# Read the data into a dataframe, give names to the variables, and inspect the data:

names(heart)<-c("agrp","age","chd")

heart

# Check that the data correspond to those given in the exercise.

# Attach the dataframe

attach(heart)

# QUESTION a)

`# Fit a logistic regression model with age group (agrp) as a numeric covariate, inspect the results, and write an analysis of deviance table: `

fit.agrp<-glm(chd~agrp,family=binomial)

summary(fit.agrp)

anova(fit.agrp, test="Chisq")

# Make sure that you understand what the output tells you!

# Use the output to answer the questions in a).

#Comment:

# Note that we above have fitted a model where age group is treated as a numeric covariate.

# Alternatively we may let age group be a categorical covariate by writing "chd~factor(agrg)" in the glm-command.

# Discuss the difference between these two models.

# The difference in deviance between the model where age group in treated as a numeric covariate and the model where it is treated as a categorical covariate, may be used to assess the fit of the first model. How?

# QUESTION b)

# Do similar commands as in a), but with age as covariate:

fit.age<-glm(chd~age,family=binomial)

summary(fit.age)

anova(fit.age, test="Chisq")

# What does this output tell you?

# The estimate for beta1 is much larger for the model in a) than for the model in b). Why?

# Give an interpretation of the estimate of beta 1 for the model in b).

# QUESTION c)

`# We then test whether a second order term for age will improve the fit:`
`fit.age2<-glm(chd~age+I(age^2),family=binomial)`
`summary(fit.age2)`
`anova(fit.age, fit.age2,test="Chisq")`
` `

# Make sure that you understand what the output tells you!

# Use the output to answer the question in c).

` `