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.
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.
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.
Lab Task 5##
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'.
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.
Lab Task 6##
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 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.
Lab Task 7##
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).
Single numerical variable
n = 998, y-bar = 38.3347, s = 2.9316
90% CI: (38.1819 , 38.4874)
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.
Lab Task 9##
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.
Do married mothers tend to visit the hospital more than single mothers?
H0: There is no difference in mean number of visits to hospital between married and unmarried mothers.
Ha: There is a difference in mean number of visits to hospital between married and unmarried mothers.