# Run this code block to load the Tidyverse package
.libPaths(new = "~/Rlibs")
library(tidyverse)
# Load inference() function from file "inference.RData"
load("inference.RData")
nc.data<-read.csv("nc.csv")
The code below generates a full summary statistics report by running the summary() function on the dataset.
summary(nc.data)
The code below filters out entries in the data set with 'NA' values for the 'habit' variable.
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.
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.
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'.
inference(y = weight, x = habit, data = nc.data,
statistic = "mean", type = "ht", null = 0,
alternative = "twosided", method = "theoretical",
order = c("smoker", "nonsmoker"))
The results of the hypothesis test produce a p-value that is less than the significance level which is .05, meaning that the null hypothesis, that there is no statistically significant difference between smokers and nonsmokers, can be rejected.
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.
inference(y = weight, x = habit, data = nc.data,
statistic = "mean", type = "ci",
method = "theoretical",
order = c("smoker", "nonsmoker"))
In this example, both values of the confidence interval are negative, indicating that difference of the second mean from the first mean is negative, meaning that the second population (nonsmokers) have a higher mean birth weight.
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).
inference(y = weeks, data = nc.data,
statistic = "mean", type = "ci",
method = "theoretical",
order = c("smoker", "nonsmoker"))
The results indicate that the true mean of the length of pregnancy for women in North Carolina is somewhere between 38.1 weeks and 38.5 weeks.
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.
inference(y = weeks, data = nc.data,
statistic = "mean", type = "ci",
method = "theoretical",
order = c("smoker", "nonsmoker"),conf_level=.90)
This change in confidence level narrows the range of values containing the true mean of the average length of pregnancy for women in North Carolina.
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.
inference(y = visits, x = marital, data = nc.data,
statistic = "mean", type = "ht", null = 0,
alternative = "twosided", method = "theoretical",
order = c("married", "unmarried"))
inference(y = visits, x = marital, data = nc.data,
statistic = "mean", type = "ci",
method = "theoretical",
order = c("married", "unmarried"))