North Carolina births

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. Many of the commands in the lab are repeats from previous labs. You may need to look back at old labs to refresh yourself on the commands.

Exploratory analysis

Load the nc data set.

download.file("http://www.openintro.org/stat/data/nc.RData", destfile = "nc.RData")
load("nc.RData")

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.

Now do Exercise 1

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.

Now do Exercise 2.

The box plots show how the medians of the two distributions compare, but we can also compare the means of the distributions by using the following function to split the weight variable into the habit groups, and then take the mean of each using the mean function.

by(nc$weight, nc$habit, mean)

There is an observed difference, but is this difference statistically significant? In order to answer this question we will conduct a hypothesis test.

Inference

Now do Exercise 3.

Next, we introduce a new function, inference, that we will use for conducting hypothesis tests and constructing confidence intervals. Enter the following command in a code chunk as part of exercise 4. When you run this function for the first time, R will take some time to download things. Later runs of the function won’t require downloads.

inference(y = nc$weight, x = nc$habit, est = "mean", type = "ht", null = 0, 
          alternative = "greater", method = "theoretical")

Let’s pause for a moment to go through the arguments of this custom function. The first argument is y, which is the response variable that we are interested in: nc$weight. The second argument is the explanatory variable, x, which is the variable that splits the data into two groups, smokers and non-smokers: nc$habit. The third argument, est, is the parameter we’re interested in: "mean" (other options are "median", or "proportion".) Next we decide on the type of inference we want: a hypothesis test ("ht") or a confidence interval ("ci"). When performing a hypothesis test, we also need to supply the null value, which in this case is 0, since the null hypothesis sets the two population means equal to each other. The alternative hypothesis can be "less", "greater", or "twosided". Lastly, the method of inference can be "theoretical" or "simulation" based.

Now do Exercise 4.

Now do Exercise 5.

Now do Exercise 6.

ANOVA: Analysis of Variance

So far in class, we have performed hypothesis tests for one population or for two populations, but sometimes we may be interested in comparing more than two populations. Consider the variable mature. It is a categorical variable with two categories: mature mom and younger mom. Suppose, however, that we are interested in introducing a third category teen mom. To this end, we introduce a new column to the dataset nc titled mature.new. The code is shown below. Put it in a code chunk in your notebook as part of exercise 8.

nc$mature.new <- nc$mature
levels(nc$mature.new) <- c("mature mom","young mom","teen mom")
nc$mature.new[nc$mage<20] <- "teen mom"

Note that the variable mature.new is a new variable we’re creating. The levels command specifies the possible values for the new variable.

Now do Exercise 7.

While side-by-side boxplots may indicate a relationship between two variables, we need to perform a hypothesis test to determine whether this relationship is significant or merely a consequence of random sampling. But how do we perform a hypothesis test with three populations? It may be helpful to start by thinking about what our hypotheses should be. If we let \(\mu_m\), \(\mu_y\), and \(\mu_t\) denote the true means of the mature, young, and teen mom populations, respectively, then we can state the hypotheses as,

\[\begin{align*} H_0: & \quad \mu_m = \mu_y = \mu_t \\ H_a: & \quad \text{at least one of the means differs from the others.} \end{align*}\]

It may seem intuitive to perform 3 hypothesis tests: teen mom vs. young mom, teen mom vs. mature mom, and mature mom vs. young mom. The code for the teen mom vs. young mom hypothesis test is shown below.

nc.ty <- subset(nc,mature.new %in% c("teen mom","young mom"))
nc.ty$mature.new <- droplevels(nc.ty$mature.new)
inference(y = nc.ty$weight, x = nc.ty$mature.new, est = "mean", type = "ht", null = 0, 
          alternative = "twosided", method = "theoretical")

This code is a bit complicated. Let’s walk through it. First, we’re creating a new variable called nc.ty. The “t” is for “teen mom” and the y is for “young mom” to help us remember the meaning of the variable. We’re using the subset command (we used this in previous labs) to select data for teen mom and young mom. We’re leaving out the mature mom data. Next, the droplevels command gets rid of any unused maturity levels–in this case, it gets rid of the “mature mom” level because we’re not using that one anymore. Finally, the third line uses the inference function from before to conduct a hypothesis test for the difference in the mean birth weights between infants of teen mothers and infants of young mothers.

Now do Exercise 8.

There is a bit of a problem in performing 3 hypothesis tests. Each test has an error associated with it and that error is amplified when multiple tests are performed. For instance, when \(\alpha=0.05\), the probability of not making a type I error is 0.95. That is, the probability that we conclude that two means are the same, given that they actually are the same, is 0.95. In the test that we are performing above, in order to correctly determine that all three means are the same, we would need this to happen three times. Meaning that the probability that we correctly conclude that all three means are the same is, \[ (0.95)(0.95)(0.95) = 0.8574. \] This value is significantly lower than 95%. So the technique that we employed above is biased toward rejecting the null hypothesis. Thankfully, there is a specific test to perform in exactly this type of scenario. It is called ANOVA, which stands for Analysis of Variance. The inner workings of ANOVA are a bit beyond the scope of this course. However, at its core, it is just a hypothesis test that allows us to make conclusions about \(H_0\) in the same way as with any other hypothesis test. The code below performs ANOVA for the three populations of interest. Include the code below in a code chunk as part of exercise 9.

my.anova <- aov(weight~mature.new,data=nc)
summary(my.anova)

Now do Exercise 9.

Acknowledgements

This lab is a modification of a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was written for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel.