In 2004, the state of North Carolina released a large data set containing information on births recorded in this state. This data set is useful to researchers studying the relation between habits and practices of expectant mothers and the birth of their children. We will work with a random sample of observations from this data set.
Load the nc
data set into our workspace.
## Warning: package 'ggplot2' was built under R version 3.5.1
## fage mage mature weeks premie visits marital gained weight
## 1 NA 13 younger mom 39 full term 10 married 38 7.63
## 2 NA 14 younger mom 42 full term 15 married 20 7.88
## 3 19 15 younger mom 37 full term 11 married 38 6.63
## 4 21 15 younger mom 41 full term 6 married 34 8.00
## 5 NA 15 younger mom 39 full term 9 married 27 6.38
## 6 NA 15 younger mom 38 full term 19 married 22 5.38
## lowbirthweight gender habit whitemom
## 1 not low male nonsmoker not white
## 2 not low male nonsmoker not white
## 3 not low female nonsmoker white
## 4 not low male nonsmoker white
## 5 not low female nonsmoker not white
## 6 low male nonsmoker not white
We have observations on 13 different variables, some categorical and some numerical. The meaning of each variable is as follows.
variable | description |
---|---|
fage |
father’s age in years. |
mage |
mother’s age in years. |
mature |
maturity status of mother. |
weeks |
length of pregnancy in weeks. |
premie |
whether the birth was classified as premature (premie) or full-term. |
visits |
number of hospital visits during pregnancy. |
marital |
whether mother is married or not married at birth. |
gained |
weight gained by mother during pregnancy in pounds. |
weight |
weight of the baby at birth in pounds. |
lowbirthweight |
whether baby was classified as low birthweight (low ) or not (not low ). |
gender |
gender of the baby, female or male . |
habit |
status of the mother as a nonsmoker or a smoker . |
whitemom |
whether mom is white or not white . |
There are 13 cases.
## fage mage mature weeks
## Min. :14.00 Min. :13 mature mom :133 Min. :20.00
## 1st Qu.:25.00 1st Qu.:22 younger mom:867 1st Qu.:37.00
## Median :30.00 Median :27 Median :39.00
## Mean :30.26 Mean :27 Mean :38.33
## 3rd Qu.:35.00 3rd Qu.:32 3rd Qu.:40.00
## Max. :55.00 Max. :50 Max. :45.00
## NA's :171 NA's :2
## premie visits marital gained
## full term:846 Min. : 0.0 married :386 Min. : 0.00
## premie :152 1st Qu.:10.0 not married:613 1st Qu.:20.00
## NA's : 2 Median :12.0 NA's : 1 Median :30.00
## Mean :12.1 Mean :30.33
## 3rd Qu.:15.0 3rd Qu.:38.00
## Max. :30.0 Max. :85.00
## NA's :9 NA's :27
## weight lowbirthweight gender habit
## Min. : 1.000 low :111 female:503 nonsmoker:873
## 1st Qu.: 6.380 not low:889 male :497 smoker :126
## Median : 7.310 NA's : 1
## Mean : 7.101
## 3rd Qu.: 8.060
## Max. :11.750
##
## whitemom
## not white:284
## white :714
## NA's : 2
##
##
##
##
As you review the variable summaries, consider which variables are categorical and which are numerical. For numerical variables, are there outliers? If you aren’t sure or want to take a closer look at the data, make a graph.
## 'data.frame': 1000 obs. of 13 variables:
## $ fage : int NA NA 19 21 NA NA 18 17 NA 20 ...
## $ mage : int 13 14 15 15 15 15 15 15 16 16 ...
## $ mature : Factor w/ 2 levels "mature mom","younger mom": 2 2 2 2 2 2 2 2 2 2 ...
## $ weeks : int 39 42 37 41 39 38 37 35 38 37 ...
## $ premie : Factor w/ 2 levels "full term","premie": 1 1 1 1 1 1 1 2 1 1 ...
## $ visits : int 10 15 11 6 9 19 12 5 9 13 ...
## $ marital : Factor w/ 2 levels "married","not married": 1 1 1 1 1 1 1 1 1 1 ...
## $ gained : int 38 20 38 34 27 22 76 15 NA 52 ...
## $ weight : num 7.63 7.88 6.63 8 6.38 5.38 8.44 4.69 8.81 6.94 ...
## $ lowbirthweight: Factor w/ 2 levels "low","not low": 2 2 2 2 2 1 2 1 2 2 ...
## $ gender : Factor w/ 2 levels "female","male": 2 2 1 2 1 2 2 2 2 1 ...
## $ habit : Factor w/ 2 levels "nonsmoker","smoker": 1 1 1 1 1 1 1 1 1 1 ...
## $ whitemom : Factor w/ 2 levels "not white","white": 1 1 2 2 1 1 1 1 2 2 ...
The numeric variables are fage, mage, weeks, visits, gained, and weight. There are plenty of outliers in terms of length of pregnancy and weight of newborn, as can be seen from the boxplots below.
## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
Consider the possible relationship between a mother’s smoking habit and the weight of her baby. Plotting the data is a useful first step because it helps us quickly visualize trends, identify strong associations, and develop research questions.
habit
and weight
. What does the plot highlight about the relationship between these two variables?The box plots show how the medians of the two distributions compare, but we can also compare the means of the distributions using the following function to split the weight
variable into the habit
groups, then take the mean of each using the mean
function.
## nc$habit: nonsmoker
## [1] 7.144273
## --------------------------------------------------------
## nc$habit: smoker
## [1] 6.82873
There is an observed difference, but is this difference statistically significant? In order to answer this question we will conduct a hypothesis test .
by
command above but replacing mean
with length
.## nc$habit: nonsmoker
## [1] 873
## --------------------------------------------------------
## nc$habit: smoker
## [1] 126
. The individual observations must be independent. A random sample from less than 10% of the population ensures the observations are independent. In experiments, we generally require that subjects are randomized into groups.
. Other conditions focus on sample size and skew. For example, if the sample size is too small, the skew too strong, or extreme outliers are present, then the normal model for the sample mean will fail.
mean.nonsmoker <- round(mean(nc$weight[nc$habit == "nonsmoker"], na.rm = T),3)
mean.smoker <- round(mean(nc$weight[nc$habit == "smoker"], na.rm = T),3)
print(paste0("newborn weight of non-smoking mother: ", mean.nonsmoker))
## [1] "newborn weight of non-smoking mother: 7.144"
## [1] "newborn weight of smoking mother: 6.829"
## [1] "difference in means: 0.315"
var.nonsmoker <- round(var(nc$weight[nc$habit == "nonsmoker"], na.rm=T),3)
var.smoker <- round(var(nc$weight[nc$habit == "smoker"], na.rm=T),3)
se <- round(sqrt((var.nonsmoker/ 873) + (var.smoker/126)),3)
t.score <- round(abs(qt(0.025, 872)),3)
print(paste0("estimated critical t-score: ", t.score))
## [1] "estimated critical t-score: 1.963"
## [1] "standard error: 0.134"
low.bound <- round((mean.diff - t.score * se), 3)
high.bound <- round((mean.diff + t.score * se), 3)
print(paste0("95% confidence interval: ", low.bound, ", ", high.bound))
## [1] "95% confidence interval: 0.052, 0.578"
As the confidence interval is always positive, we can say with confidence the difference in newborn is NOT due to chance.
Next, we introduce a new function, inference
, that we will use for conducting hypothesis tests and constructing confidence intervals.
inference(y = nc$weight, x = nc$habit, est = "mean", type = "ht", null = 0,
alternative = "twosided", method = "theoretical")
## Warning: package 'BHH2' was built under R version 3.5.1
## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_nonsmoker = 873, mean_nonsmoker = 7.1443, sd_nonsmoker = 1.5187
## n_smoker = 126, mean_smoker = 6.8287, sd_smoker = 1.3862
## Observed difference between means (nonsmoker-smoker) = 0.3155
##
## H0: mu_nonsmoker - mu_smoker = 0
## HA: mu_nonsmoker - mu_smoker != 0
## Standard error = 0.134
## Test statistic: Z = 2.359
## p-value = 0.0184
type
argument to "ci"
to construct and record a confidence interval for the difference between the weights of babies born to smoking and non-smoking mothers.By default the function reports an interval for (\(\mu_{nonsmoker} - \mu_{smoker}\)) . We can easily change this order by using the order
argument:
inference(y = nc$weight, x = nc$habit, est = "mean", type = "ci", null = 0,
alternative = "twosided", method = "theoretical",
order = c("smoker","nonsmoker"))
## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_smoker = 126, mean_smoker = 6.8287, sd_smoker = 1.3862
## n_nonsmoker = 873, mean_nonsmoker = 7.1443, sd_nonsmoker = 1.5187
## Observed difference between means (smoker-nonsmoker) = -0.3155
##
## Standard error = 0.1338
## 95 % Confidence interval = ( -0.5777 , -0.0534 )
weeks
) and interpret it in context.mean.weeks <- round(mean(nc$weeks, na.rm=T),3)
print(paste0("Mean number of weeks of pregnancy: ", mean.weeks))
## [1] "Mean number of weeks of pregnancy: 38.335"
sd.weeks <- round(sd(nc$weeks, na.rm=T),3)
print(paste0("Standard deviation of weeks of pregnancy: ", sd.weeks))
## [1] "Standard deviation of weeks of pregnancy: 2.932"
conflevel <- 0.95
z.score <- abs(qnorm((1-conflevel)/2))
se.weeks <- round(z.score * (sd.weeks / (sqrt(length(nc$weeks)))),3)
low.bound.weeks <- mean.weeks - se.weeks
high.bound.weeks <- mean.weeks + se.weeks
print(paste0("95% confidence interval of avg. weeks of pregnancy: [", low.bound.weeks, ", ", high.bound.weeks, "]"))
## [1] "95% confidence interval of avg. weeks of pregnancy: [38.153, 38.517]"
conflevel = 0.90
.conflevel <- 0.90
z.score <- abs(qnorm((1-conflevel)/2))
se.weeks <- round(z.score * (sd.weeks / (sqrt(length(nc$weeks)))),3)
low.bound.weeks <- mean.weeks - se.weeks
high.bound.weeks <- mean.weeks + se.weeks
print(paste0("90% confidence interval of avg. weeks of pregnancy: [", low.bound.weeks, ", ", high.bound.weeks, "]"))
## [1] "90% confidence interval of avg. weeks of pregnancy: [38.182, 38.488]"
The null hypothesis states that the weight gain of young and mature mothers is equivalent.
mean.fage <- round(mean(nc$fage, na.rm = T),3)
print(paste0("Mean age of all mothers: ", mean.fage))
## [1] "Mean age of all mothers: 30.256"
mean.gained.young <- round(mean(nc$gained[nc$fage < mean.fage], na.rm = T),3)
mean.gained.mature <- round(mean(nc$gained[nc$fage > mean.fage], na.rm = T),3)
print(paste0("Mean weight gain by young mothers: ", mean.gained.young))
## [1] "Mean weight gain by young mothers: 31.282"
## [1] "Mean weight gain by mature mothers: 30.2"
## nc$fage < mean.fage: FALSE
## [1] 404
## --------------------------------------------------------
## nc$fage < mean.fage: TRUE
## [1] 425
mean.gained.diff <- mean.gained.young - mean.gained.mature
var.young <- round(var(nc$gained[nc$fage < mean.fage], na.rm=T),3)
var.mature <- round(var(nc$gained[nc$fage > mean.fage], na.rm=T),3)
se <- round(sqrt((var.young/ 425) + (var.mature/404)),3)
t.score <- round(abs(qt(0.025, 425)),3)
print(paste0("estimated critical t-score: ", t.score))
## [1] "estimated critical t-score: 1.966"
## [1] "standard error: 0.979"
low.bound <- round((mean.diff - t.score * se), 3)
high.bound <- round((mean.diff + t.score * se), 3)
print(paste0("95% confidence interval: ", low.bound, ", ", high.bound))
## [1] "95% confidence interval: -1.61, 2.24"
As 0 can be found in the 95% confidence interval, we cannot reject the null hypothesis. There is not sufficient evidence that the weight gain differs between young and mature mothers.
## Warning: Removed 190 rows containing non-finite values (stat_smooth).
## Warning: Removed 190 rows containing missing values (geom_point).
In the hypothesis test, I used the mean age of 30 years old and 3 months. Viewing the scatterplot, there is no basis for determining a better break-off point, when age suddenly has an impact on weight gain.
inference
function, report the statistical results, and also provide an explanation in plain language.I would perform a student to determine if race has an impact on the number of hospital visits during pregnancy.
My hypothesis test is that
Average visits of white mothers - Average visits of non-white mothers = 0
inference(y = nc$visits, x = nc$whitemom, est = "mean", type = "ci", null = 0,
alternative = "onesided", method = "theoretical",
order = c("white","not white"))
## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_white = 710, mean_white = 12.3014, sd_white = 3.7701
## n_not white = 279, mean_not white = 11.6272, sd_not white = 4.3644
## Observed difference between means (white-not white) = 0.6742
##
## Standard error = 0.2971
## 95 % Confidence interval = ( 0.0918 , 1.2565 )
The 95 % Confidence interval is ( 0.0918 , 1.2565 ). We have sufficient evidence to conclude that white women did go to the doctor more, but not much more than one-visit on average.