SharedCDS-102 / Lab Week 10 - Statistical inference of North Carolina birth records / CDS-102 Lab Week 10 Workbook.htmlOpen in CoCalc

In [2]:

```
# Run this code block to load the Tidyverse package
.libPaths(new = "~/Rlibs")
library(tidyverse)
# Load inference() function from file "inference.RData"
load("inference.RData")
```

In [3]:

```
nc.data<-read.csv("nc.csv")
```

The code below generates a full summary statistics report by running the summary() function on the dataset.

In [4]:

```
summary(nc.data)
```

The code below filters out entries in the data set with 'NA' values for the 'habit' variable.

In [5]:

```
nc.data<-filter(nc.data,habit!='NA')
nc.data.nonsmoker<-filter(nc.data,habit=='nonsmoker')
nc.data.smoker<-filter(nc.data,habit=='smoker')
```

The code below plots a (frequency) histogram of the habit and weight variables in the dataset. The code plots both histograms on the same chart using the geom_histogram() function including inputs position="identity" and alpha=0.3 so that the plot is readable.

The code saves the histogram to a .png file using the ggsave() function.

In [6]:

```
options(repr.plot.width = 9, repr.plot.height = 4)
both_hist<-ggplot(nc.data) + geom_histogram(mapping = aes(x = weight, y = ..density.., fill = habit), binwidth = 1, position = "identity", alpha = 0.3)
ggsave("both_hist.png", plot = both_hist, device="png", scale=1, width=5, height=4)
both_hist
```

The code below uses the summary() function on each of the 'habit' subsets to calculate the mean and standard deviation of birth weights for the group of non-smokers and the group of smokers. The smoking group does have a slightly lower mean birth weight. This may suggest a correlation between smoking and birth weight. We will find if this difference is statistically significant in later steps.

In [7]:

```
stat.table.nonsmoker<-summarise(nc.data.nonsmoker,
mean=mean(weight), sd=sd(weight))
stat.table.nonsmoker
stat.table.smoker<-summarise(nc.data.smoker,
mean=mean(weight), sd=sd(weight))
stat.table.smoker
```

For this task, the null and alternative hypotheses are specified for testing if the average weights of babies born to smoking and non-smoking mothers are different. They are shown below.

H0: There is no difference in the mean birth weights between babies born to smokers and babies born to non-smokers.

Ha: There is a difference in the mean birth weights between babies born to smokers and babies born to non-smokers.

The code below uses the inference() function to test the hypotheses specified in the previous step. The inference() function simplifies the hypothesis testing procedure, effectively hiding the manual computational work needed to perform them. For the code below, the mean birth weight is estimated for both smoking and non-smoking mothers and the type of test is specified as a 'hypothesis test'. The test is based on the Central Limit Theorem which is specified by the parameter 'method' and setting it to 'theoretical'.

In [8]:

```
inference(y = weight, x = habit, data = nc.data,
statistic = "mean", type = "ht", null = 0,
alternative = "twosided", method = "theoretical",
order = c("smoker", "nonsmoker"))
```

The code below uses the inference() function again but this time the 'type' parameter is changed to 'ci' to produce a confidence interval. The 'null' and 'alternative' inputs are removed becaue the confidence interval will indicate a non-rejection of the null hypothesis if the value 0 is included within the confidence interval (this suggests that there is literally zero difference between the true means of both populations). The confidence interval indicates the range of values in which the difference between the true means of both populations lies.

In [9]:

```
inference(y = weight, x = habit, data = nc.data,
statistic = "mean", type = "ci",
method = "theoretical",
order = c("smoker", "nonsmoker"))
```

The code below calculates a 95% confidence interval for the average length of pregnancies (weeks variable). Rather than finding a difference between true means of true populations, this confidence interval just finds the range of values in which the true mean of the variable for the population of interest lies (in this case it is the true mean of the length of pregnancy in weeks for the entire population of women in North Carolina).

In [10]:

```
inference(y = weeks, data = nc.data,
statistic = "mean", type = "ci",
method = "theoretical",
order = c("smoker", "nonsmoker"))
```

The code below calculates a new confidence interval for the same parameter at the 90% confidence level. The confidence level is changed by adding a new argument to the function: conf_level = 0.90.

In [11]:

```
inference(y = weeks, data = nc.data,
statistic = "mean", type = "ci",
method = "theoretical",
order = c("smoker", "nonsmoker"),conf_level=.90)
```

The code below tests the hypothesis that there is no difference in mean number of hospital visits for pregnant married women and pregnant unmarried women. The code below uses the inference() function to perform a hypothesis test and a confidence interval using an Î± level of .05. This defines both the p-value threshold for statistical significance and the size of the confidence interval.

In [12]:

```
inference(y = visits, x = marital, data = nc.data,
statistic = "mean", type = "ht", null = 0,
alternative = "twosided", method = "theoretical",
order = c("married", "unmarried"))
```

In [13]:

```
inference(y = visits, x = marital, data = nc.data,
statistic = "mean", type = "ci",
method = "theoretical",
order = c("married", "unmarried"))
```

In [ ]:

```
```