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.

Exploratory analysis

Load the nc data set into our workspace.

download.file("http://www.openintro.org/stat/data/nc.RData", destfile = "nc.RData")
load("nc.RData")
head(nc)
##   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.
  1. What are the cases in this data set? How many cases are there in our sample?

In statistics, cases mean observations.

nrow(nc)
## [1] 1000

There are 1000 observation in the data sample.

As a first step in the analysis, we should consider summaries of the data. This can be done using the summary command:

summary(nc)
##       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.

The variables fage (female age), mage (male age), weeks, visits, gained (weight gained during pregnancy) and weight(baby weight) are numerical while variables mature, premie, marital, lowbirthweight, gender, habit and whitemom are categorical. In order to check whether numerical variables have outliers lets built box plots.

boxplot(nc$fage, main="Female Age")

boxplot(nc$mage, main="Male Age")

boxplot(nc$weeks, main="Weeks")

boxplot(nc$visits, main="Visits")

boxplot(nc$gained, main="Weight Gained During Pregnancy")

boxplot(nc$weight, main="Baby Weight")

According to box plots results, variables fage (female age), mage(male age) and gained (weight gained during pregnancy) have values that are far above maximum while variables weeks, visits and weight(baby weight) have values that are far above maximum and values that are far below minimum.

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.

  1. Make a side-by-side box plot of habit and weight. What does the plot highlight about the relationship between these two variables?
boxplot(nc$weight~nc$habit, main="Weight vs Habit", xlab="Smoking Habit", ylab="Weight")

summary(subset(nc$weight,nc$habit=="smoker"))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.690   6.077   7.060   6.829   7.735   9.190
summary(subset(nc$weight,nc$habit=="nonsmoker"))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   6.440   7.310   7.144   8.060  11.750

According to the box plot and summary statistics, the median baby weight is greater for non-smoking moms than for smoking moms. Also, 50% of babies (the range from 1st to 3rd quartiles) of non-smoking moms are heavier than 50% of babies of smoking moms. Furthermore, the maximum baby weight is greater for non-smoking moms than for smoking moms while minimum baby weight is almost the same for both groups. In addition, both samples have outliers. Non-smoking moms gave births to babies whose weight is far above the maximum and is far below the minimum whereas smoking moms gave births to babies whose weight is far below the minimum.

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.

by(nc$weight, nc$habit, mean)
## 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 .

Inference

  1. Check if the conditions necessary for inference are satisfied. Note that you will need to obtain sample sizes to check the conditions. You can compute the group size using the same by command above but replacing mean with length.
by(nc$weight, nc$habit, length)
## nc$habit: nonsmoker
## [1] 873
## -------------------------------------------------------- 
## nc$habit: smoker
## [1] 126

First, according to study description, we were given random sample (“We will work with a random sample of observations from this data set”). Since samples (the sample of non-smoking moms and the sample of smoking moms) were taken from from random sample nc they are also random.

Second, we can assume that the random samples are independent since one birth doesn’t depend on the other birth and births in one sample reveal no information about births of the other sample. Moreover, both samples are less than 10% of the population (I would assume that the number of women who gave birth in 2004 in North Caroline is much larger).

Third, both random samples sizes are above 30.

So that, the conditions necessary for inference are satisfied.

  1. Write the hypotheses for testing if the average weights of babies born to smoking and non-smoking mothers are different.

Null Hypotheses : Average weights of babies born to smoking mothers are the same compared to the non-smoking mothers. Alternative Hypotheses: Average weights of babies born to smoking and non-smoking mothers are NOT the same.

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")
## 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

The null hypotheses is rejected. There is a difference in average weights of babies born to smoking mothers and babies born to non-smoking mothers.

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.

  1. Change the 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 )

On your own

inference(y = nc$weeks,est = "mean",conflevel = 95,type = "ci",alternative = "twosided",method = "theoretical")
## Warning: Confidence level converted to 0.95.
## Single mean 
## Summary statistics:

## mean = 38.3347 ;  sd = 2.9316 ;  n = 998 
## Standard error = 0.0928 
## 95 % Confidence interval = ( 38.1528 , 38.5165 )

The average length of pregnancies will be between 38.1528 and 38.5165 of the true population mean 95% of the time.

inference(y = nc$weeks,est = "mean",conflevel = 90,type = "ci",alternative = "twosided",method = "theoretical")
## Warning: Confidence level converted to 0.9.
## Single mean 
## Summary statistics:

## mean = 38.3347 ;  sd = 2.9316 ;  n = 998 
## Standard error = 0.0928 
## 90 % Confidence interval = ( 38.182 , 38.4873 )

The average length of pregnancies will be between 38.128 and 38.4875 of the true population mean 90% of the time.

Null Hypotheses : The average weight gained by younger mothers is the same than the average weight gained by mature mothers. Alternative Hypotheses: The average weight gained by younger mothers is NOT the same than the average weight gained by mature mothers.

Let’s figure out types of maturity.

levels(nc$mature)
## [1] "mature mom"  "younger mom"

Now, let’s run inference function in order to conduct hypothesis testing.

inference(y = nc$weight, x = nc$mature, est = "mean", type = "ci", null = 0, 
          alternative = "twosided", method = "theoretical", 
          order = c("mature mom","younger mom"))
## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_mature mom = 133, mean_mature mom = 7.1256, sd_mature mom = 1.6591
## n_younger mom = 867, mean_younger mom = 7.0972, sd_younger mom = 1.4855

## Observed difference between means (mature mom-younger mom) = 0.0283
## 
## Standard error = 0.1525 
## 95 % Confidence interval = ( -0.2705 , 0.3271 )
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
cutoff_age <- nc %>% group_by(mature)  %>%  summarise(min=min(mage),max=max(mage))
cutoff_age
## # A tibble: 2 x 3
##        mature   min   max
##        <fctr> <dbl> <dbl>
## 1  mature mom    35    50
## 2 younger mom    13    34

Women aged from 35 to 50 are considered to be mature moms while women aged from 13 to 34 are considered to be younger moms.

Let’s consider the possible relationship between length of pregnancy in weeks and mom’s marital status. The first question is whether average length of pregnancy in weeks varies for married and unmarried moms.

Let’s build

boxplot(nc$weeks~nc$marital, main="Weeks vs Marital", xlab="Marital", ylab="Weeks")

by(nc$weeks, nc$marital, mean)
## nc$marital: married
## [1] 38.08031
## -------------------------------------------------------- 
## nc$marital: not married
## [1] NA
by(nc$weeks, nc$marital, length)
## nc$marital: married
## [1] 386
## -------------------------------------------------------- 
## nc$marital: not married
## [1] 613
summary(subset(nc$weeks,nc$marital=="married"))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   20.00   37.00   39.00   38.08   40.00   45.00
summary(subset(nc$weeks,nc$marital=="not married"))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##    22.0    38.0    39.0    38.5    40.0    45.0       1

According to the box plot and summary statistics, the median pregnancy length is almost the same(very small difference) for married and unmarried moms. Also, pregnancies of 50% of married women (the range from 1st to 3rd quartiles) last less that pregnancies of 50% of married women. Furthermore, the maximum pregnancy length is greater for married moms than for unmarried moms while minimum pregnancy length is greater for unmarried moms than for married moms. In addition, both samples have outlines. Both samples contains pregnancies that last far below the minimum pregnancy length and far above the maximum pregnancy length.

Let’s state the hypotheses.

Null Hypotheses : Average length of pregnancy of married moms is the same as average length of pregnancy of unmarried moms. Alternative Hypotheses: Average length of pregnancy of married moms is NOT the same as average length of pregnancy of unmarried moms.

Next, we introduce a new function, inference, that we will use for conducting hypothesis tests and constructing confidence intervals.

Now, we have to check whether the conditions necessary for inference are satisfied.

Let’s find samples sizes.

by(nc$weeks, nc$marital, length)
## nc$marital: married
## [1] 386
## -------------------------------------------------------- 
## nc$marital: not married
## [1] 613

First, according to study description, we were given random sample (“We will work with a random sample of observations from this data set”). Since samples (sample of married moms and sample of unmarried moms) were taken from from random sample nc they are also random.

Second, we can assume that the random samples are independent since one pregnancy doesn’t depend on the other pregnancy and pregnancies in one sample reveal no information about pregnancies of the other sample. Moreover, both samples are less than 10% of the population (I would assume that the number of women who gave birth in 2004 in North Caroline is much larger).

Third, both random samples sizes are above 30.

Now let’s run inference function.

inference(y = nc$weeks, x = nc$marital, est = "mean", type = "ht", null = 0, 
          alternative = "twosided", method = "theoretical")
## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_married = 386, mean_married = 38.0803, sd_married = 3.4243
## n_not married = 612, mean_not married = 38.4951, sd_not married = 2.5628
## Observed difference between means (married-not married) = -0.4148
## 
## H0: mu_married - mu_not married = 0 
## HA: mu_married - mu_not married != 0 
## Standard error = 0.203 
## Test statistic: Z =  -2.046 
## p-value =  0.0408

Null hypotheses is rejected. The hypotheses testing showed that there is a difference in average pregnancy length of married and unmarried moms.

This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was adapted for OpenIntro by Mine Çetinkaya-Rundel from a lab written by the faculty and TAs of UCLA Statistics.