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.
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. |
##ANSWER 1: The cases in this dataset are a random sample of the birth records released by North Carolina in 2004 (overall population). Using the tail command, we see that there are 1,000 rows in the sample with 13 variables.
## fage mage mature weeks premie visits marital gained weight
## 995 NA 41 mature mom 33 premie 13 not married 0 5.69
## 996 47 42 mature mom 40 full term 10 not married 26 8.44
## 997 34 42 mature mom 38 full term 18 not married 20 6.19
## 998 39 45 mature mom 40 full term 15 not married 32 6.94
## 999 55 46 mature mom 31 premie 8 not married 25 4.56
## 1000 45 50 mature mom 39 full term 14 not married 23 7.13
## lowbirthweight gender habit whitemom
## 995 not low female nonsmoker not white
## 996 not low male nonsmoker not white
## 997 not low female nonsmoker white
## 998 not low female nonsmoker white
## 999 low female nonsmoker not white
## 1000 not low female nonsmoker white
As a first step in the analysis, we should consider summaries of the data. This can be done using the summary command:
## fage mage mature weeks premie
## Min. :14.00 Min. :13 mature mom :133 Min. :20.00 full term:846
## 1st Qu.:25.00 1st Qu.:22 younger mom:867 1st Qu.:37.00 premie :152
## Median :30.00 Median :27 Median :39.00 NA's : 2
## 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
## visits marital gained weight
## Min. : 0.0 married :386 Min. : 0.00 Min. : 1.000
## 1st Qu.:10.0 not married:613 1st Qu.:20.00 1st Qu.: 6.380
## Median :12.0 NA's : 1 Median :30.00 Median : 7.310
## Mean :12.1 Mean :30.33 Mean : 7.101
## 3rd Qu.:15.0 3rd Qu.:38.00 3rd Qu.: 8.060
## Max. :30.0 Max. :85.00 Max. :11.750
## NA's :9 NA's :27
## lowbirthweight gender habit whitemom
## low :111 female:503 nonsmoker:873 not white:284
## not low:889 male :497 smoker :126 white :714
## NA's : 1 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.
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?##ANSWER 2: The median weight of babies is lower for mothers who smoke versus for those who don’t smoke. The two variables weight and smoking habit are associated or related. Specifically, the median weight of babies of mothers who smoked is less than the median weight of babies whose mothers didn’t smoke. The variability of weight is also lower for smoker mothers than for non-smokers and the max weight is lower for mothers who smoked.
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.##ANSWER 3: The sample size is 1,000 which is less than 10% of the overall births in the state of NC for 2004 (overall population). Also given in the problem statement is that this sample was randomly selected, hence, the observations are independent. The distribution looks nearly normal with some skew, both sample sizes are larger than 30 (which is the sample size for moderate skew to be not problematic).
## nc$habit: nonsmoker
## [1] 873
## ------------------------------------------------------------
## nc$habit: smoker
## [1] 126
4. Write the hypotheses for testing if the average weights of babies born to smoking and non-smoking mothers are different.
##ANSWER 4: The null hypothesis is that the average weight of babies born to smoking and non-smoking mothers is the same. The alternative hypothesis is that the average weight of babies born to smoking and non-smoking mothers is different (a two-tailed hypothesis).
##H0: Means − Meanns = 0 ##H0: Means − Meanns != 0
##where μs is the average weight of babies born to smoking mothers and μns is the average weight of babies born to non-smoking mothers.
Next, we introduce a new function, inference, that we will use for conducting hypothesis tests and constructing confidence intervals.
## Loading required package: shiny
## Loading required package: openintro
## Please visit openintro.org for free statistics materials
##
## Attaching package: 'openintro'
## The following object is masked _by_ '.GlobalEnv':
##
## normTail
## The following object is masked from 'package:BHH2':
##
## dotPlot
## The following objects are masked from 'package:datasets':
##
## cars, trees
## Loading required package: OIdata
## Loading required package: RCurl
## Loading required package: maps
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
## The following object is masked from 'package:openintro':
##
## diamonds
## Loading required package: markdown
##
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics
## This package is designed to support this course. The text book used
## is OpenIntro Statistics, 3rd Edition. You can read this by typing
## vignette('os3') or visit www.OpenIntro.org.
##
## The getLabs() function will return a list of the labs available.
##
## The demo(package='DATA606') will list the demos that are available.
##
## Attaching package: 'DATA606'
## The following object is masked _by_ '.GlobalEnv':
##
## inference
## The following object is masked from 'package:utils':
##
## demo
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
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.
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.inference(y = nc$weight, x = nc$habit, est = "mean", type = "ci", 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
##
## Standard error = 0.1338
## 95 % Confidence interval = ( 0.0534 , 0.5777 )
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. Note that since you’re doing inference on a single population parameter, there is no explanatory variable, so you can omit the x variable from the function.##We can be 95% confident that the true average length of pregnancies (population parameter) in the state of NC in 2004 falls between 38.16 and 38.52. Another interpretation would be that 95% of the confidenec intervals generated would contain the true average length of pregnancies.
inference(y = nc$weeks, est = "mean", type = "ci", null = 0,
alternative = "twosided", method = "theoretical")## Single mean
## Summary statistics:
## mean = 38.3347 ; sd = 2.9316 ; n = 998
## Standard error = 0.0928
## 95 % Confidence interval = ( 38.1528 , 38.5165 )
conflevel = 0.90.inference(y = nc$weeks, est = "mean", type = "ci", conflevel = 0.9, null = 0,
alternative = "twosided", method = "theoretical")## Single mean
## Summary statistics:
## mean = 38.3347 ; sd = 2.9316 ; n = 998
## Standard error = 0.0928
## 90 % Confidence interval = ( 38.182 , 38.4873 )
##ANSWER: The null hypothesis is that the average weight gained by younger mothers is the same as the average weight gained by mature mothers. The alternative hypothesis is that the average weight gained by younger mothers is different than the average weight gained by mature mothers (a two-tailed hypothesis).
##H0: Meanym − Meanm = 0 ##H0: Meanym − Meanm != 0
##where μym is the mean weight gained by younger mothers and μm is the mean weight gained by mature mothers.
##The p-value of 0.1686 is larger than the significance level of 0.05.Hence, we fail to reject the null hypothesis as there is a 16.86% probability that any difference in average weight gain between younger and mature mothers is due to chance alone.
inference(y = nc$gained, x = nc$mature, est = "mean", type = "ht", null = 0, alternative = "twosided",
method = "theoretical")## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_mature mom = 129, mean_mature mom = 28.7907, sd_mature mom = 13.4824
## n_younger mom = 844, mean_younger mom = 30.5604, sd_younger mom = 14.3469
## Observed difference between means (mature mom-younger mom) = -1.7697
##
## H0: mu_mature mom - mu_younger mom = 0
## HA: mu_mature mom - mu_younger mom != 0
## Standard error = 1.286
## Test statistic: Z = -1.376
## p-value = 0.1686
- Now, a non-inference task: Determine the age cutoff for younger and mature mothers. Use a method of your choice, and explain how your method works.
##The cutoff for younger mothers is 13 - 34 and the cutoff for mature mothers is 35 - 50.
## [1] 13
## [1] 34
## [1] 35
## [1] 50
inference function, report the statistical results, and also provide an explanation in plain language.##Are male babies at birth heavier than female babies? This is the research question. The response variable is numerical and the explanatory variable is categorical.
##The null hypothesis is that babies of both gender weigh the same at birth. The alternative hypothesis is that male babies are heavier than female babies (a one-tailed hypothesis).
##H0: Meanm − Meanf = 0 ##H0: Meanm − Meanf > 0
##where μm is the average weight of male babies at birth and μf is the average weight of female babies at birth.
##The p-value of 0 is smaller than the significance level of 0.05. Hence, we reject the null hypothesis as there is a 0% probability that any difference in average weight at birth between genders is due to chance alone.
## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_male = 497, mean_male = 7.3015, sd_male = 1.5168
## n_female = 503, mean_female = 6.9029, sd_female = 1.4759
## Observed difference between means (male-female) = 0.3986
##
## H0: mu_male - mu_female = 0
## HA: mu_male - mu_female > 0
## Standard error = 0.095
## Test statistic: Z = 4.211
## p-value = 0