## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 
## Attaching package: 'table1'
## 
## 
## The following objects are masked from 'package:base':
## 
##     units, units<-
## 
## 
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
## 
## Loading required package: carData
## 
## 
## Attaching package: 'car'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## 
## The following object is masked from 'package:purrr':
## 
##     some
## 
## 
## # Attaching packages: easystats 0.6.0 (red = needs update)
## ✔ bayestestR  0.13.1   ✔ correlation 0.8.4 
## ✔ datawizard  0.9.0    ✔ effectsize  0.8.6 
## ✔ insight     0.19.6   ✔ modelbased  0.8.6 
## ✖ performance 0.10.5   ✖ parameters  0.21.2
## ✔ report      0.5.7    ✖ see         0.8.0 
## 
## Restart the R-Session and update packages in red with `easystats::easystats_update()`.
## 
## 
## 
## Attaching package: 'modelsummary'
## 
## 
## The following object is masked from 'package:parameters':
## 
##     supported_models
## 
## 
## The following object is masked from 'package:insight':
## 
##     supported_models

The lab specifications can be found [here]

Exercise 1

Use the table1 package to create a table with appropriate descriptive information about the data set. Use the examples provided in the following vignette to help you build a table.

# Write your code here
natality$CIG_REC <- factor(natality$CIG_REC, 
         levels=c("No", "Yes"),
         labels=c("Non-smoker", "Smoker"))

natality$risks <- factor(natality$risks,
       levels=c("No", "Yes"))

natality$MRACEHISP <- factor(natality$MRACEHISP,
       levels=c("Hispanic", "NH White", "NH Black", "NH Other"))

natality$DMAR <- factor(natality$DMAR,
       levels=c("Married", "Unmarried"))

natality$MEDUC <- factor(natality$MEDUC,
       levels=c("lower", "HS", "Some college", "Uni"))

natality$PRIORLIVE <- factor(natality$PRIORLIVE,
                             levels=c("0", "1", "2", "3", "4+"))

natality$PRIORDEAD <- factor(natality$PRIORDEAD,
                             levels=c("0", "1+"))

label(natality$DBWT) <- "Birth Weight"
label(natality$BMI) <- "Mother BMI"
label(natality$MAGER) <- "Mother Age"
label(natality$risks) <- "Risk Factors Reported"
label(natality$MRACEHISP) <- "Mother Race/Hispanic Origin"
label(natality$DMAR) <- "Marital Status"
label(natality$MEDUC) <- "Mother Education"
label(natality$PRIORLIVE) <- "Prior births (living)"
label(natality$PRIORDEAD) <- "Prior births (dead)"

units(natality$DBWT) <- "g"
units(natality$MAGER) <- "years"

table1(~ DBWT + BMI + risks + MAGER + MRACEHISP + DMAR + MEDUC + PRIORLIVE + PRIORDEAD | CIG_REC, overall=F, data = natality)
Non-smoker
(N=1564)
Smoker
(N=124)
Birth Weight (g)
Mean (SD) 3250 (601) 3070 (597)
Median [Min, Max] 3290 [369, 5880] 3090 [630, 4220]
Mother BMI
Mean (SD) 26.9 (6.32) 27.3 (6.42)
Median [Min, Max] 25.6 [15.6, 58.4] 26.3 [16.5, 50.1]
Risk Factors Reported
No 1088 (69.6%) 84 (67.7%)
Yes 476 (30.4%) 40 (32.3%)
Mother Age (years)
Mean (SD) 29.0 (5.79) 27.8 (5.79)
Median [Min, Max] 29.0 [15.0, 49.0] 28.0 [17.0, 42.0]
Mother Race/Hispanic Origin
Hispanic 329 (21.0%) 6 (4.8%)
NH White 844 (54.0%) 94 (75.8%)
NH Black 266 (17.0%) 14 (11.3%)
NH Other 125 (8.0%) 10 (8.1%)
Marital Status
Married 1017 (65.0%) 40 (32.3%)
Unmarried 547 (35.0%) 84 (67.7%)
Mother Education
lower 177 (11.3%) 34 (27.4%)
HS 377 (24.1%) 45 (36.3%)
Some college 437 (27.9%) 37 (29.8%)
Uni 573 (36.6%) 8 (6.5%)
Prior births (living)
0 601 (38.4%) 38 (30.6%)
1 516 (33.0%) 41 (33.1%)
2 265 (16.9%) 17 (13.7%)
3 93 (5.9%) 17 (13.7%)
4+ 89 (5.7%) 11 (8.9%)
Prior births (dead)
0 1551 (99.2%) 123 (99.2%)
1+ 13 (0.8%) 1 (0.8%)

Exercise 2

Use the ggpairs function to explore the association between the different variables. Try to replicate the figure below, or create your own variation.

# Write your code here
natality |>
  ggpairs(c("DBWT", "risks", "MAGER", "DMAR", "BMI"), mapping = ggplot2::aes(color = CIG_REC), title = "Natality in the United States (2018)", columnLabels = c("Birth Weight (g)", "Risk Factors Reported", "Mother Age", "Marital Status", "BMI"),     upper = list(continuous = "density", combo = "facethist", discrete = "count", na = "na"), lower = list(continuous = "cor", combo = "facethist", discrete = "ratio", na = "na"),diag = list(continuous = "densityDiag", discrete = "barDiag", na = "naDiag"), proportions = "auto", legend = c(2,2))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Exercise 3

Fit four linear models, each nested within the other: the null model, the unadjusted model, the full model and the optimal model. Use the step function find the optimal model using the stepwise method. Then use anova to compare the four models. What is your conclusion? Now compare your models using modelsummary function. What does the number in the (brackets) mean? How can you identify the optimal model just by looking at the \(R^2\) and the AIC?

# Write your code here
M.NULL <- lm(DBWT ~ 1, natality)
M.UNADJUSTED <- lm(DBWT ~ CIG_REC, natality)

# The dot represents all variables except the outcome DBWT
M.FULL <- lm(DBWT ~ ., natality)  

# Finding the optimal model
M.OPTIMAL <- step(M.NULL,
     scope = list(upper = M.FULL),
     direction="both",
     data=natality)
## Start:  AIC=21609.55
## DBWT ~ 1
## 
##             Df Sum of Sq       RSS   AIC
## + DMAR       1  28672236 583166851 21530
## + MRACEHISP  3  28557357 583281730 21535
## + PRIORLIVE  4   7485192 604353895 21597
## + MEDUC      3   5189885 606649203 21601
## + CIG_REC    1   3685187 608153901 21601
## + risks      1   3277949 608561138 21602
## + BMI        1   3170730 608668358 21603
## + MAGER      1   2941408 608897679 21603
## + PRIORDEAD  1    849628 610989460 21609
## <none>                   611839087 21610
## 
## Step:  AIC=21530.53
## DBWT ~ DMAR
## 
##             Df Sum of Sq       RSS   AIC
## + MRACEHISP  3  15359037 567807814 21492
## + risks      1   4395289 578771562 21520
## + BMI        1   3607980 579558871 21522
## + PRIORLIVE  4   4500359 578666492 21526
## + CIG_REC    1    978350 582188501 21530
## + PRIORDEAD  1    818812 582348039 21530
## <none>                   583166851 21530
## + MAGER      1     82475 583084376 21532
## + MEDUC      3    313468 582853384 21536
## - DMAR       1  28672236 611839087 21610
## 
## Step:  AIC=21491.48
## DBWT ~ DMAR + MRACEHISP
## 
##             Df Sum of Sq       RSS   AIC
## + BMI        1   4532441 563275373 21480
## + risks      1   3924653 563883161 21482
## + CIG_REC    1   2181695 565626119 21487
## + PRIORLIVE  4   3880005 563927809 21488
## + PRIORDEAD  1    846700 566961114 21491
## <none>                   567807814 21492
## + MAGER      1     52434 567755381 21493
## + MEDUC      3     98014 567709800 21497
## - MRACEHISP  3  15359037 583166851 21530
## - DMAR       1  15473916 583281730 21535
## 
## Step:  AIC=21479.95
## DBWT ~ DMAR + MRACEHISP + BMI
## 
##             Df Sum of Sq       RSS   AIC
## + risks      1   6059709 557215664 21464
## + CIG_REC    1   2362075 560913299 21475
## + PRIORLIVE  4   3457999 559817375 21478
## + PRIORDEAD  1    896345 562379028 21479
## <none>                   563275373 21480
## + MAGER      1    125890 563149483 21482
## + MEDUC      3    115009 563160364 21486
## - BMI        1   4532441 567807814 21492
## - MRACEHISP  3  16283498 579558871 21522
## - DMAR       1  15325756 578601129 21523
## 
## Step:  AIC=21463.69
## DBWT ~ DMAR + MRACEHISP + BMI + risks
## 
##             Df Sum of Sq       RSS   AIC
## + PRIORLIVE  4   5580134 551635530 21455
## + CIG_REC    1   2199370 555016294 21459
## + PRIORDEAD  1   1025923 556189741 21463
## <none>                   557215664 21464
## + MAGER      1     22588 557193076 21466
## + MEDUC      3     69675 557145989 21470
## - risks      1   6059709 563275373 21480
## - BMI        1   6667497 563883161 21482
## - MRACEHISP  3  15921403 573137067 21505
## - DMAR       1  16431163 573646827 21511
## 
## Step:  AIC=21454.7
## DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE
## 
##             Df Sum of Sq       RSS   AIC
## + CIG_REC    1   2707991 548927539 21448
## + PRIORDEAD  1    921529 550714001 21454
## <none>                   551635530 21455
## + MAGER      1    280671 551354859 21456
## + MEDUC      3    173640 551461890 21460
## - PRIORLIVE  4   5580134 557215664 21464
## - BMI        1   6486820 558122350 21472
## - risks      1   8181845 559817375 21478
## - MRACEHISP  3  15145908 566781438 21494
## - DMAR       1  14456460 566091990 21496
## 
## Step:  AIC=21448.4
## DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC
## 
##             Df Sum of Sq       RSS   AIC
## + PRIORDEAD  1    922493 548005046 21448
## <none>                   548927539 21448
## + MAGER      1    337056 548590483 21449
## + MEDUC      3     75431 548852108 21454
## - CIG_REC    1   2707991 551635530 21455
## - PRIORLIVE  4   6088755 555016294 21459
## - BMI        1   6662694 555590232 21467
## - risks      1   8131760 557059299 21471
## - DMAR       1  10955889 559883427 21480
## - MRACEHISP  3  16576572 565504111 21493
## 
## Step:  AIC=21447.56
## DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC + 
##     PRIORDEAD
## 
##             Df Sum of Sq       RSS   AIC
## <none>                   548005046 21448
## - PRIORDEAD  1    922493 548927539 21448
## + MAGER      1    361028 547644018 21448
## + MEDUC      3     82260 547922786 21453
## - CIG_REC    1   2708955 550714001 21454
## - PRIORLIVE  4   5984714 553989760 21458
## - BMI        1   6749558 554754604 21466
## - risks      1   8234365 556239411 21471
## - DMAR       1  10970232 558975278 21479
## - MRACEHISP  3  16594210 564599256 21492
# Displaying the four models by each other
modelsummary(
  list(
    null = M.NULL, 
    unadjasted = M.UNADJUSTED, 
    optimal = M.OPTIMAL,
    full = M.FULL
    ), 
  stars = TRUE
  )
null unadjasted optimal full
(Intercept) 3234.746*** 3247.903*** 3006.067*** 3089.909***
(14.658) (15.187) (72.680) (109.100)
CIG_RECSmoker −179.096** −160.083** −156.593**
(56.032) (55.632) (56.325)
DMARUnmarried −184.275*** −189.782***
(31.823) (35.084)
MRACEHISPNH White 74.492+ 72.464+
(38.075) (39.120)
MRACEHISPNH Black −217.648*** −217.711***
(46.643) (47.533)
MRACEHISPNH Other −47.203 −48.310
(59.382) (60.171)
BMI 10.261*** 10.493***
(2.259) (2.277)
risksYes −158.808*** −152.902***
(31.655) (32.055)
PRIORLIVE1 130.686*** 140.483***
(33.814) (34.725)
PRIORLIVE2 107.170* 123.385**
(41.746) (43.880)
PRIORLIVE3 138.776* 161.298*
(59.972) (63.149)
PRIORLIVE4+ 131.949* 162.508*
(62.333) (67.562)
PRIORDEAD1+ 258.434+ 265.467+
(153.905) (154.184)
MAGER −3.836
(3.085)
MEDUCHS 8.448
(49.909)
MEDUCSome college −0.240
(50.838)
MEDUCUni 31.610
(56.902)
Num.Obs. 1688 1688 1688 1688
R2 0.000 0.006 0.104 0.105
R2 Adj. 0.000 0.005 0.098 0.097
AIC 26401.9 26393.7 26239.9 26246.1
BIC 26412.7 26410.0 26315.9 26343.8
Log.Lik. −13198.942 −13193.843 −13105.946 −13105.039
F 10.217 16.259 12.291
RMSE 602.05 600.23 569.78 569.47
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

The number in the brackets is the Standard Error (SE). The optimal model can be identified by the Adjusted R^2 that is closest to 1 and the AIC (or BIC) that is the lowest. So the M.OPTIMAL is indeed the optimal model.

Exercise 4

Center and scale all the continuous variables, and create a forest plot for the optimal model.

# Write your code here
# Scaling
natality.ctr <- natality |> 
  standardize(two_sd = TRUE)

# Fitting the model
M.OPT.ctr <- lm(
  DBWT ~ DMAR + MRACEHISP + BMI + 
    risks + PRIORLIVE + CIG_REC + 
    PRIORDEAD, 
  data = natality.ctr
  )

# creating a forest plot
M.OPT.ctr %>% 
  model_parameters() %>%
  plot()

The graph represents the values from the optimal model from the previous exercise. The error bars represent the confidence intervals around the point estimates. The vertical dotted line at 0 represents no effect. Coefficients are expressed in terms of 2SD.The variables with red bars suggest a statistically significant negative association with birth weight. The variables with blue bars represent a statistically significant positive association with birth weight.

Exercise 5

How do the effect estimate of smoking, the standard error of its estimate, its 95% confidence interval, and its statistical significance change between the un-adjusted, optimal and full models? Please use the centered and scaled models to answer this question.

# Write your code here
M.UNA.ctr <- lm(
  DBWT ~ CIG_REC, 
  data = natality.ctr
  )

M.FULL.ctr <- lm(
  DBWT ~ ., 
  data = natality.ctr
)

tidy_results <- bind_rows(
  M.UNADJUSTED %>% tidy(conf.int = TRUE) %>% mutate(Model = "UNADJUSTED"),
  M.OPTIMAL %>% tidy(conf.int = TRUE) %>% mutate(Model = "OPTIMAL"),
  M.FULL %>% tidy(conf.int = TRUE) %>% mutate(Model = "FULL"),
  M.UNA.ctr %>% tidy(conf.int = TRUE) %>% mutate(Model = "UNADJUSTED ctr"),
  M.FULL.ctr %>% tidy(conf.int = TRUE) %>% mutate(Model = "FULL ctr"),
  M.OPT.ctr %>% tidy(conf.int = TRUE) %>% mutate(Model = "OPTIMAL ctr")
)

result_table <- tidy_results %>%
  filter(term == "CIG_RECSmoker") %>%
  select(Model, estimate, std.error, statistic, p.value, conf.low, conf.high) %>%
  mutate(across(c(estimate, std.error, statistic, p.value, conf.low, conf.high), ~round(., 3)))

print(result_table)
## # A tibble: 6 × 7
##   Model          estimate std.error statistic p.value conf.low conf.high
##   <chr>             <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 UNADJUSTED     -179.       56.0       -3.20   0.001 -289.      -69.2  
## 2 OPTIMAL        -160.       55.6       -2.88   0.004 -269.      -51.0  
## 3 FULL           -157.       56.3       -2.78   0.005 -267.      -46.1  
## 4 UNADJUSTED ctr   -0.149     0.047     -3.20   0.001   -0.24     -0.057
## 5 FULL ctr         -0.13      0.047     -2.78   0.005   -0.222    -0.038
## 6 OPTIMAL ctr      -0.133     0.046     -2.88   0.004   -0.224    -0.042

The estimate of the effect for smoking during pregnancy becomes closer to zero (less negative) as you move from the unadjusted to the optimal and full models. The standard error slight decreases and the 95% confidence interval becomes tighter, thus indicating increased precision in the estimates. The p-values remain below alpha 0.05, indicating that the effect estimate is statistically significant in all three models. Overall, the effect estimate for smoking changes, becomes more precise, and remains statistically significant as you move from the unadjusted to the optimal and full models, for both centered and scaled.

Exercise 6

Does the association between cigarette smoking during pregnancy and birth weight depend on mother’s education? Modify the FULL model appropriately to be able to answer this question. Use anova to test whether the model with the interaction is significantly better than the one without it. Regardless of statistical significance, create a [dumbell plot] (https://r-charts.com/distribution/dumbbell-plot-ggplot2/) illustrating the smoking effect at each level of mother’s education and estimate the cigarette smoking effect at each level of mother’s education. Hint: When creating the dataset for plotting, include the value zero for each predictor not included in the interaction (the reference level for all categorical variables). Picking the value zero in a centered predictor is like choosing the mean of that variable, which is a reasonable choice.

# Write your code here
M.FULL_int.ctr <- lm(formula = DBWT ~ . + CIG_REC * MEDUC, data = natality.ctr)

modelsummary(
  list(
    full_interaction = M.FULL_int.ctr,
    full = M.FULL.ctr
    ), 
  stars = TRUE
  )
full_interaction full
(Intercept) 0.006 0.022
(0.050) (0.049)
CIG_RECSmoker −0.018 −0.130**
(0.091) (0.047)
risksYes −0.127*** −0.127***
(0.027) (0.027)
MAGER −0.036 −0.037
(0.030) (0.030)
MRACEHISPNH White 0.056+ 0.060+
(0.033) (0.032)
MRACEHISPNH Black −0.183*** −0.181***
(0.040) (0.039)
MRACEHISPNH Other −0.046 −0.040
(0.050) (0.050)
DMARUnmarried −0.157*** −0.158***
(0.029) (0.029)
MEDUCHS 0.026 0.007
(0.045) (0.041)
MEDUCSome college 0.025 0.000
(0.045) (0.042)
MEDUCUni 0.044 0.026
(0.049) (0.047)
PRIORLIVE1 0.117*** 0.117***
(0.029) (0.029)
PRIORLIVE2 0.104** 0.102**
(0.036) (0.036)
PRIORLIVE3 0.129* 0.134*
(0.053) (0.052)
PRIORLIVE4+ 0.135* 0.135*
(0.056) (0.056)
PRIORDEAD1+ 0.225+ 0.220+
(0.128) (0.128)
BMI 0.108*** 0.110***
(0.024) (0.024)
CIG_RECSmoker × MEDUCHS −0.121
(0.117)
CIG_RECSmoker × MEDUCSome college −0.202+
(0.122)
CIG_RECSmoker × MEDUCUni −0.065
(0.192)
Num.Obs. 1688 1688
R2 0.107 0.105
R2 Adj. 0.097 0.097
AIC 2300.6 2297.5
BIC 2414.6 2395.2
Log.Lik. −1129.286 −1130.732
F 10.500 12.291
RMSE 0.47 0.47
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
anova(M.FULL_int.ctr, M.FULL.ctr)
## Analysis of Variance Table
## 
## Model 1: DBWT ~ CIG_REC + risks + MAGER + MRACEHISP + DMAR + MEDUC + PRIORLIVE + 
##     PRIORDEAD + BMI + CIG_REC * MEDUC
## Model 2: DBWT ~ CIG_REC + risks + MAGER + MRACEHISP + DMAR + MEDUC + PRIORLIVE + 
##     PRIORDEAD + BMI
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1   1668 376.70                           
## 2   1671 377.34 -3  -0.64613 0.9537 0.4138
#Dumbell plot
newdata <- data.frame(
  CIG_REC = c("Non-smoker", "Smoker", "Non-smoker", "Smoker", "Non-smoker", "Smoker", "Non-smoker", "Smoker"),  
  MEDUC   = c("lower", "lower", "HS", "HS", "Some college", "Some college", "Uni", "Uni")
  ) |> 
  mutate(
    risks = "No", 
    MAGER = 0, 
    MRACEHISP = "NH White", 
    DMAR = "Married", 
    PRIORLIVE = "0", 
    PRIORDEAD = "0", 
    BMI = 0
    )

newdata |> 
  mutate(
    DBWT = predict(
      M.FULL_int.ctr, 
      newdata
      )
  ) |> select(CIG_REC, MEDUC, DBWT)
##      CIG_REC        MEDUC        DBWT
## 1 Non-smoker        lower  0.06227469
## 2     Smoker        lower  0.04447809
## 3 Non-smoker           HS  0.08854779
## 4     Smoker           HS -0.05014673
## 5 Non-smoker Some college  0.08777361
## 6     Smoker Some college -0.13236808
## 7 Non-smoker          Uni  0.10674030
## 8     Smoker          Uni  0.02404656
tribble(
  ~Education, ~No_Smoking, ~Smoking, 
  "lower",    0.06227469,  0.04447809, 
  "HS",       0.08854779,  -0.05014673, 
  "Some College", 0.08777361, -0.13236808, 
  "Uni",        0.10674030, 0.02404656  
  ) |> ggplot() + 
  geom_segment(
    aes(
      x = "No Smoking", y = No_Smoking, 
      xend = "Smoking", yend = Smoking, 
      color = Education
      ),
    linewidth = 2
    ) + 
  geom_point(
    aes(x = "No Smoking", y = No_Smoking, color = Education), 
    size = 6
    ) + 
  geom_point(
    aes(x = "Smoking", y = Smoking, color = Education), 
    size = 6
    ) +
  scale_y_continuous(
    name = "Birth Weight (centered, scaled)"
  ) +
  labs(
    x = "Cigarette Smoking during Pregnancy",
    color = "Mother's Education"
  )

#Dumbell plot new exercise
library(marginaleffects)

M.FULL_int.ctr.2 <- lm(formula = DBWT ~ . + CIG_REC * MEDUC * MRACEHISP, data = natality.ctr)
summary(M.FULL_int.ctr.2)
## 
## Call:
## lm(formula = DBWT ~ . + CIG_REC * MEDUC * MRACEHISP, data = natality.ctr)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.22854 -0.24972  0.01846  0.30228  1.98938 
## 
## Coefficients: (2 not defined because of singularities)
##                                                    Estimate Std. Error t value
## (Intercept)                                        0.032086   0.059469   0.540
## CIG_RECSmoker                                     -0.069270   0.340915  -0.203
## risksYes                                          -0.125031   0.026836  -4.659
## MAGER                                             -0.041769   0.030027  -1.391
## MRACEHISPNH White                                 -0.040403   0.084498  -0.478
## MRACEHISPNH Black                                 -0.146511   0.105115  -1.394
## MRACEHISPNH Other                                 -0.204099   0.159526  -1.279
## DMARUnmarried                                     -0.157127   0.029367  -5.350
## MEDUCHS                                           -0.008044   0.069699  -0.115
## MEDUCSome college                                 -0.009233   0.073650  -0.125
## MEDUCUni                                          -0.025081   0.084438  -0.297
## PRIORLIVE1                                         0.123213   0.029013   4.247
## PRIORLIVE2                                         0.106148   0.036713   2.891
## PRIORLIVE3                                         0.136886   0.052918   2.587
## PRIORLIVE4+                                        0.136985   0.056643   2.418
## PRIORDEAD1+                                        0.221597   0.128492   1.725
## BMI                                                0.106901   0.024127   4.431
## CIG_RECSmoker:MEDUCHS                              0.242609   0.482162   0.503
## CIG_RECSmoker:MEDUCSome college                   -0.220956   0.483530  -0.457
## CIG_RECSmoker:MEDUCUni                             0.390725   0.444074   0.880
## CIG_RECSmoker:MRACEHISPNH White                    0.194065   0.359732   0.539
## CIG_RECSmoker:MRACEHISPNH Black                   -0.637574   0.447599  -1.424
## CIG_RECSmoker:MRACEHISPNH Other                    0.167193   0.442246   0.378
## MRACEHISPNH White:MEDUCHS                          0.126928   0.103731   1.224
## MRACEHISPNH Black:MEDUCHS                         -0.080748   0.125525  -0.643
## MRACEHISPNH Other:MEDUCHS                          0.191199   0.212284   0.901
## MRACEHISPNH White:MEDUCSome college                0.093750   0.104581   0.896
## MRACEHISPNH Black:MEDUCSome college                0.004403   0.128078   0.034
## MRACEHISPNH Other:MEDUCSome college                0.141468   0.183082   0.773
## MRACEHISPNH White:MEDUCUni                         0.135572   0.108804   1.246
## MRACEHISPNH Black:MEDUCUni                         0.059629   0.138258   0.431
## MRACEHISPNH Other:MEDUCUni                         0.201396   0.182649   1.103
## CIG_RECSmoker:MRACEHISPNH White:MEDUCHS           -0.525266   0.503055  -1.044
## CIG_RECSmoker:MRACEHISPNH Black:MEDUCHS            0.087978   0.613629   0.143
## CIG_RECSmoker:MRACEHISPNH Other:MEDUCHS           -0.382442   0.665429  -0.575
## CIG_RECSmoker:MRACEHISPNH White:MEDUCSome college -0.096510   0.507048  -0.190
## CIG_RECSmoker:MRACEHISPNH Black:MEDUCSome college  0.573840   0.593818   0.966
## CIG_RECSmoker:MRACEHISPNH Other:MEDUCSome college  0.085792   0.657070   0.131
## CIG_RECSmoker:MRACEHISPNH White:MEDUCUni          -0.784876   0.499422  -1.572
## CIG_RECSmoker:MRACEHISPNH Black:MEDUCUni                 NA         NA      NA
## CIG_RECSmoker:MRACEHISPNH Other:MEDUCUni                 NA         NA      NA
##                                                   Pr(>|t|)    
## (Intercept)                                        0.58959    
## CIG_RECSmoker                                      0.83901    
## risksYes                                          3.43e-06 ***
## MAGER                                              0.16440    
## MRACEHISPNH White                                  0.63260    
## MRACEHISPNH Black                                  0.16356    
## MRACEHISPNH Other                                  0.20093    
## DMARUnmarried                                     1.00e-07 ***
## MEDUCHS                                            0.90813    
## MEDUCSome college                                  0.90025    
## MEDUCUni                                           0.76648    
## PRIORLIVE1                                        2.29e-05 ***
## PRIORLIVE2                                         0.00389 ** 
## PRIORLIVE3                                         0.00977 ** 
## PRIORLIVE4+                                        0.01570 *  
## PRIORDEAD1+                                        0.08479 .  
## BMI                                               1.00e-05 ***
## CIG_RECSmoker:MEDUCHS                              0.61491    
## CIG_RECSmoker:MEDUCSome college                    0.64776    
## CIG_RECSmoker:MEDUCUni                             0.37906    
## CIG_RECSmoker:MRACEHISPNH White                    0.58963    
## CIG_RECSmoker:MRACEHISPNH Black                    0.15451    
## CIG_RECSmoker:MRACEHISPNH Other                    0.70544    
## MRACEHISPNH White:MEDUCHS                          0.22127    
## MRACEHISPNH Black:MEDUCHS                          0.52013    
## MRACEHISPNH Other:MEDUCHS                          0.36789    
## MRACEHISPNH White:MEDUCSome college                0.37015    
## MRACEHISPNH Black:MEDUCSome college                0.97258    
## MRACEHISPNH Other:MEDUCSome college                0.43981    
## MRACEHISPNH White:MEDUCUni                         0.21293    
## MRACEHISPNH Black:MEDUCUni                         0.66632    
## MRACEHISPNH Other:MEDUCUni                         0.27034    
## CIG_RECSmoker:MRACEHISPNH White:MEDUCHS            0.29657    
## CIG_RECSmoker:MRACEHISPNH Black:MEDUCHS            0.88601    
## CIG_RECSmoker:MRACEHISPNH Other:MEDUCHS            0.56555    
## CIG_RECSmoker:MRACEHISPNH White:MEDUCSome college  0.84907    
## CIG_RECSmoker:MRACEHISPNH Black:MEDUCSome college  0.33401    
## CIG_RECSmoker:MRACEHISPNH Other:MEDUCSome college  0.89613    
## CIG_RECSmoker:MRACEHISPNH White:MEDUCUni           0.11624    
## CIG_RECSmoker:MRACEHISPNH Black:MEDUCUni                NA    
## CIG_RECSmoker:MRACEHISPNH Other:MEDUCUni                NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4753 on 1649 degrees of freedom
## Multiple R-squared:  0.1168, Adjusted R-squared:  0.09644 
## F-statistic: 5.739 on 38 and 1649 DF,  p-value: < 2.2e-16
new_data_6 <- predictions(M.FULL_int.ctr.2, 
            newdata = datagrid(
              MRACEHISP = c("NH White", "NH Black", "NH Other", "Hispanic"),
              MEDUC   = c("lower", "HS", "Some college", "Uni"),
              CIG_REC = c("Non-smoker", "Smoker")
              )
            ) |> 
  as.data.frame() |> 
  mutate(DBWT = estimate)  |> 
  select(DBWT, MRACEHISP, CIG_REC, MEDUC)
## Warning: Model matrix is rank deficient. Some variance-covariance parameters are
##   missing.
fig_6a <- tribble(
  ~Education, ~Non_smoker, ~Smoker, 
  "lower",    0.032086047,  -0.037183980, 
  "HS",       0.024041622,  0.197380550, 
  "Some college", 0.022853001,  -0.267373209, 
  "Uni",        0.007004896,  0.328459898
  ) |> ggplot() + 
  geom_segment(
    aes(
      x = "No", y = Non_smoker, 
      xend = "Yes", yend = Smoker, 
      color = Education
      ),
    linewidth = 1
    ) + 
  geom_point(
    aes(x = "No", y = Non_smoker, color = Education), 
    size = 2
    ) + 
  geom_point(
    aes(x = "Yes", y = Smoker, color = Education), 
    size = 2
    ) + 
  labs(
    x = "Cigarette Smoking during Pregnancy",
    color = "Mother's Education",
    title = "Hispanic"
  ) +
  scale_y_continuous(
    name = "Birth Weight"
  )

fig_6b <- tribble(
  ~Education, ~Non_smoker, ~Smoker, 
  "lower",    -0.008317104,  0.116478046, 
  "HS",       0.110566399,  -0.047295141, 
  "Some college", 0.076199617,  -0.116471574, 
  "Uni",        0.102174187,  -0.167181876
  ) |> ggplot() + 
  geom_segment(
    aes(
      x = "No", y = Non_smoker, 
      xend = "Yes", yend = Smoker, 
      color = Education
      ),
    linewidth = 1
    ) + 
  geom_point(
    aes(x = "No", y = Non_smoker, color = Education), 
    size = 2
    ) + 
  geom_point(
    aes(x = "Yes", y = Smoker, color = Education), 
    size = 2
    ) + 
  labs(
    x = "Cigarette Smoking during Pregnancy",
    color = "Mother's Education",
    title = "NH White"
  ) +
  scale_y_continuous(
    name = "Birth Weight"
  )

fig_6c <- tribble(
  ~Education, ~Non_smoker, ~Smoker, 
  "lower",    -0.114424774, -0.821268574, 
  "HS",       -0.203217090, -0.579474229, 
  "Some college", -0.119254504, -0.473214090, 
  "Uni",       -0.079876992,  -0.395995763
  ) |> ggplot() + 
  geom_segment(
    aes(
      x = "No", y = Non_smoker, 
      xend = "Yes", yend = Smoker, 
      color = Education
      ),
    linewidth = 1
    ) + 
  geom_point(
    aes(x = "No", y = Non_smoker, color = Education), 
    size = 2
    ) + 
  geom_point(
    aes(x = "Yes", y = Smoker, color = Education), 
    size = 2
    ) + 
  labs(
    x = "Cigarette Smoking during Pregnancy",
    color = "Mother's Education",
    title = "NH Black"
  ) +
  scale_y_continuous(
    name = "Birth Weight"
  )

fig_6d <- tribble(
  ~Education, ~Non_smoker, ~Smoker, 
  "lower",    -0.172012722, -0.074089899, 
  "HS",         0.011142015,  -0.030768003, 
  "Some college", -0.039778006, -0.077019131, 
  "Uni",     0.004302378, 0.492950230 
  ) |> ggplot() + 
  geom_segment(
    aes(
      x = "No", y = Non_smoker, 
      xend = "Yes", yend = Smoker, 
      color = Education
      ),
    linewidth = 1
    ) + 
  geom_point(
    aes(x = "No", y = Non_smoker, color = Education), 
    size = 2
    ) + 
  geom_point(
    aes(x = "Yes", y = Smoker, color = Education), 
    size = 2
    ) + 
  labs(
    x = "Cigarette Smoking during Pregnancy",
    color = "Mother's Education",
    title = "NH Other"
  ) +
  scale_y_continuous(
    name = "Birth Weight"
  )

library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:lubridate':
## 
##     stamp
plot_grid(fig_6a, fig_6b, fig_6c, fig_6d,
          nrow = 2, align = "hv", axis = "tblr", 
          labels = c("A", "B", "C", "D"), 
          label_size = 10, hjust = -0.1, vjust = 1.2)

There is no significant effect of mother’s education on the association of cigarette smoking and birth weight, reflected by the model summary of the full and the full interaction model. High school vs. lower is the effect is estimated to be 0.121 (p = ns), Some college vs. lower is the effect estimated to be 0.202 (p = ns) and university vs. lower is the effect estimated to be 0.065 (p = ns). The effect is defined as the estimated change in birth weight comparing smokers and non-smokers. As per all the dumbell plots (with and without race): Smoking has a negative effect on birth weight for all education types and all races.

Exercise 7

Does the association between mother’s BMI and birth-weight differ between mothers of different race/ethnicity? Modify the OPTIMAL model appropriately to be able to answer this question, using the dataset with the centered variables. Use anova to test whether the model with the interaction is significantly better than the one without it. Regardless of statistical significance, estimate the BMI effect at each level of mother’s race/ethnicity and plot the BMI effect at each level of mother’s race/ethnicity using a dumbell plot.

# Write your code here
M.OPT_int.ctr <- lm(formula = DBWT ~ DMAR + MRACEHISP +BMI + 
    risks + PRIORLIVE + CIG_REC + 
    PRIORDEAD + BMI * MRACEHISP, data = natality.ctr)
M.OPT.ctr.2 <- lm(DBWT ~ DMAR + MRACEHISP +BMI + 
    risks + PRIORLIVE + CIG_REC + 
    PRIORDEAD, data = natality.ctr)

modelsummary(
  list(
    optimal_interaction = M.OPT_int.ctr,
    optimal = M.OPT.ctr.2
    ), 
  stars = TRUE
  )
optimal_interaction optimal
(Intercept) 0.042 0.040
(0.034) (0.034)
DMARUnmarried −0.155*** −0.153***
(0.026) (0.026)
MRACEHISPNH White 0.061+ 0.062+
(0.032) (0.032)
MRACEHISPNH Black −0.179*** −0.181***
(0.039) (0.039)
MRACEHISPNH Other −0.031 −0.039
(0.050) (0.049)
BMI 0.074 0.108***
(0.054) (0.024)
risksYes −0.132*** −0.132***
(0.026) (0.026)
PRIORLIVE1 0.108*** 0.109***
(0.028) (0.028)
PRIORLIVE2 0.090** 0.089*
(0.035) (0.035)
PRIORLIVE3 0.115* 0.115*
(0.050) (0.050)
PRIORLIVE4+ 0.108* 0.110*
(0.052) (0.052)
CIG_RECSmoker −0.136** −0.133**
(0.046) (0.046)
PRIORDEAD1+ 0.207 0.215+
(0.128) (0.128)
MRACEHISPNH White × BMI 0.046
(0.063)
MRACEHISPNH Black × BMI −0.005
(0.073)
MRACEHISPNH Other × BMI 0.166
(0.108)
Num.Obs. 1688 1688
R2 0.106 0.104
R2 Adj. 0.098 0.098
AIC 2294.1 2291.3
BIC 2386.4 2367.3
Log.Lik. −1130.048 −1131.640
F 13.219 16.259
RMSE 0.47 0.47
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
anova(M.OPT_int.ctr, M.OPT.ctr.2)
## Analysis of Variance Table
## 
## Model 1: DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC + 
##     PRIORDEAD + BMI * MRACEHISP
## Model 2: DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC + 
##     PRIORDEAD
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1   1672 377.04                           
## 2   1675 377.75 -3  -0.71163 1.0519 0.3685
#Dumbell plot
mean_bmi <- 0
sd_bmi <- 2

newdata <- data.frame(
   BMI = c(mean_bmi, mean_bmi + sd_bmi, mean_bmi, mean_bmi + sd_bmi, mean_bmi, mean_bmi + sd_bmi, mean_bmi, mean_bmi + sd_bmi),   
  MRACEHISP   = c("NH White","NH White","NH Black","NH Black","NH Other","NH Other","Hispanic","Hispanic")
  ) |> 
  mutate(
    risks = "No", 
    DMAR = "Married", 
    PRIORLIVE = "0", 
    PRIORDEAD = "0",
    CIG_REC = "Non-smoker"
    )

newdata |> 
  mutate(
    DBWT = predict(
      M.OPT_int.ctr, 
      newdata
      )
  ) |> select(MRACEHISP, BMI, DBWT)
##   MRACEHISP BMI         DBWT
## 1  NH White   0  0.102843774
## 2  NH White   2  0.342827274
## 3  NH Black   0 -0.136664894
## 4  NH Black   2  0.002678953
## 5  NH Other   0  0.010786473
## 6  NH Other   2  0.491499237
## 7  Hispanic   0  0.042158252
## 8  Hispanic   2  0.190671430
tribble(
  ~Ethnicity, ~mean_BMI, ~mean_BMI_sd_BMI, 
  "Hispanic",    0.042158252,  0.190671430, 
  "Black",      -0.136664894, 0.002678953,
  "Other",       0.010786473,  0.491499237,
  "White",       0.102843774,  0.342827274
  ) |> ggplot() + 
  geom_segment(
    aes(
      x = "mean BMI", y = mean_BMI, 
      xend = "mean BMI + sd BMI", yend = mean_BMI_sd_BMI, 
      color = Ethnicity
      ),
    linewidth = 2
    ) + 
  geom_point(
    aes(x = "mean BMI", y = mean_BMI, color = Ethnicity), 
    size = 6
    ) + 
  geom_point(
    aes(x = "mean BMI + sd BMI", y = mean_BMI_sd_BMI, color = Ethnicity), 
    size = 6
    ) +
  scale_y_continuous(
    name = "Birth Weight (centered, scaled)"
  ) +
  labs(
    x = "Mother's BMI",
    color = "Mother's Ethnicity"
  )

The association of BMI and birth weight does not significantly differ between mothers of different race/ethnicity. White vs. Hispanic is the effect is estimated to be 0.046 (p = ns), Black vs. Hispanic is the effect estimated to be -0.005 (p = ns) and Other vs. Hispanic is the effect estimated to be 0.166 (p = ns). The effect is defined as the estimated change in birth weight comparing the mean BMI and the mean plus standard deviation of BMI. As per the dumbbell plot, it reflects the coefficients found in the model with the interaction group: The steepest line corresponds to the “Other” ethnicity group suggesting that the effect of BMI is estimated to be more positive (high birth weight) in this group. It is also the group with the greatest magnitude of the effect compared to the other ethnic groups. This means that you notice a substantial increase in birth weight for those with mean BMI compared to those one standard deviation above the mean BMI. As for the other ethnic groups, the White ethnic group have on average a higher birth weight than other ethnic groups. This is especially the case with a standard deviation increase to mean BMI. The magnitude of the White, Black and Hispanic groups are the same. The Black ethnic group has the lowest birth weight compared to the other ethnic groups scoring below 0 or near 0 to mean birth weight.

Exercise 8

Does the association between mother’s BMI and birth-weight depend on mother’s age? Modify the FULL model appropriately to be able to answer this question, using the dataset with the centered variables. Use anova to test whether the model with the interaction is significantly better than the one without it. Regardless of statistical significance, estimate the BMI effect for mothers age 20, 30, and 40 years and make a plot illustrating the BMI effect at each of these ages.

# Write your code here
M.FULL_int.ctr <- lm(formula = DBWT ~ . + BMI * MAGER, data = natality.ctr)
M.FULL.ctr <- lm(DBWT ~ ., data = natality.ctr)

modelsummary(
  list(
    full_interaction = M.FULL_int.ctr,
    full = M.FULL.ctr
    ), 
  stars = TRUE
  )
full_interaction full
(Intercept) 0.024 0.022
(0.049) (0.049)
CIG_RECSmoker −0.131** −0.130**
(0.047) (0.047)
risksYes −0.127*** −0.127***
(0.027) (0.027)
MAGER −0.037 −0.037
(0.030) (0.030)
MRACEHISPNH White 0.059+ 0.060+
(0.033) (0.032)
MRACEHISPNH Black −0.180*** −0.181***
(0.039) (0.039)
MRACEHISPNH Other −0.041 −0.040
(0.050) (0.050)
DMARUnmarried −0.158*** −0.158***
(0.029) (0.029)
MEDUCHS 0.007 0.007
(0.041) (0.041)
MEDUCSome college 0.000 0.000
(0.042) (0.042)
MEDUCUni 0.025 0.026
(0.047) (0.047)
PRIORLIVE1 0.116*** 0.117***
(0.029) (0.029)
PRIORLIVE2 0.102** 0.102**
(0.036) (0.036)
PRIORLIVE3 0.134* 0.134*
(0.052) (0.052)
PRIORLIVE4+ 0.135* 0.135*
(0.056) (0.056)
PRIORDEAD1+ 0.217+ 0.220+
(0.128) (0.128)
BMI 0.110*** 0.110***
(0.024) (0.024)
MAGER × BMI −0.024
(0.047)
Num.Obs. 1688 1688
R2 0.105 0.105
R2 Adj. 0.096 0.097
AIC 2299.2 2297.5
BIC 2402.4 2395.2
Log.Lik. −1130.597 −1130.732
F 11.578 12.291
RMSE 0.47 0.47
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
anova(M.FULL_int.ctr, M.FULL.ctr)
## Analysis of Variance Table
## 
## Model 1: DBWT ~ CIG_REC + risks + MAGER + MRACEHISP + DMAR + MEDUC + PRIORLIVE + 
##     PRIORDEAD + BMI + BMI * MAGER
## Model 2: DBWT ~ CIG_REC + risks + MAGER + MRACEHISP + DMAR + MEDUC + PRIORLIVE + 
##     PRIORDEAD + BMI
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1   1670 377.28                           
## 2   1671 377.34 -1 -0.060384 0.2673 0.6052
#Dumbell plot
mean_bmi <- 0
sd_bmi <- 2
MAGER_20 <- 20
MAGER_30 <- 30
MAGER_40 <- 40

newdata_3 <- data.frame(
   BMI = c(mean_bmi, mean_bmi + sd_bmi, mean_bmi, mean_bmi + sd_bmi, mean_bmi, mean_bmi + sd_bmi),   
  MAGER   = c(MAGER_20, MAGER_20, MAGER_30, MAGER_30, MAGER_40, MAGER_40)
  ) |> 
  mutate(
    risks = "No", 
    MEDUC = "lower", 
    MRACEHISP = "NH White", 
    DMAR = "Married", 
    PRIORLIVE = "0", 
    PRIORDEAD = "0", 
    CIG_REC = "Non-smoker"
    )

newdata_3 |> 
  mutate(
    DBWT = predict(
      M.FULL_int.ctr, 
      newdata_3
      )
  ) |> select(MAGER, BMI, DBWT)
##   MAGER BMI       DBWT
## 1    20   0 -0.6575681
## 2    20   2 -1.4076355
## 3    30   0 -1.0279007
## 4    30   2 -2.2629360
## 5    40   0 -1.3982333
## 6    40   2 -3.1182366
tribble(
  ~Mothers_Age, ~mean_BMI, ~mean_BMI_sd_BMI, 
  "20",    -0.6575681,  -1.4076355, 
  "30",      -1.0279007, -2.2629360,
  "40",       -1.3982333,  -3.1182366
  ) |> ggplot() + 
  geom_segment(
    aes(
      x = "mean BMI", y = mean_BMI, 
      xend = "mean BMI + sd BMI", yend = mean_BMI_sd_BMI, 
      color = Mothers_Age
      ),
    linewidth = 2
    ) + 
  geom_point(
    aes(x = "mean BMI", y = mean_BMI, color = Mothers_Age), 
    size = 6
    ) + 
  geom_point(
    aes(x = "mean BMI + sd BMI", y = mean_BMI_sd_BMI, color = Mothers_Age), 
    size = 6
    ) +
  scale_y_continuous(
    name = "Birth Weight (centered, scaled)"
  ) +
  labs(
    x = "Mother's BMI",
    color = "Mother's Age"
  )

The association between mother’s BMI and birth-weight does not significantly depend on the mother’s age (coefficient -0.024 (p = ns)). As well as the fact that the full model without the interaction of BMI and age is a better fit (reflected by a higher R^2 adjusted and lower AIC for the full model). As per the dumbbell plot, it reflects the coefficients found in the model with the interaction group: The steepest line corresponds to the 40-year old age group suggesting that the effect of BMI is estimated to be more negative (lower birth weight) in this group. It is also the group with the greatest magnitude of the effect compared to the other age levels. This means that you notice a substantial decrease in birth weight for those with mean BMI compared to those one standard deviation above the mean BMI. The highest line refers to the 20-year old age group suggesting that the effect of BMI is estimated to be the least negative (higher birth weight) in this group.