## ── 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]
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
label(natality$risks) <- "Risks"
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 birhts (dead)"
natality$CIG_REC <- factor(natality$CIG_REC, levels = c("No", "Yes"))
caption <- "Maternal demographics stratified by smoking"
table1(~ DBWT + risks + MAGER + MRACEHISP + DMAR + MEDUC + PRIORLIVE + PRIORDEAD + BMI | CIG_REC, data=natality, overall = F, caption = caption)| No (N=1564) |
Yes (N=124) |
|
|---|---|---|
| Birth Weight (g) | ||
| Mean (SD) | 3250 (601) | 3070 (597) |
| Median [Min, Max] | 3290 [369, 5880] | 3090 [630, 4220] |
| Risks | ||
| No | 1088 (69.6%) | 84 (67.7%) |
| Yes | 476 (30.4%) | 40 (32.3%) |
| Mother Age | ||
| 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 | ||
| NH White | 844 (54.0%) | 94 (75.8%) |
| NH Black | 266 (17.0%) | 14 (11.3%) |
| NH Other | 125 (8.0%) | 10 (8.1%) |
| Hispanic | 329 (21.0%) | 6 (4.8%) |
| 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 birhts (dead) | ||
| 0 | 1551 (99.2%) | 123 (99.2%) |
| 1+ | 13 (0.8%) | 1 (0.8%) |
| 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] |
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: natality$CIG_REC and natality$risks
## X-squared = 0.1043, df = 1, p-value = 0.7467
##
## Pearson's Chi-squared test
##
## data: natality$CIG_REC and natality$MRACEHISP
## X-squared = 27.316, df = 3, p-value = 5.054e-06
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: natality$CIG_REC and natality$DMAR
## X-squared = 51.31, df = 1, p-value = 7.888e-13
##
## Pearson's Chi-squared test
##
## data: natality$CIG_REC and natality$MEDUC
## X-squared = 61.216, df = 3, p-value = 3.231e-13
##
## Pearson's Chi-squared test
##
## data: natality$CIG_REC and natality$PRIORLIVE
## X-squared = 15.145, df = 4, p-value = 0.00441
##
## Fisher's Exact Test for Count Data
##
## data: natality$CIG_REC and natality$PRIORDEAD
## p-value = 1
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.02264252 6.55965443
## sample estimates:
## odds ratio
## 0.9699981
Answer: From this table, we can interpret that there are a total of 1,688 mother of which 124 (7.3%) were smoking during the pregnancy. For the continuous variables Birth Weight, mother’s age and mother’s BMI, we first have to check the distribution to assess if we can use the two-sided t-test or need to use the Mann-Whitney U test. Since the assessment of the variables is included in exercise 2, we will answer if these variables are different between mothers who didn’t smoke during the pregnancy and mother who did smoke during the pregnancy at question 2. For the other variables, we have performed chi-squared tests to test if there are differences between both groups. We found that reported risk factors and prior birth now death are not significantly different between the groups, while mother’s ethnicity, marital stage, mother’s education and prior birth now living were significantly different between the groups.
Use the ggpairs function to explore the association
between the different variables. Try to replicate the figure below, or
create your own variation.
discrete_upper_plot <- function(data, mapping, text, ...){
p <- ggally_colbar(
data,
mapping,
label_format = scales::label_percent(accuracy = 0.1)
) }
ggpairs(data = natality,
aes(color=CIG_REC),
columns = c("DBWT", "risks", "MAGER", "DMAR", "BMI"),
columnLabels = c("Birth Weight (g)", "Risks", "Mother Age", "Marital status", "Mother BMI"),
lower = list(continuous = "cor",
discrete= "facetbar",
combo="facethist"),
upper= list(continuous = wrap("density", size=0.4),
discrete= wrap(discrete_upper_plot),
combo="facethist"),
diag = list(continuous = wrap("densityDiag", alpha=0.4, color = NA),
discrete="densityDiag"),
showStrips=F,
legend = 3,
title = "Natality in the United States (2018)"
)+
theme_minimal()+
theme(legend.position = "bottom",
legend.key = element_rect(colour = "transparent", fill = "white"),
strip.background = element_blank()) + scale_color_discrete(name = "Smoking")## `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`.
Answer: the graphs indicates that the variables are not related to each other. Also, birth weight in smoking mothers is slightly lower compared to the non-smoking mothers.
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
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
## Step Df Deviance Resid. Df Resid. Dev AIC
## 1 NA NA 1687 611839087 21609.55
## 2 + DMAR -1 28672236.1 1686 583166851 21530.53
## 3 + MRACEHISP -3 15359037.0 1683 567807814 21491.48
## 4 + BMI -1 4532441.0 1682 563275373 21479.95
## 5 + risks -1 6059709.5 1681 557215664 21463.69
## 6 + PRIORLIVE -4 5580134.0 1677 551635530 21454.70
## 7 + CIG_REC -1 2707991.1 1676 548927539 21448.40
## 8 + PRIORDEAD -1 922492.9 1675 548005046 21447.56
##
## Call:
## lm(formula = DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE +
## CIG_REC + PRIORDEAD, data = natality)
##
## Coefficients:
## (Intercept) DMARUnmarried MRACEHISPNH Black MRACEHISPNH Other
## 3080.56 -184.27 -292.14 -121.70
## MRACEHISPHispanic BMI risksYes PRIORLIVE1
## -74.49 10.26 -158.81 130.69
## PRIORLIVE2 PRIORLIVE3 PRIORLIVE4+ CIG_RECYes
## 107.17 138.78 131.95 -160.08
## PRIORDEAD1+
## 258.43
##
## Call:
## lm(formula = DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE +
## CIG_REC + PRIORDEAD, data = natality)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2751.64 -299.56 29.27 355.17 2394.55
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3080.559 64.625 47.668 < 2e-16 ***
## DMARUnmarried -184.275 31.823 -5.791 8.36e-09 ***
## MRACEHISPNH Black -292.140 41.690 -7.007 3.51e-12 ***
## MRACEHISPNH Other -121.695 52.796 -2.305 0.021288 *
## MRACEHISPHispanic -74.492 38.075 -1.956 0.050577 .
## BMI 10.261 2.259 4.542 5.97e-06 ***
## risksYes -158.808 31.655 -5.017 5.81e-07 ***
## PRIORLIVE1 130.686 33.814 3.865 0.000115 ***
## PRIORLIVE2 107.170 41.746 2.567 0.010339 *
## PRIORLIVE3 138.776 59.972 2.314 0.020788 *
## PRIORLIVE4+ 131.949 62.333 2.117 0.034420 *
## CIG_RECYes -160.083 55.632 -2.878 0.004059 **
## PRIORDEAD1+ 258.434 153.905 1.679 0.093304 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 572 on 1675 degrees of freedom
## Multiple R-squared: 0.1043, Adjusted R-squared: 0.09791
## F-statistic: 16.26 on 12 and 1675 DF, p-value: < 2.2e-16
## Analysis of Variance Table
##
## Model 1: DBWT ~ 1
## Model 2: DBWT ~ CIG_REC
## Model 3: DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC +
## PRIORDEAD
## Model 4: DBWT ~ CIG_REC + risks + MAGER + MRACEHISP + DMAR + MEDUC + PRIORLIVE +
## PRIORDEAD + BMI
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1687 611839087
## 2 1686 608153901 1 3685187 11.2491 0.0008144 ***
## 3 1675 548005046 11 60148855 16.6914 < 2.2e-16 ***
## 4 1671 547416336 4 588710 0.4493 0.7730024
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modelsummary(
list(
null = M.NULL,
unadjusted = M.UNADJUSTED,
optimal = optimal,
full = M.FULL
),
stars = TRUE
)| null | unadjusted | optimal | full | |
|---|---|---|---|---|
| (Intercept) | 3234.746*** | 3247.903*** | 3080.559*** | 3162.373*** |
| (14.658) | (15.187) | (64.625) | (107.021) | |
| CIG_RECYes | -179.096** | -160.083** | -156.593** | |
| (56.032) | (55.632) | (56.325) | ||
| DMARUnmarried | -184.275*** | -189.782*** | ||
| (31.823) | (35.084) | |||
| MRACEHISPNH Black | -292.140*** | -290.175*** | ||
| (41.690) | (41.829) | |||
| MRACEHISPNH Other | -121.695* | -120.774* | ||
| (52.796) | (52.960) | |||
| MRACEHISPHispanic | -74.492+ | -72.464+ | ||
| (38.075) | (39.120) | |||
| 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 | ||||
Answer: The optimal model seems to have a better fit due to the adjusted R2, AIC and BIC. However, the difference is not much compared to the complete model.
Center and scale all the continuous variables, and create a forest plot for the optimal model.
# Write your code here
natality.ctr <- natality |>
standardise(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()Answer: In this forestplot you see the effect estimates with the 95% CI for all the variables included in the model. Since the data is standardized with 2SD, you can interpret the dot as the differences when the variable increases with 2 SD. In this way it is possible to plot variables in different ranges. In addition, the forestplots visualized the significances of the variables. When the lines of the variable don’t cross 0, it means that the variable is signficantly associated with the outcome. In this case, Hispanic race and prior birth now dead crossing 0 and are not significant, which is as expected in line with the output table from the previous exercise.
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.
M.UNADJUSTED.ctr <- lm(DBWT ~ CIG_REC, natality.ctr)
M.FULL.ctr <- lm(DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE + PRIORDEAD + MAGER + MEDUC , natality.ctr)
full_est <- M.FULL.ctr |> tidy(conf.int = TRUE)
opt_est <- M.OPT.ctr |> tidy(conf.int = TRUE)
unadj_est <- M.UNADJUSTED.ctr |> tidy(conf.int = TRUE)
Reduce( function (x,y) rbind(x,y), list(Unadjusted=unadj_est[unadj_est$term=="CIG_RECYes",],
Optimal=opt_est[opt_est$term=="CIG_RECYes",],
Full = full_est[full_est$term=="CIG_RECYes",])) |> mutate(CIG_RECYes=estimate)## # A tibble: 3 × 8
## term estimate std.error statistic p.value conf.low conf.high CIG_RECYes
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 CIG_RECYes -0.149 0.0465 -3.20 0.00142 -0.240 -0.0575 -0.149
## 2 CIG_RECYes -0.133 0.0462 -2.88 0.00406 -0.224 -0.0423 -0.133
## 3 CIG_RECYes -0.130 0.0468 -2.78 0.00549 -0.222 -0.0383 -0.130
Answer: Addition of variables into the model does not change the significance effect of smoking on birht weight, yet the effect estimate were changed. the confidence interval of the unadjusted model is wider than the full model and optimal model. this may indicate that unadjusted model provide worst precision of estimate.
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 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
natality$CIG_REC <- factor(natality$CIG_REC, levels = c("No", "Yes"))
natality.ctr$CIG_REC <- factor(natality.ctr$CIG_REC, levels = c("No", "Yes"))
library(marginaleffects)
##Full model
M.FULL <- lm(DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE + PRIORDEAD + MAGER + MEDUC, data=natality.ctr)
summary(M.FULL)##
## Call:
## lm(formula = DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks +
## PRIORLIVE + PRIORDEAD + MAGER + MEDUC, data = natality.ctr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.27637 -0.25032 0.02045 0.29810 1.98838
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0823000 0.0482664 1.705 0.08836 .
## CIG_RECYes -0.1300115 0.0467642 -2.780 0.00549 **
## DMARUnmarried -0.1575663 0.0291286 -5.409 7.24e-08 ***
## MRACEHISPNH Black -0.2409178 0.0347287 -6.937 5.70e-12 ***
## MRACEHISPNH Other -0.1002727 0.0439704 -2.280 0.02271 *
## MRACEHISPHispanic -0.0601636 0.0324792 -1.852 0.06415 .
## BMI 0.1102108 0.0239127 4.609 4.36e-06 ***
## risksYes -0.1269471 0.0266136 -4.770 2.00e-06 ***
## PRIORLIVE1 0.1166359 0.0288308 4.046 5.46e-05 ***
## PRIORLIVE2 0.1024405 0.0364313 2.812 0.00498 **
## PRIORLIVE3 0.1339180 0.0524295 2.554 0.01073 *
## PRIORLIVE4+ 0.1349220 0.0560935 2.405 0.01627 *
## PRIORDEAD1+ 0.2204036 0.1280112 1.722 0.08530 .
## MAGER -0.0369532 0.0297204 -1.243 0.21391
## MEDUCHS 0.0070139 0.0414368 0.169 0.86561
## MEDUCSome college -0.0001992 0.0422082 -0.005 0.99623
## MEDUCUni 0.0262445 0.0472425 0.556 0.57861
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4752 on 1671 degrees of freedom
## Multiple R-squared: 0.1053, Adjusted R-squared: 0.09673
## F-statistic: 12.29 on 16 and 1671 DF, p-value: < 2.2e-16
##Full model with an interaction term
M.FULL_int.ctr <- lm(DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE + PRIORDEAD + MAGER + MEDUC + CIG_REC:MEDUC, data=natality.ctr)
summary(M.FULL_int.ctr)##
## Call:
## lm(formula = DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks +
## PRIORLIVE + PRIORDEAD + MAGER + MEDUC + CIG_REC:MEDUC, data = natality.ctr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.27704 -0.24697 0.02151 0.29953 1.98393
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.06227 0.05010 1.243 0.21402
## CIG_RECYes -0.01780 0.09114 -0.195 0.84520
## DMARUnmarried -0.15687 0.02915 -5.382 8.40e-08 ***
## MRACEHISPNH Black -0.23940 0.03476 -6.888 7.98e-12 ***
## MRACEHISPNH Other -0.10225 0.04402 -2.323 0.02030 *
## MRACEHISPHispanic -0.05612 0.03260 -1.721 0.08539 .
## BMI 0.10834 0.02394 4.526 6.45e-06 ***
## risksYes -0.12659 0.02664 -4.752 2.18e-06 ***
## PRIORLIVE1 0.11737 0.02884 4.070 4.93e-05 ***
## PRIORLIVE2 0.10388 0.03646 2.850 0.00443 **
## PRIORLIVE3 0.12858 0.05261 2.444 0.01463 *
## PRIORLIVE4+ 0.13510 0.05610 2.408 0.01615 *
## PRIORDEAD1+ 0.22467 0.12811 1.754 0.07965 .
## MAGER -0.03616 0.02977 -1.215 0.22466
## MEDUCHS 0.02627 0.04459 0.589 0.55580
## MEDUCSome college 0.02550 0.04503 0.566 0.57128
## MEDUCUni 0.04447 0.04899 0.908 0.36418
## CIG_RECYes:MEDUCHS -0.12090 0.11702 -1.033 0.30171
## CIG_RECYes:MEDUCSome college -0.20235 0.12165 -1.663 0.09645 .
## CIG_RECYes:MEDUCUni -0.06490 0.19249 -0.337 0.73605
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4752 on 1668 degrees of freedom
## Multiple R-squared: 0.1068, Adjusted R-squared: 0.09665
## F-statistic: 10.5 on 19 and 1668 DF, p-value: < 2.2e-16
## Analysis of Variance Table
##
## Model 1: DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE +
## PRIORDEAD + MAGER + MEDUC
## Model 2: DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE +
## PRIORDEAD + MAGER + MEDUC + CIG_REC:MEDUC
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1671 377.34
## 2 1668 376.70 3 0.64613 0.9537 0.4138
##Predictions to plot
results_int_full<- predictions(M.FULL_int.ctr,
newdata = datagrid(
MEDUC = c("lower", "HS", "Some college", "Uni"),
CIG_REC = c("No", "Yes"),
MRACEHISP= c("NH White", "NH Black", "NH Other", "Hispanic")
)
) |>
as.data.frame() |>
mutate(DBWT = estimate) |>
select(DBWT, CIG_REC, MEDUC,MRACEHISP)
results_int_full## DBWT CIG_REC MEDUC MRACEHISP
## 1 0.062274688 No lower NH White
## 2 -0.177128407 No lower NH Black
## 3 -0.039972642 No lower NH Other
## 4 0.006155744 No lower Hispanic
## 5 0.044478085 Yes lower NH White
## 6 -0.194925010 Yes lower NH Black
## 7 -0.057769244 Yes lower NH Other
## 8 -0.011640859 Yes lower Hispanic
## 9 0.088547793 No HS NH White
## 10 -0.150855303 No HS NH Black
## 11 -0.013699537 No HS NH Other
## 12 0.032428848 No HS Hispanic
## 13 -0.050146730 Yes HS NH White
## 14 -0.289549825 Yes HS NH Black
## 15 -0.152394060 Yes HS NH Other
## 16 -0.106265674 Yes HS Hispanic
## 17 0.087773609 No Some college NH White
## 18 -0.151629486 No Some college NH Black
## 19 -0.014473721 No Some college NH Other
## 20 0.031654665 No Some college Hispanic
## 21 -0.132368083 Yes Some college NH White
## 22 -0.371771178 Yes Some college NH Black
## 23 -0.234615412 Yes Some college NH Other
## 24 -0.188487027 Yes Some college Hispanic
## 25 0.106740302 No Uni NH White
## 26 -0.132662793 No Uni NH Black
## 27 0.004492972 No Uni NH Other
## 28 0.050621358 No Uni Hispanic
## 29 0.024046565 Yes Uni NH White
## 30 -0.215356530 Yes Uni NH Black
## 31 -0.078200765 Yes Uni NH Other
## 32 -0.032072379 Yes Uni Hispanic
#Dumbell plot
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 = 1.5
) +
geom_point(
aes(x = "No Smoking", y = No_Smoking, color = Education),
size = 4
) +
geom_point(
aes(x = "Smoking", y = Smoking, color = Education),
size = 4
)+
ylab("Birth weight(centred, scaled)")+
theme(legend.position = "bottom", axis.title.x = element_blank())library(marginaleffects)
##Full model
M.FULL <- lm(DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE + PRIORDEAD + MAGER + MEDUC, data=natality.ctr)
summary(M.FULL)##
## Call:
## lm(formula = DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks +
## PRIORLIVE + PRIORDEAD + MAGER + MEDUC, data = natality.ctr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.27637 -0.25032 0.02045 0.29810 1.98838
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0823000 0.0482664 1.705 0.08836 .
## CIG_RECYes -0.1300115 0.0467642 -2.780 0.00549 **
## DMARUnmarried -0.1575663 0.0291286 -5.409 7.24e-08 ***
## MRACEHISPNH Black -0.2409178 0.0347287 -6.937 5.70e-12 ***
## MRACEHISPNH Other -0.1002727 0.0439704 -2.280 0.02271 *
## MRACEHISPHispanic -0.0601636 0.0324792 -1.852 0.06415 .
## BMI 0.1102108 0.0239127 4.609 4.36e-06 ***
## risksYes -0.1269471 0.0266136 -4.770 2.00e-06 ***
## PRIORLIVE1 0.1166359 0.0288308 4.046 5.46e-05 ***
## PRIORLIVE2 0.1024405 0.0364313 2.812 0.00498 **
## PRIORLIVE3 0.1339180 0.0524295 2.554 0.01073 *
## PRIORLIVE4+ 0.1349220 0.0560935 2.405 0.01627 *
## PRIORDEAD1+ 0.2204036 0.1280112 1.722 0.08530 .
## MAGER -0.0369532 0.0297204 -1.243 0.21391
## MEDUCHS 0.0070139 0.0414368 0.169 0.86561
## MEDUCSome college -0.0001992 0.0422082 -0.005 0.99623
## MEDUCUni 0.0262445 0.0472425 0.556 0.57861
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4752 on 1671 degrees of freedom
## Multiple R-squared: 0.1053, Adjusted R-squared: 0.09673
## F-statistic: 12.29 on 16 and 1671 DF, p-value: < 2.2e-16
##Full model with an interaction term: BONUS
M.FULL_int.ctr <- lm(DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE + PRIORDEAD + MAGER + MEDUC + CIG_REC:MEDUC:MRACEHISP, data=natality.ctr)
summary(M.FULL_int.ctr)##
## Call:
## lm(formula = DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks +
## PRIORLIVE + PRIORDEAD + MAGER + MEDUC + CIG_REC:MEDUC:MRACEHISP,
## data = natality.ctr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.22854 -0.24972 0.01846 0.30228 1.98938
##
## Coefficients: (10 not defined because of singularities)
## Estimate Std. Error t value
## (Intercept) -1.18867 0.79310 -1.499
## CIG_RECYes 0.48865 0.34321 1.424
## DMARUnmarried -0.15713 0.02937 -5.350
## MRACEHISPNH Black 0.57595 0.39930 1.442
## MRACEHISPNH Other 0.66013 0.38983 1.693
## MRACEHISPHispanic 0.66283 0.39959 1.659
## BMI 0.10690 0.02413 4.431
## risksYes -0.12503 0.02684 -4.659
## PRIORLIVE1 0.12321 0.02901 4.247
## PRIORLIVE2 0.10615 0.03671 2.891
## PRIORLIVE3 0.13689 0.05292 2.587
## PRIORLIVE4+ 0.13698 0.05664 2.418
## PRIORDEAD1+ 0.22160 0.12849 1.725
## MAGER -0.04177 0.03003 -1.391
## MEDUCHS 0.23456 0.47672 0.492
## MEDUCSome college -0.23019 0.47760 -0.482
## MEDUCUni 0.53284 0.48632 1.096
## CIG_RECNo:MRACEHISPNH White:MEDUClower 1.18035 0.79365 1.487
## CIG_RECYes:MRACEHISPNH White:MEDUClower 0.81650 0.53038 1.539
## CIG_RECNo:MRACEHISPNH Black:MEDUClower 0.49829 0.49714 1.002
## CIG_RECYes:MRACEHISPNH Black:MEDUClower -0.69720 0.44407 -1.570
## CIG_RECNo:MRACEHISPNH Other:MEDUClower 0.35652 0.51809 0.688
## CIG_RECYes:MRACEHISPNH Other:MEDUClower -0.03420 0.42152 -0.081
## CIG_RECNo:MRACEHISPHispanic:MEDUClower 0.55792 0.48327 1.154
## CIG_RECYes:MRACEHISPHispanic:MEDUClower NA NA NA
## CIG_RECNo:MRACEHISPNH White:MEDUCHS 1.06467 0.79319 1.342
## CIG_RECYes:MRACEHISPNH White:MEDUCHS 0.41816 0.52929 0.790
## CIG_RECNo:MRACEHISPNH Black:MEDUCHS 0.17493 0.49201 0.356
## CIG_RECYes:MRACEHISPNH Black:MEDUCHS -0.68997 0.42249 -1.633
## CIG_RECNo:MRACEHISPNH Other:MEDUCHS 0.30511 0.51434 0.593
## CIG_RECYes:MRACEHISPNH Other:MEDUCHS -0.22545 0.48473 -0.465
## CIG_RECNo:MRACEHISPHispanic:MEDUCHS 0.31531 0.48367 0.652
## CIG_RECYes:MRACEHISPHispanic:MEDUCHS NA NA NA
## CIG_RECNo:MRACEHISPNH White:MEDUCSome college 1.49506 0.79325 1.885
## CIG_RECYes:MRACEHISPNH White:MEDUCSome college 0.81374 0.53209 1.529
## CIG_RECNo:MRACEHISPNH Black:MEDUCSome college 0.72365 0.49281 1.468
## CIG_RECYes:MRACEHISPNH Black:MEDUCSome college -0.11896 0.39253 -0.303
## CIG_RECNo:MRACEHISPNH Other:MEDUCSome college 0.71895 0.50328 1.429
## CIG_RECYes:MRACEHISPNH Other:MEDUCSome college 0.19306 0.48492 0.398
## CIG_RECNo:MRACEHISPHispanic:MEDUCSome college 0.77887 0.48475 1.607
## CIG_RECYes:MRACEHISPHispanic:MEDUCSome college NA NA NA
## CIG_RECNo:MRACEHISPNH White:MEDUCUni 0.75800 0.39517 1.918
## CIG_RECYes:MRACEHISPNH White:MEDUCUni NA NA NA
## CIG_RECNo:MRACEHISPNH Black:MEDUCUni NA NA NA
## CIG_RECYes:MRACEHISPNH Black:MEDUCUni NA NA NA
## CIG_RECNo:MRACEHISPNH Other:MEDUCUni NA NA NA
## CIG_RECYes:MRACEHISPNH Other:MEDUCUni NA NA NA
## CIG_RECNo:MRACEHISPHispanic:MEDUCUni NA NA NA
## CIG_RECYes:MRACEHISPHispanic:MEDUCUni NA NA NA
## Pr(>|t|)
## (Intercept) 0.13413
## CIG_RECYes 0.15471
## DMARUnmarried 1.00e-07 ***
## MRACEHISPNH Black 0.14938
## MRACEHISPNH Other 0.09057 .
## MRACEHISPHispanic 0.09735 .
## BMI 1.00e-05 ***
## risksYes 3.43e-06 ***
## PRIORLIVE1 2.29e-05 ***
## PRIORLIVE2 0.00389 **
## PRIORLIVE3 0.00977 **
## PRIORLIVE4+ 0.01570 *
## PRIORDEAD1+ 0.08479 .
## MAGER 0.16440
## MEDUCHS 0.62276
## MEDUCSome college 0.62989
## MEDUCUni 0.27339
## CIG_RECNo:MRACEHISPNH White:MEDUClower 0.13714
## CIG_RECYes:MRACEHISPNH White:MEDUClower 0.12389
## CIG_RECNo:MRACEHISPNH Black:MEDUClower 0.31634
## CIG_RECYes:MRACEHISPNH Black:MEDUClower 0.11660
## CIG_RECNo:MRACEHISPNH Other:MEDUClower 0.49146
## CIG_RECYes:MRACEHISPNH Other:MEDUClower 0.93534
## CIG_RECNo:MRACEHISPHispanic:MEDUClower 0.24848
## CIG_RECYes:MRACEHISPHispanic:MEDUClower NA
## CIG_RECNo:MRACEHISPNH White:MEDUCHS 0.17969
## CIG_RECYes:MRACEHISPNH White:MEDUCHS 0.42962
## CIG_RECNo:MRACEHISPNH Black:MEDUCHS 0.72223
## CIG_RECYes:MRACEHISPNH Black:MEDUCHS 0.10263
## CIG_RECNo:MRACEHISPNH Other:MEDUCHS 0.55312
## CIG_RECYes:MRACEHISPNH Other:MEDUCHS 0.64192
## CIG_RECNo:MRACEHISPHispanic:MEDUCHS 0.51455
## CIG_RECYes:MRACEHISPHispanic:MEDUCHS NA
## CIG_RECNo:MRACEHISPNH White:MEDUCSome college 0.05964 .
## CIG_RECYes:MRACEHISPNH White:MEDUCSome college 0.12637
## CIG_RECNo:MRACEHISPNH Black:MEDUCSome college 0.14218
## CIG_RECYes:MRACEHISPNH Black:MEDUCSome college 0.76188
## CIG_RECNo:MRACEHISPNH Other:MEDUCSome college 0.15334
## CIG_RECYes:MRACEHISPNH Other:MEDUCSome college 0.69059
## CIG_RECNo:MRACEHISPHispanic:MEDUCSome college 0.10830
## CIG_RECYes:MRACEHISPHispanic:MEDUCSome college NA
## CIG_RECNo:MRACEHISPNH White:MEDUCUni 0.05526 .
## CIG_RECYes:MRACEHISPNH White:MEDUCUni NA
## CIG_RECNo:MRACEHISPNH Black:MEDUCUni NA
## CIG_RECYes:MRACEHISPNH Black:MEDUCUni NA
## CIG_RECNo:MRACEHISPNH Other:MEDUCUni NA
## CIG_RECYes:MRACEHISPNH Other:MEDUCUni NA
## CIG_RECNo:MRACEHISPHispanic:MEDUCUni NA
## CIG_RECYes:MRACEHISPHispanic: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
##Predictions to plot
results_int_full<- predictions(M.FULL_int.ctr,
newdata = datagrid(
MEDUC = c("lower", "HS", "Some college", "Uni"),
CIG_REC = c("No", "Yes"),
MRACEHISP= c("NH White", "NH Black", "NH Other", "Hispanic")
)
) |>
as.data.frame() |>
mutate(DBWT = estimate) |>
select(DBWT, CIG_REC, MEDUC,MRACEHISP) ## Warning: Model matrix is rank deficient. Some variance-covariance parameters are
## missing.
## DBWT CIG_REC MEDUC MRACEHISP
## 1 -0.008317104 No lower NH White
## 2 -0.114424774 No lower NH Black
## 3 -0.172012722 No lower NH Other
## 4 0.032086047 No lower Hispanic
## 5 0.116478046 Yes lower NH White
## 6 -0.821268574 Yes lower NH Black
## 7 -0.074089899 Yes lower NH Other
## 8 -0.037183980 Yes lower Hispanic
## 9 0.110566399 No HS NH White
## 10 -0.203217090 No HS NH Black
## 11 0.011142015 No HS NH Other
## 12 0.024041622 No HS Hispanic
## 13 -0.047295141 Yes HS NH White
## 14 -0.579474229 Yes HS NH Black
## 15 -0.030768003 Yes HS NH Other
## 16 0.197380550 Yes HS Hispanic
## 17 0.076199617 No Some college NH White
## 18 -0.119254504 No Some college NH Black
## 19 -0.039778006 No Some college NH Other
## 20 0.022853001 No Some college Hispanic
## 21 -0.116471574 Yes Some college NH White
## 22 -0.473214090 Yes Some college NH Black
## 23 -0.077019131 Yes Some college NH Other
## 24 -0.267373209 Yes Some college Hispanic
## 25 0.102174187 No Uni NH White
## 26 -0.079876992 No Uni NH Black
## 27 0.004302378 No Uni NH Other
## 28 0.007004896 No Uni Hispanic
## 29 -0.167181876 Yes Uni NH White
## 30 0.408770861 Yes Uni NH Black
## 31 0.492950230 Yes Uni NH Other
## 32 0.495652749 Yes Uni Hispanic
#Dumbell plot
results_int_full |> spread(CIG_REC, DBWT) |> ggplot() +
geom_segment(
aes(
x = "No Smoking", y = No,
xend = "Smoking", yend = Yes,
color = MEDUC
),
linewidth = 1.5
) +
geom_point(
aes(x = "No Smoking", y = No, color = MEDUC),
size = 4
) +
geom_point(
aes(x = "Smoking", y = Yes, color = MEDUC),
size = 4
) + facet_wrap(~MRACEHISP)+
ylab("Birth weight(centred, scaled)")+
theme(legend.position = "bottom", axis.title.x = element_blank())Answer: The association between cigarette smoking during pregnancy and birth weight does not depend on mother’s education. When we performed the interaction analysis, we found no significant evidence of interaction between smoking and education on birth weight (interaction P value > 0.05). In addition, when we compared the full model with an interaction term with the model without interaction term, we did not find that the model with interaction is significantly better than the one without it (Pr(>F) = 0.4138) Plot interpretation: when we observe the dumbell plot, it seems that there is an interaction between smoking and education. Birth weight is higher when mothers have a higher education (some college, uni) and non-smokers. However, the fit model showed there is no a significant interaction, a possible explanation could be that there is a third variable we need to consider as another way of interaction.
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
library(marginaleffects)
##Optimal model
M.OPT.ctr <- lm(DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC + PRIORDEAD, data = natality.ctr)
summary(M.OPT.ctr)##
## Call:
## lm(formula = DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE +
## CIG_REC + PRIORDEAD, data = natality.ctr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.2845 -0.2487 0.0243 0.2949 1.9881
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.10140 0.02385 4.252 2.24e-05 ***
## DMARUnmarried -0.15299 0.02642 -5.791 8.36e-09 ***
## MRACEHISPNH Black -0.24255 0.03461 -7.007 3.51e-12 ***
## MRACEHISPNH Other -0.10104 0.04383 -2.305 0.021288 *
## MRACEHISPHispanic -0.06185 0.03161 -1.956 0.050577 .
## BMI 0.10778 0.02373 4.542 5.97e-06 ***
## risksYes -0.13185 0.02628 -5.017 5.81e-07 ***
## PRIORLIVE1 0.10850 0.02807 3.865 0.000115 ***
## PRIORLIVE2 0.08898 0.03466 2.567 0.010339 *
## PRIORLIVE3 0.11522 0.04979 2.314 0.020788 *
## PRIORLIVE4+ 0.10955 0.05175 2.117 0.034420 *
## CIG_RECYes -0.13291 0.04619 -2.878 0.004059 **
## PRIORDEAD1+ 0.21457 0.12778 1.679 0.093304 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4749 on 1675 degrees of freedom
## Multiple R-squared: 0.1043, Adjusted R-squared: 0.09791
## F-statistic: 16.26 on 12 and 1675 DF, p-value: < 2.2e-16
##Optimal model with an interaction term
M.OPT_int.ctr <- lm(DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC + PRIORDEAD + BMI:MRACEHISP, data=natality.ctr)
summary(M.OPT_int.ctr)##
## Call:
## lm(formula = DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE +
## CIG_REC + PRIORDEAD + BMI:MRACEHISP, data = natality.ctr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.25450 -0.24913 0.02481 0.30091 1.97315
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.10284 0.02388 4.306 1.76e-05 ***
## DMARUnmarried -0.15462 0.02645 -5.845 6.07e-09 ***
## MRACEHISPNH Black -0.23951 0.03478 -6.887 8.05e-12 ***
## MRACEHISPNH Other -0.09206 0.04430 -2.078 0.037876 *
## MRACEHISPHispanic -0.06069 0.03165 -1.917 0.055353 .
## BMI 0.11999 0.03235 3.710 0.000214 ***
## risksYes -0.13234 0.02631 -5.031 5.41e-07 ***
## PRIORLIVE1 0.10773 0.02808 3.837 0.000129 ***
## PRIORLIVE2 0.08952 0.03466 2.582 0.009894 **
## PRIORLIVE3 0.11519 0.04984 2.311 0.020940 *
## PRIORLIVE4+ 0.10850 0.05181 2.094 0.036406 *
## CIG_RECYes -0.13550 0.04621 -2.932 0.003412 **
## PRIORDEAD1+ 0.20701 0.12791 1.618 0.105753
## MRACEHISPNH Black:BMI -0.05032 0.05884 -0.855 0.392561
## MRACEHISPNH Other:BMI 0.12036 0.09825 1.225 0.220711
## MRACEHISPHispanic:BMI -0.04574 0.06290 -0.727 0.467270
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4749 on 1672 degrees of freedom
## Multiple R-squared: 0.106, Adjusted R-squared: 0.098
## F-statistic: 13.22 on 15 and 1672 DF, p-value: < 2.2e-16
## Analysis of Variance Table
##
## Model 1: DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC +
## PRIORDEAD
## Model 2: DBWT ~ DMAR + MRACEHISP + BMI + risks + PRIORLIVE + CIG_REC +
## PRIORDEAD + BMI:MRACEHISP
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1675 377.75
## 2 1672 377.04 3 0.71163 1.0519 0.3685
##Predictions to plot
results_int_opt<- predictions(M.OPT_int.ctr,
newdata = datagrid(
MRACEHISP = c("NH White", "NH Black", "NH Other","Hispanic"),
BMI = c("mean(BMI)", "mean(BMI)+sd(BMI)")
)
) |>
as.data.frame() |>
mutate(DBWT = estimate) |>
select(DBWT, MRACEHISP, BMI)
results_int_opt## DBWT MRACEHISP BMI
## 1 0.10284377 NH White mean(BMI)
## 2 0.22283552 NH White mean(BMI)+sd(BMI)
## 3 -0.13666489 NH Black mean(BMI)
## 4 -0.06699297 NH Black mean(BMI)+sd(BMI)
## 5 0.01078647 NH Other mean(BMI)
## 6 0.25114286 NH Other mean(BMI)+sd(BMI)
## 7 0.04215825 Hispanic mean(BMI)
## 8 0.11641484 Hispanic mean(BMI)+sd(BMI)
##Dumbell plot
tribble(
~Ethnicity, ~mean, ~mean_sd,
"NH White", 0.10284377, 0.22283552,
"NH Black", -0.13666489, -0.06699297,
"NH Other", 0.01078647, 0.2511428,
"Hispanic", 0.04215825, 0.11641484
) |> ggplot() +
geom_segment(
aes(x = "mean(BMI)", y = mean,
xend = "mean(BMI)+sd(BMI)", yend = mean_sd,
color = Ethnicity
),
linewidth = 1.5
) +
geom_point(
aes(x = "mean(BMI)", y = mean, color = Ethnicity),
size = 4) +
geom_point(
aes(x = "mean(BMI)+sd(BMI)", y = mean_sd, color = Ethnicity),
size = 4)+
ylab("Birth weight(centred, scaled)")+
theme(legend.position = "bottom", axis.title.x = element_blank())Answer: The association between mother’s BMI during pregnancy and birth weight does not differ between mothers of different race/ethnicity. When we performed the interaction analysis, we found no significant evidence of interaction between BMI and race/ethnicity on birth weight (interaction P value > 0.05). In addition, when we compared the optimal model with an interaction term with the model without interaction term, we did not find that the model with interaction is significantly better than the one without it (Pr(>F) = 0.3685) Plot interpretation: when we observe the dumbell plot, it seems that there is an interaction between BMI and race/ethnicity. Birth weight is higher in NH White mothers, and they also have higher BMI. However, the fit model showed there is no a significant interaction between BMI and race/ethnicity on birth weight.
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
library(marginaleffects)
##Full model
M.FULL <- lm(DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE + PRIORDEAD + MAGER + MEDUC, data=natality.ctr)
summary(M.FULL)##
## Call:
## lm(formula = DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks +
## PRIORLIVE + PRIORDEAD + MAGER + MEDUC, data = natality.ctr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.27637 -0.25032 0.02045 0.29810 1.98838
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0823000 0.0482664 1.705 0.08836 .
## CIG_RECYes -0.1300115 0.0467642 -2.780 0.00549 **
## DMARUnmarried -0.1575663 0.0291286 -5.409 7.24e-08 ***
## MRACEHISPNH Black -0.2409178 0.0347287 -6.937 5.70e-12 ***
## MRACEHISPNH Other -0.1002727 0.0439704 -2.280 0.02271 *
## MRACEHISPHispanic -0.0601636 0.0324792 -1.852 0.06415 .
## BMI 0.1102108 0.0239127 4.609 4.36e-06 ***
## risksYes -0.1269471 0.0266136 -4.770 2.00e-06 ***
## PRIORLIVE1 0.1166359 0.0288308 4.046 5.46e-05 ***
## PRIORLIVE2 0.1024405 0.0364313 2.812 0.00498 **
## PRIORLIVE3 0.1339180 0.0524295 2.554 0.01073 *
## PRIORLIVE4+ 0.1349220 0.0560935 2.405 0.01627 *
## PRIORDEAD1+ 0.2204036 0.1280112 1.722 0.08530 .
## MAGER -0.0369532 0.0297204 -1.243 0.21391
## MEDUCHS 0.0070139 0.0414368 0.169 0.86561
## MEDUCSome college -0.0001992 0.0422082 -0.005 0.99623
## MEDUCUni 0.0262445 0.0472425 0.556 0.57861
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4752 on 1671 degrees of freedom
## Multiple R-squared: 0.1053, Adjusted R-squared: 0.09673
## F-statistic: 12.29 on 16 and 1671 DF, p-value: < 2.2e-16
##Full model with an interaction term
M.FULL_int.ctr2 <- lm(DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE + PRIORDEAD + MAGER + MEDUC + BMI:MAGER, data=natality.ctr)
summary(M.FULL_int.ctr2)##
## Call:
## lm(formula = DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks +
## PRIORLIVE + PRIORDEAD + MAGER + MEDUC + BMI:MAGER, data = natality.ctr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.27538 -0.25308 0.02022 0.29794 1.98582
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0830972 0.0483016 1.720 0.08555 .
## CIG_RECYes -0.1308047 0.0467996 -2.795 0.00525 **
## DMARUnmarried -0.1577950 0.0291384 -5.415 7.01e-08 ***
## MRACEHISPNH Black -0.2397811 0.0348058 -6.889 7.93e-12 ***
## MRACEHISPNH Other -0.1008122 0.0439924 -2.292 0.02205 *
## MRACEHISPHispanic -0.0593407 0.0325253 -1.824 0.06826 .
## BMI 0.1099342 0.0239239 4.595 4.65e-06 ***
## risksYes -0.1266568 0.0266254 -4.757 2.13e-06 ***
## PRIORLIVE1 0.1162589 0.0288463 4.030 5.82e-05 ***
## PRIORLIVE2 0.1016648 0.0364701 2.788 0.00537 **
## PRIORLIVE3 0.1339035 0.0524410 2.553 0.01076 *
## PRIORLIVE4+ 0.1352514 0.0561094 2.410 0.01604 *
## PRIORDEAD1+ 0.2172404 0.1281854 1.695 0.09031 .
## MAGER -0.0370333 0.0297274 -1.246 0.21303
## MEDUCHS 0.0067930 0.0414481 0.164 0.86984
## MEDUCSome college -0.0004223 0.0422197 -0.010 0.99202
## MEDUCUni 0.0251011 0.0473046 0.531 0.59575
## BMI:MAGER -0.0242484 0.0469027 -0.517 0.60523
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4753 on 1670 degrees of freedom
## Multiple R-squared: 0.1054, Adjusted R-squared: 0.09633
## F-statistic: 11.58 on 17 and 1670 DF, p-value: < 2.2e-16
## Analysis of Variance Table
##
## Model 1: DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE +
## PRIORDEAD + MAGER + MEDUC
## Model 2: DBWT ~ CIG_REC + DMAR + MRACEHISP + BMI + risks + PRIORLIVE +
## PRIORDEAD + MAGER + MEDUC + BMI:MAGER
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1671 377.34
## 2 1670 377.28 1 0.060384 0.2673 0.6052
##Predictions to plot
results_int_full2<- predictions(M.FULL_int.ctr2,
newdata = datagrid(
BMI = c("mean(BMI)", "mean(BMI)+sd(BMI)"),
MAGER = c(20, 30, 40)
)
) |>
as.data.frame() |>
mutate(DBWT = estimate) |>
select(DBWT,BMI, MAGER)
results_int_full2## DBWT BMI MAGER
## 1 -0.632467 mean(BMI) 20
## 2 -1.002800 mean(BMI) 30
## 3 -1.373132 mean(BMI) 40
## 4 -1.007501 mean(BMI)+sd(BMI) 20
## 5 -1.620317 mean(BMI)+sd(BMI) 30
## 6 -2.233134 mean(BMI)+sd(BMI) 40
#Dumbell plot
tribble(
~age, ~mean, ~mean_sd,
"20", -0.632467, -1.007501,
"30", -1.002800, -1.620317,
"40", -1.373132, -2.233134,
) |> ggplot() +
geom_segment(
aes(
x = "mean(BMI)", y = mean,
xend = "mean(BMI)+sd(BMI)", yend = mean_sd,
color = age
),
linewidth = 1.5
) +
geom_point(
aes(x = "mean(BMI)", y = mean, color = age),
size = 4
) +
geom_point(
aes(x = "mean(BMI)+sd(BMI)", y = mean_sd, color = age),
size = 4
)+
ylab("Birth weight(centred, scaled)")+
theme(legend.position = "bottom", axis.title.x = element_blank())+
theme_minimal()Answer: The association between mother’s BMI during pregnancy and birth weight does not depend on the mother’s age. When we performed the interaction analysis, we found no significant evidence of interaction between BMI and age on birth weight (interaction P value > 0.05). In addition, when we compared the full model with an interaction term with the model without interaction term, we did not find that the model with interaction is significantly better than the one without it (Pr(>F) = 0.6052) Plot interpretation: when we observe the dumbell plot, it seems that there is an interaction between BMI and age. Mothers with higher age have lower BMI and the birth weight is lower. However, the fit model showed there is no a significant interaction between BMI and age on birth weight.