In this project, students will demonstrate their understanding of the inference on numerical data with the t-distribution. If not specifically mentioned, students will assume a significance level of 0.05.
Store the ncbirths dataset in your environment in the
following R chunk. Do some exploratory analysis using the str()
function, viewing the dataframe, and reading its documentation to
familiarize yourself with all the variables. None of this will be
graded, just something for you to do on your own.
# Load Openintro Library
library(openintro)
# Store ncbirths in environment
ncbirths <- ncbirths
Construct a 90% t-confidence interval for the average weight gained for North Carolina mothers and interpret it in context.
# Store mean, standard deviation, and sample size of dataset
mncb <- mean(ncbirths$gained, na.rm = TRUE)
sdncb <- sd(ncbirths$gained, na.rm = TRUE)
lncb <- 1000-sum(is.na(ncbirths$gained))
# Calculate t-critical value for 90% confidence
df <- lncb - 1
print(t <- qt(0.90, df))
## [1] 1.282423
# Calculate margin of error
print(moe <- t * (sdncb / sqrt(lncb)))
## [1] 0.5854967
# Boundaries of confidence interval
print(lb <- mncb - moe)
## [1] 29.7403
print(ub <- mncb + moe)
## [1] 30.91129
print(t95 <- qt(0.95, df))
## [1] 1.646423
# Calculate margin of error
print(moe95 <- t95 * (sdncb / sqrt(lncb)))
## [1] 0.7516826
# Boundaries of confidence interval
print(lb95 <- mncb - moe95)
## [1] 29.57411
print(ub95 <- mncb + moe95)
## [1] 31.07748
The average birthweight of European babies is 7.7 lbs. Conduct a hypothesis test by p-value to determine if the average birthweight of NC babies is different from that of European babies.
Write hypotheses
#HO: mu1 = mu2 #HA: mu1 =/= mu2
Test by p-value and decision
# Sample statistics (sample mean, standard deviation, and size)
mwt <- mean(ncbirths$weight, na.rm = TRUE)
sdwt <- sd(ncbirths$weight, na.rm = TRUE)
lwt <- 1000-sum(is.na(ncbirths$weight))
# Test statistic
se <- sdwt/sqrt(lwt)
print(t3 <- (mwt - 7.7) / se)
## [1] -12.55388
# Probability of test statistic by chance
df <- lwt-1
2 * pt(-abs(t3), df)
## [1] 1.135415e-33
In the ncbirths dataset, test if there is a significant difference between the mean age of mothers and fathers.
men <- subset(ncbirths, gender == "male")
women <- subset(ncbirths, gender == "female")
t.test(men$fage, women$mage)$p.value
## [1] 1.22309e-15
In the ncbirths dataset, test if there is a significant difference in length of pregnancy between smokers and non-smokers.
smokers <- subset(ncbirths, habit == "smoker" )
nonsmokers <- subset(ncbirths, habit == "nonsmoker" )
t.test(smokers$weeks, nonsmokers$weeks)$p.value
## [1] 0.6043917
Conduct a hypothesis test at the 0.05 significance level evaluating whether the average weight gained by younger mothers is more than the average weight gained by mature mothers.
younger <- subset(ncbirths, mature == "younger mom" )
mature <- subset(ncbirths, mature == "mature mom" )
t.test(younger$gained, mature$gained)$p.value
## [1] 0.1704274
Determine the weight cutoff for babies being classified as “low” or “not low”. Use a method of your choice, describe how you accomplished your answer, and explain as needed why you believe your answer is accurate. (This is a non-inference task)
low <- subset(ncbirths, lowbirthweight == "low" )
notlow <- subset(ncbirths, lowbirthweight == "not low" )
max(low$weight)
## [1] 5.5
min(notlow$weight)
## [1] 5.56
#the cutoff point is at 5.5 because it is the maximum weight for the low subset of the data.
Pick a pair of numerical and categorical variables from the
ncbirths dataset 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.
premie <- subset(ncbirths, premie == "premie")
fultrm <- subset(ncbirths, premie == "full term")
t.test(premie$weight, fultrm$weight)$p.value
## [1] 1.358422e-30