In this lab, we will make sure of the `lattice`

and `tidyverse`

packages.

```
library(lattice)
library(tidyverse)
```

We will also make use of the following helper function that generates summary statistics

```
= function(mydata){
favstats # input mydata is a numerical vector or matrices
= rep(0, 9);
result = summary(mydata);
mysummary 1] = mysummary[1];
result[2] = mysummary[2];
result[3] = mysummary[3];
result[4] = mysummary[4];
result[5] = mysummary[5];
result[6] = mysummary[6];
result[7] = sd(mydata);
result[8] = length(mydata);
result[9] = sum(is.na(mydata));
result[names(result) = c("min", "Q1", "median", "mean", "Q3", "max", "sd", "n", "missing");
result; }
```

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.

```
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` |
whether the mother age under 35 (`younger mom` ) or 35+ (`mature mom` ) |

`weeks` |
length of pregnancy in weeks. |

`premie` |
whether the birth was classified as premature (premie, if `weeks` <37) or full-term (if 37 weeks or more). |

`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` , `weight` < 5.5066, or `not low` , if `weight` \(\ge 5.5066\)). |

`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` . |

**Exercise 1:**What are the cases in this data set? How many cases are there in our 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)`

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.

**Exercise 2:** Make a side-by-side boxplot of `habit`

and `weight`

. What does the plot highlight about the relationship between these two variables?

`boxplot(weight~habit, data=nc)`

The box plots show how the medians of the two distributions compare. There is an observed difference, but is this difference statistically significant? In order to answer this question we will need to conduct a hypothesis test .

**Exercise 3:** Check if the conditions necessary for inference are satisfied. You can obtain the sample sizes of the two groups from the command `dim()`

, after you split the data into the two groups using the `subset()`

function.

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

Now, let’s conduct that hypothesis test.

```
# split the weight variable into the habit groups
= subset(nc, habit == "smoker");
nc.smoker = subset(nc, habit == "nonsmoker");
nc.nonsmoker
= favstats(nc.smoker$weight)
fv.smoker = favstats(nc.nonsmoker$weight)
fv.nonsmoker = rbind(fv.nonsmoker, fv.smoker)
fv
= fv[1, "mean"];
mean.ns = fv[2, "mean"];
mean.s
= fv[1, "sd"]
sd.ns = fv[2, "sd"]
sd.s
= fv[1, "n"]
n.ns = fv[2, "n"]
n.s
= mean.ns - mean.s
p = sqrt((sd.ns^2 / n.ns) + (sd.s^2 / n.s))
se = (p - 0) / se
t
2 * pt(-abs(t), df = min(n.ns-1, n.s-1))
```

Let’s pause for a moment to go through this code. First, we split `nc`

into two groups broken down by `habit`

. Then we compute summary statistics on the two subsets using the `favstats()`

function, these statistics include the number of observations (`n`

), the group means (`mean`

), and the group standard deviations (`sd`

).

Then, we calculate our point estimate, `p`

, which is the difference of the mean weights. We compute the standard error, `se`

, and then our t-statistic, `t`

. Finally, we use the `pt`

function to compute the p-value of the t-statistics. Using the simple formula, the degrees of freedom are \(\min(n_s-1, n_{ns}-1)\).

**Exercise 5:** Construct a confidence interval for the difference between the weights of babies born to smoking and non-smoking mothers.

R has a built-in function `t.test`

that can perform the two-sample t-test and the confidence interval above, though it use the more accurate software formula to calculate the degrees of freedom.

`t.test(weight ~ habit, data=nc)`

Calculate a 95% confidence interval for the average length of pregnancies (

`weeks`

) and interpret it in context.Calculate a new confidence interval for the same parameter at the 90% confidence level.

Conduct a hypothesis test evaluating whether the average weight gained by younger mothers is different than the average weight gained by mature mothers.

Determine the age cutoff for younger and mature mothers. Use a method of your choice, and explain how your method works.

Pick a pair of numerical and categorical variables and come up with a research question evaluating the relationship between these variables. Formulate the question in a way that it can be answered using a hypothesis test and/or a confidence interval. Answer your question by performing the hypothesis test or calculating the confidence interval, and also provide an explanation in plain language.

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.