Logistic Regression & Effect Plots

Excercise 7.5:

The dataset “Caesar” in vcdExtra gives a 3x2 frequency table classifying 251 women who gave birth by Caesarian section by infection and risk, whether antibiotics were used, and whether the Caesarian section was planned or not. Consider infection as the binary response variable.

data("Caesar", package="vcdExtra")
str(Caesar)
##  table [1:3, 1:2, 1:2, 1:2] 0 1 17 0 1 1 11 17 30 4 ...
##  - attr(*, "dimnames")=List of 4
##   ..$ Infection  : chr [1:3] "Type 1" "Type 2" "None"
##   ..$ Risk       : chr [1:2] "Yes" "No"
##   ..$ Antibiotics: chr [1:2] "Yes" "No"
##   ..$ Planned    : chr [1:2] "Yes" "No"

Create a binary outcome variable with response as infection or no infection.

Caesar.df= as.data.frame(Caesar)
Caesar.df$Infect=as.numeric(Caesar.df$Infection %in% c("Type 1", "Type 2"))

Reorder level of factors with “No” as the baseline level for the rest of the predictors.

Caesar.df$Risk=relevel(x = Caesar.df$Risk,ref = "No")
Caesar.df$Antibiotics=relevel(x = Caesar.df$Antibiotics,ref = "No")
Caesar.df$Planned=relevel(x = Caesar.df$Planned,ref = "No")
str(Caesar.df)
## 'data.frame':    24 obs. of  6 variables:
##  $ Infection  : Factor w/ 3 levels "Type 1","Type 2",..: 1 2 3 1 2 3 1 2 3 1 ...
##  $ Risk       : Factor w/ 2 levels "No","Yes": 2 2 2 1 1 1 2 2 2 1 ...
##  $ Antibiotics: Factor w/ 2 levels "No","Yes": 2 2 2 2 2 2 1 1 1 1 ...
##  $ Planned    : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Freq       : num  0 1 17 0 1 1 11 17 30 4 ...
##  $ Infect     : num  1 1 0 1 1 0 1 1 0 1 ...

A) Fit the main effects logit model for the binary response “Infect”. Note that with the data in the form of frequency data frame you wil need to use weights=freq.

(Caesar.model= glm(Infect ~ Risk + Antibiotics + Planned, weights = Freq, 
                  data= Caesar.df, family = "binomial" ))
## 
## Call:  glm(formula = Infect ~ Risk + Antibiotics + Planned, family = "binomial", 
##     data = Caesar.df, weights = Freq)
## 
## Coefficients:
##    (Intercept)         RiskYes  AntibioticsYes      PlannedYes  
##        -0.7935          1.8270         -3.0011         -0.9064  
## 
## Degrees of Freedom: 16 Total (i.e. Null);  13 Residual
## Null Deviance:       300.9 
## Residual Deviance: 236.4     AIC: 244.4

B) Use summary or ANOVA to test the effects of this model.

summary(Caesar.model)
## 
## Call:
## glm(formula = Infect ~ Risk + Antibiotics + Planned, family = "binomial", 
##     data = Caesar.df, weights = Freq)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -6.7471  -0.4426   0.0000   3.2338   5.4201  
## 
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)     -0.7935     0.4785  -1.658   0.0972 .  
## RiskYes          1.8270     0.4364   4.186 2.84e-05 ***
## AntibioticsYes  -3.0011     0.4593  -6.535 6.37e-11 ***
## PlannedYes      -0.9064     0.4084  -2.219   0.0265 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 300.85  on 16  degrees of freedom
## Residual deviance: 236.36  on 13  degrees of freedom
## AIC: 244.36
## 
## Number of Fisher Scoring iterations: 6

C) Interprate the coefficients in the fitted model in terms of their effect in the odds of infection.

The existence of risk increases the probability of getting infected by 521%. Taking antibiotics decreases the probability of infection by 95% and a planned caesarian section also decreases probabilities of infection by 60%.

(effect <- exp(coef(Caesar.model)) - 1)
##    (Intercept)        RiskYes AntibioticsYes     PlannedYes 
##     -0.5477367      5.2151576     -0.9502659     -0.5960215
(effect2<- paste(round(100*effect, 2), "%", sep=""))
## [1] "-54.77%" "521.52%" "-95.03%" "-59.6%"

D) Make one or more effect plots for this model, showing separate terms, or their combinations.

The visual representation of our model confirms that the odds of infection increase substantially when risk is present and decrease when antibiotics are taken as well as when caesarian is planned.

library(effects)
## Warning: package 'effects' was built under R version 3.4.4
## Loading required package: carData
## Warning: package 'carData' was built under R version 3.4.4
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
plot(allEffects(Caesar.model), rows=1, cols=3)

Taking antibiotics is a good way to reduce the odds of infeciton when risk is present. The combined plot below proves such effect. All patientes with risk present should be taking antibiotics.

Caesar.model2=update(Caesar.model, .~. + Risk:Antibiotics)
plot(allEffects(Caesar.model2))