# install.packages(c("openintro", "tidyverse", "pROC")) # run once if needed
library(openintro)
library(tidyverse)
library(pROC)
# Import the dataset
births <- births14
Research question: Do a mother’s smoking habit, age, weight gain during pregnancy, number of prenatal visits, and marital status predict whether her baby is born at a low birth weight?
Low birth weight is linked to health problems for babies, so I want
to see which factors about the mother make it more likely. To answer
this question, I am using the births14 dataset from
OpenIntro. It is a random sample of births in the United States from
2014, taken from public birth records released by the Centers for
Disease Control and Prevention (CDC). The dataset has 1,000 rows
and 13 variables. Each row (case) is one birth. The 13
variables describe the father (his age), the mother (her age, whether
she is a “mature” mom, smoking habit, marital status, race, and weight
gained), the pregnancy (length in weeks, whether the baby was premature,
and number of hospital visits), and the baby (sex, birth weight in
pounds, and whether the baby was low birth weight).
For my analysis I focus on six of these variables:
lowbirthweight (outcome, categorical): whether the baby
was born at a low birth weight (low) or not
(not low). A baby is classified as low birth weight if it
weighs 5.5 pounds or less.habit (categorical): whether the mother is a
smoker or nonsmoker.mage (quantitative): the mother’s age in years.gained (quantitative): the weight the mother gained
during pregnancy, in pounds.visits (quantitative): the number of hospital visits
during pregnancy.marital (categorical): whether the mother was
married or not married at the time of
birth.The dataset is available from OpenIntro at https://www.openintro.org/data/index.php?data=births14. The original data comes from the CDC’s National Center for Health Statistics, Natality Detail File, 2014 (https://doi.org/10.3886/ICPSR36461.v1).
First, I will explore the dataset to understand its structure and
find any missing values. Then I will use dplyr to keep only
the variables I need, remove rows with missing values, and create a 0/1
version of the outcome so it can be used in logistic regression. After
that, I will compare the percentage of low birth weight babies between
groups using summary tables and plots. I will make a bar
chart comparing the low birth weight rate for smokers and
nonsmokers, and boxplots comparing the number of
prenatal visits and weight gained for low and not low birth weight
babies.
# Exploratory data analysis: structure, summary, and missing values
glimpse(births)
## Rows: 1,000
## Columns: 13
## $ fage <int> 34, 36, 37, NA, 32, 32, 37, 29, 30, 29, 30, 34, 28, 28,…
## $ mage <dbl> 34, 31, 36, 16, 31, 26, 36, 24, 32, 26, 34, 27, 22, 31,…
## $ mature <chr> "younger mom", "younger mom", "mature mom", "younger mo…
## $ weeks <dbl> 37, 41, 37, 38, 36, 39, 36, 40, 39, 39, 42, 40, 40, 39,…
## $ premie <chr> "full term", "full term", "full term", "full term", "pr…
## $ visits <dbl> 14, 12, 10, NA, 12, 14, 10, 13, 15, 11, 14, 16, 20, 15,…
## $ gained <dbl> 28, 41, 28, 29, 48, 45, 20, 65, 25, 22, 40, 30, 31, NA,…
## $ weight <dbl> 6.96, 8.86, 7.51, 6.19, 6.75, 6.69, 6.13, 6.74, 8.94, 9…
## $ lowbirthweight <chr> "not low", "not low", "not low", "not low", "not low", …
## $ sex <chr> "male", "female", "female", "male", "female", "female",…
## $ habit <chr> "nonsmoker", "nonsmoker", "nonsmoker", "nonsmoker", "no…
## $ marital <chr> "married", "married", "married", "not married", "marrie…
## $ whitemom <chr> "white", "white", "not white", "white", "white", "white…
summary(births)
## fage mage mature weeks
## Min. :15.00 Min. :14.00 Length :1000 Min. :21.00
## 1st Qu.:26.00 1st Qu.:24.00 N.unique : 2 1st Qu.:38.00
## Median :31.00 Median :28.00 N.blank : 0 Median :39.00
## Mean :31.13 Mean :28.45 Min.nchar: 10 Mean :38.67
## 3rd Qu.:35.00 3rd Qu.:33.00 Max.nchar: 11 3rd Qu.:40.00
## Max. :85.00 Max. :47.00 Max. :46.00
## NAs :114
## premie visits gained weight
## Length :1000 Min. : 0.00 Min. : 0.00 Min. : 0.750
## N.unique : 2 1st Qu.: 9.00 1st Qu.:20.00 1st Qu.: 6.545
## N.blank : 0 Median :12.00 Median :30.00 Median : 7.310
## Min.nchar: 6 Mean :11.35 Mean :30.43 Mean : 7.198
## Max.nchar: 9 3rd Qu.:14.00 3rd Qu.:38.00 3rd Qu.: 8.000
## Max. :30.00 Max. :98.00 Max. :10.620
## NAs :56 NAs :42
## lowbirthweight sex habit marital
## Length :1000 Length :1000 Length :1000 Length :1000
## N.unique : 2 N.unique : 2 N.unique : 2 N.unique : 2
## N.blank : 0 N.blank : 0 N.blank : 0 N.blank : 0
## Min.nchar: 3 Min.nchar: 4 Min.nchar: 6 Min.nchar: 7
## Max.nchar: 7 Max.nchar: 6 Max.nchar: 9 Max.nchar: 11
## NAs : 19
##
## whitemom
## Length :1000
## N.unique : 2
## N.blank : 0
## Min.nchar: 5
## Max.nchar: 9
##
##
colSums(is.na(births))
## fage mage mature weeks premie
## 114 0 0 0 0
## visits gained weight lowbirthweight sex
## 56 42 0 0 0
## habit marital whitemom
## 19 0 0
The dataset has missing values in fage (father’s age),
visits, gained, and habit. I am
not using fage, so I only need to remove rows missing
habit, gained, or visits.
# Clean the data and prepare it for modeling
births_clean <- births %>%
select(lowbirthweight, habit, mage, gained, visits, marital) %>%
filter(!is.na(habit), !is.na(gained), !is.na(visits)) %>%
mutate(
low = ifelse(lowbirthweight == "low", 1, 0),
habit = factor(habit, levels = c("nonsmoker", "smoker")),
marital = factor(marital, levels = c("married", "not married"))
)
dim(births_clean)
## [1] 894 7
table(births_clean$lowbirthweight)
##
## low not low
## 70 824
After cleaning, there are 894 births left. Only 70 of them (about 7.8%) were low birth weight, so low birth weight is fairly rare in this sample.
# Low birth weight rate by smoking habit and by marital status
births_clean %>%
group_by(habit) %>%
summarise(births = n(),
low_bw = sum(low),
percent_low = round(mean(low) * 100, 1))
## # A tibble: 2 × 4
## habit births low_bw percent_low
## <fct> <int> <dbl> <dbl>
## 1 nonsmoker 816 52 6.4
## 2 smoker 78 18 23.1
births_clean %>%
group_by(marital) %>%
summarise(births = n(),
low_bw = sum(low),
percent_low = round(mean(low) * 100, 1))
## # A tibble: 2 × 4
## marital births low_bw percent_low
## <fct> <int> <dbl> <dbl>
## 1 married 535 38 7.1
## 2 not married 359 32 8.9
# Bar chart: percent of low birth weight babies for smokers vs. nonsmokers
births_clean %>%
group_by(habit) %>%
summarise(percent_low = mean(low) * 100) %>%
ggplot(aes(x = habit, y = percent_low, fill = habit)) +
geom_col(width = 0.6, show.legend = FALSE) +
geom_text(aes(label = paste0(round(percent_low, 1), "%")), vjust = -0.5) +
scale_fill_manual(values = c("nonsmoker" = "#4C9BE8", "smoker" = "#E8734C")) +
labs(title = "Low Birth Weight Rate by Mother's Smoking Habit",
x = "Mother's smoking habit", y = "Percent low birth weight") +
theme_minimal()
# Boxplots: prenatal visits and weight gained by birth weight group
ggplot(births_clean, aes(x = lowbirthweight, y = visits, fill = lowbirthweight)) +
geom_boxplot(show.legend = FALSE) +
labs(title = "Number of Prenatal Visits by Birth Weight Group",
x = "Birth weight group", y = "Number of visits") +
theme_minimal()
ggplot(births_clean, aes(x = lowbirthweight, y = gained, fill = lowbirthweight)) +
geom_boxplot(show.legend = FALSE) +
labs(title = "Weight Gained During Pregnancy by Birth Weight Group",
x = "Birth weight group", y = "Weight gained (lbs)") +
theme_minimal()
The bar chart shows a big difference: about 23.1% of babies born to mothers who smoke were low birth weight, compared to about 6.4% of babies born to nonsmokers. The boxplots show that low birth weight babies tended to have mothers with fewer prenatal visits. Weight gained looks fairly similar between the two groups. Marital status shows only a small difference in the summary table.
Because my outcome (low) is binary, I fit a logistic
regression model with all five predictors. My final model is:
\[ \log\left(\frac{p}{1-p}\right) = \beta_0 + \beta_1(\text{smoker}) + \beta_2(\text{mage}) + \beta_3(\text{gained}) + \beta_4(\text{visits}) + \beta_5(\text{not married}) \]
where p is the probability that a baby is born at a low birth weight.
model <- glm(low ~ habit + mage + gained + visits + marital,
data = births_clean, family = binomial)
summary(model)
##
## Call:
## glm(formula = low ~ habit + mage + gained + visits + marital,
## family = binomial, data = births_clean)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.1452330 0.8685780 -2.470 0.0135 *
## habitsmoker 1.3673578 0.3320914 4.117 3.83e-05 ***
## mage 0.0284844 0.0239826 1.188 0.2349
## gained -0.0007033 0.0082486 -0.085 0.9321
## visits -0.1245104 0.0319009 -3.903 9.50e-05 ***
## maritalnot married -0.0272443 0.2918196 -0.093 0.9256
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 490.98 on 893 degrees of freedom
## Residual deviance: 453.68 on 888 degrees of freedom
## AIC: 465.68
##
## Number of Fisher Scoring iterations: 5
# Coefficients, standard errors, p-values, odds ratios, and 95% confidence intervals
coef_table <- summary(model)$coefficients
ci <- confint(model)
results <- data.frame(
Estimate = round(coef_table[, "Estimate"], 3),
Std_Error = round(coef_table[, "Std. Error"], 3),
P_value = signif(coef_table[, "Pr(>|z|)"], 3),
Odds_Ratio = round(exp(coef_table[, "Estimate"]), 3),
OR_Lower_95 = round(exp(ci[, 1]), 3),
OR_Upper_95 = round(exp(ci[, 2]), 3)
)
knitr::kable(results, caption = "Logistic regression results with odds ratios and 95% confidence intervals")
| Estimate | Std_Error | P_value | Odds_Ratio | OR_Lower_95 | OR_Upper_95 | |
|---|---|---|---|---|---|---|
| (Intercept) | -2.145 | 0.869 | 1.35e-02 | 0.117 | 0.021 | 0.630 |
| habitsmoker | 1.367 | 0.332 | 3.83e-05 | 3.925 | 2.013 | 7.448 |
| mage | 0.028 | 0.024 | 2.35e-01 | 1.029 | 0.981 | 1.078 |
| gained | -0.001 | 0.008 | 9.32e-01 | 0.999 | 0.983 | 1.015 |
| visits | -0.125 | 0.032 | 9.50e-05 | 0.883 | 0.829 | 0.940 |
| maritalnot married | -0.027 | 0.292 | 9.26e-01 | 0.973 | 0.545 | 1.717 |
Interpretation of coefficients:
To check how well the model predicts low birth weight, I made a confusion matrix and an ROC curve. First, I used the standard probability threshold of 0.5.
births_clean$pred_prob <- predict(model, type = "response")
pred_05 <- ifelse(births_clean$pred_prob > 0.5, 1, 0)
table(Predicted = pred_05, Actual = births_clean$low)
## Actual
## Predicted 0 1
## 0 823 70
## 1 1 0
With a threshold of 0.5, the model predicts “not low” for almost every baby, so it misses all 70 low birth weight babies. This happens because low birth weight is rare (about 7.8%), so the predicted probabilities are almost always below 0.5. A better threshold for a rare outcome is the overall rate of low birth weight in the data (about 0.078).
threshold <- mean(births_clean$low)
pred_adj <- ifelse(births_clean$pred_prob > threshold, 1, 0)
cm <- table(Predicted = pred_adj, Actual = births_clean$low)
cm
## Actual
## Predicted 0 1
## 0 598 29
## 1 226 41
TN <- cm["0", "0"]; FP <- cm["1", "0"]
FN <- cm["0", "1"]; TP <- cm["1", "1"]
data.frame(
Threshold = round(threshold, 3),
Accuracy = round((TP + TN) / sum(cm), 3),
Sensitivity = round(TP / (TP + FN), 3),
Specificity = round(TN / (TN + FP), 3)
)
## Threshold Accuracy Sensitivity Specificity
## 1 0.078 0.715 0.586 0.726
roc_curve <- roc(births_clean$low, births_clean$pred_prob, quiet = TRUE)
plot(roc_curve, col = "#E8734C", lwd = 2, legacy.axes = TRUE,
main = "ROC Curve for Low Birth Weight Model")
abline(a = 0, b = 1, lty = 2, col = "gray")
auc(roc_curve)
## Area under the curve: 0.6929
Using the 0.078 threshold, the model has an accuracy of about 71%, a sensitivity of about 59% (it correctly finds 41 of the 70 low birth weight babies), and a specificity of about 73% (it correctly identifies 598 of the 824 babies who were not low birth weight). The AUC is about 0.69. An AUC of 0.5 would be no better than guessing and 1.0 would be perfect, so 0.69 means the model has fair (but not strong) ability to tell low and not low birth weight babies apart.
The results show that smoking during pregnancy is the strongest predictor of low birth weight in this data. Babies of mothers who smoke had about 3.9 times higher odds of being low birth weight, even after accounting for the mother’s age, weight gain, prenatal visits, and marital status. More prenatal visits were also linked to lower odds of low birth weight. The mother’s age, weight gain, and marital status were not significant. These results support my prediction and suggest that helping pregnant mothers quit smoking and making it easier for them to get regular prenatal care could reduce low birth weight.
null_model <- glm(low ~ 1, data = births_clean, family = binomial)
mcfadden_r2 <- 1 - as.numeric(logLik(model) / logLik(null_model))
data.frame(AIC = round(AIC(model), 2),
McFadden_R2 = round(mcfadden_r2, 3))
## AIC McFadden_R2
## 1 465.68 0.076
The model fit is modest. The AIC is 465.68 and McFadden’s pseudo-R² is about 0.076, which means the predictors explain only a small part of why some babies are born at a low birth weight. This makes sense because birth weight is also affected by many factors that are not in the dataset, such as the mother’s health conditions, nutrition, and income. The model also has some limitations. Low birth weight is rare, with only 70 cases, so some estimates have wide confidence intervals. The link between visits and low birth weight may also be partly reversed: babies who are born early have less time for prenatal visits, so fewer visits may be a result of a shorter pregnancy, not only a cause of low birth weight. Finally, this is observational data, so the results show associations, not cause and effect.
For future research, I could add the length of pregnancy
(weeks) to see how much of the smoking and visits effects
are explained by babies being born early. I could also test an
interaction term between smoking and the mother’s age, use a larger
sample with more low birth weight cases, or add variables such as
income, education, and access to health insurance.
Centers for Disease Control and Prevention, National Center for Health Statistics. (2016). Natality Detail File, 2014 United States. Inter-university Consortium for Political and Social Research. https://doi.org/10.3886/ICPSR36461.v1
OpenIntro. (n.d.). births14: US births [Data set]. https://www.openintro.org/data/index.php?data=births14
Robin, X., Turck, N., Hainard, A., Tiberti, N., Lisacek, F., Sanchez, J.-C., & Müller, M. (2011). pROC: An open-source package for R and S+ to analyze and compare ROC curves. BMC Bioinformatics, 12, 77.
Wickham, H., et al. (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686.