Purpose

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.

Instructions

  1. Update the author line at the top to have your name in it.
  2. You must knit this document to an html file and publish it to RPubs. Once you have published your project to the web, you must copy the web url link into the appropriate Course Project assignment in Blackboard before 11:59pm on the due date.
  3. Answer all the following questions completely. Some may ask for written responses.
  4. Use R chunks for code to be evaluated where needed and always comment all of your code so the reader can understand what your code aims to accomplish.
  5. Proofread your knitted document before publishing it to ensure it looks the way you want it to. Use double spaces at the end of a line to create a line break and make sure text does not have a header label that isn’t supposed to.
  6. Delete these instructions from your published project after completing it..
  7. Question #1 and #3 are to be done by the template laid out. You may choose to complete other questions similarly or by use of a valid shortcut.
  8. Question #3 must be a hypothesis test by p-value. The remaining t-test questions may be done by your choice of either p-value or confidence interval.

Preparation

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

Question 1 - Single Sample t-confidence interval

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

Question 2 - Single Sample t-confidence interval

  1. Construct a new confidence interval for the same parameter as Question 1, but at the 95% confidence level.
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
  1. How does that confidence interval compare to the one in Question #1? #it is slightly larger than the one in question 1.

Question 3 - Single Sample t-test

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.

  1. Write hypotheses
    #HO: mu1 = mu2 #HA: mu1 =/= mu2

  2. 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
  1. Conclusion
    #given the excessively small p.value, there is not enough evidence to disprove HO, so we fail to reject the null hypothesis.

Question 4 - Paired Data t-test

In the ncbirths dataset, test if there is a significant difference between the mean age of mothers and fathers.

  1. Write hypotheses
    #HO: mu1 = mu2 #HO: mu1 =/= mu2
  2. Test by confidence interval or p-value and decision
men <- subset(ncbirths, gender == "male")
women <- subset(ncbirths, gender == "female")

t.test(men$fage, women$mage)$p.value
## [1] 1.22309e-15
  1. Conclusion
    #the p value is significantly below the alpha of 0.05. due to this, we fail to reject the null hypothesis and we accept that there is no difference between the two.

Question 5 - Two Indendent Sample t-test

In the ncbirths dataset, test if there is a significant difference in length of pregnancy between smokers and non-smokers.

  1. Write hypotheses
    #HO: mu1 = mu2 #HA: mu1 =/= mu2
  2. Test by confidence interval or p-value and decision
smokers <- subset(ncbirths, habit == "smoker" )
nonsmokers <- subset(ncbirths, habit == "nonsmoker" )

t.test(smokers$weeks, nonsmokers$weeks)$p.value
## [1] 0.6043917
  1. Conclusion
    #we reject the null hypothesis because there is significant data to disprove it. the p-value is above the alpha of 0.05.

Question 6

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.

  1. Write hypotheses
    #HO: mu1 = mu2 #HA: mu1 =/= mu2
  2. Test by confidence interval or p-value and decision
younger <- subset(ncbirths, mature == "younger mom" )
mature <- subset(ncbirths, mature == "mature mom" )

t.test(younger$gained, mature$gained)$p.value
## [1] 0.1704274
  1. Conclusion
    #due to the p-value being above 0.05, we accept the

Question 7

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.

Question 8

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.

  1. Question
    #are premie babies lighter than full term babies?
  2. Write hypotheses
    #HO: mu1 = mu2 #HA: mu1 < mu2
  3. Test by confidence interval or p-value and decision
premie <- subset(ncbirths, premie == "premie")
fultrm <- subset(ncbirths, premie == "full term")

t.test(premie$weight, fultrm$weight)$p.value
## [1] 1.358422e-30
  1. Conclusion
    #based on the p-value being so low, there is not enough data to reject the null hypothesis, therefore the mean weight of premie babies is the same as full term babies.