options(tinytex.verbose = TRUE)
load("more/nc.RData")
numrows<-nrow(nc)Each case (or row) in the dataset offers data on an individual newborn. There are 1000 in the dataset.
habit and weight. What does the plot highlight about the relationship between these two variables?The plot below shows that the mean weight of newborns born to smoking mothers is lower than nonsmoking mothers.
boxplot(data=nc,weight~habit,
main = "weight vs habit",
at = c(1,2),
names = c("nonsmoker","smoker"),
las = 2,
col = c("orange","red"),
border = "brown",
horizontal = TRUE
)by command above but replacing mean with length.by(nc$weight, nc$habit, length)## nc$habit: nonsmoker
## [1] 873
## --------------------------------------------------------
## nc$habit: smoker
## [1] 126
Each group is greater than 30 observations, so the conditions are met for inference.
\(H_0\): no significant difference exists in the mean weights
\(H_A\): a significant difference exists in the mean weights
type argument to "ci" to construct and record a confidence interval for the difference between the weights of babies born to smoking and non-smoking mothers.inference(y = nc$weight, x = nc$habit, est = "mean", type = "ci", null = 0,
alternative = "twosided", method = "theoretical")## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_nonsmoker = 873, mean_nonsmoker = 7.1443, sd_nonsmoker = 1.5187
## n_smoker = 126, mean_smoker = 6.8287, sd_smoker = 1.3862
## Observed difference between means (nonsmoker-smoker) = 0.3155
##
## Standard error = 0.1338
## 95 % Confidence interval = ( 0.0534 , 0.5777 )
Calculate a 95% confidence interval for the average length of pregnancies (weeks) and interpret it in context. Note that since you’re doing inference on a single population parameter, there is no explanatory variable, so you can omit the x variable from the function.
inference(y = nc$weeks, est = "mean", type = "ci", null = 0,
alternative = "twosided", method = "theoretical")## Single mean
## Summary statistics:
## mean = 38.3347 ; sd = 2.9316 ; n = 998
## Standard error = 0.0928
## 95 % Confidence interval = ( 38.1528 , 38.5165 )
We can affirm that 95% of random samples would indicate a mean gestation period between 38.1528 and 38.5165 weeks.
Calculate a new confidence interval for the same parameter at the 90% confidence level. You can change the confidence level by adding a new argument to the function: conflevel = 0.90.
inference(y = nc$weeks, est = "mean", type = "ci", null = 0,
alternative = "twosided", method = "theoretical", conflevel = 0.90)## Single mean
## Summary statistics:
## mean = 38.3347 ; sd = 2.9316 ; n = 998
## Standard error = 0.0928
## 90 % Confidence interval = ( 38.182 , 38.4873 )
We can affirm that 90% of random samples would indicate a mean gestation period between 38.182 and 38.4873 weeks.
Conduct a hypothesis test evaluating whether the average weight gained by younger mothers is different than the average weight gained by mature mothers.
\(H_0\): no significant difference exists in the mean weight gains
\(H_A\): a significant difference exists in the mean weight gains
inference(y = nc$gained, x = nc$mature, est = "mean", type = "ci", null = 0,
alternative = "twosided", method = "theoretical")## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_mature mom = 129, mean_mature mom = 28.7907, sd_mature mom = 13.4824
## n_younger mom = 844, mean_younger mom = 30.5604, sd_younger mom = 14.3469
## Observed difference between means (mature mom-younger mom) = -1.7697
##
## Standard error = 1.2857
## 95 % Confidence interval = ( -4.2896 , 0.7502 )
The resulting confidence interval is (-4.2896, 0.7502), meaning that 95% of random samples will have as a statistical characteristic a mean group difference between -4.2896 and 0.7502; because the interval includes zero, we accept the null hypothesis and reject the alternative, and assert that there is no significant difference in the mean weight gain between the two groups.
Now, a non-inference task: Determine the age cutoff for younger and mature mothers. Use a method of your choice, and explain how your method works.
Let’s use the command introduced earlier in this lab.
by(nc$mage, nc$mature, max)## nc$mature: mature mom
## [1] 50
## --------------------------------------------------------
## nc$mature: younger mom
## [1] 34
The maximum age of “younger” moms is 34; the maximum age of “mature” moms is 50; this implies younger moms are 34 and younger, while mature moms are 35 and older.
Pick a pair of numerical and categorical variables 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. Answer your question using the inference function, report the statistical results, and also provide an explanation in plain language.
Is there a significant relationship between weight gain and birth sex?
\(H_0\): no significant difference exists in the mean weight gain
\(H_A\): a significant difference exists in the mean weight gain
inference(y = nc$gained, x = nc$gender, est = "mean", type = "ci", null = 0,
alternative = "twosided", method = "theoretical")## Response variable: numerical, Explanatory variable: categorical
## Difference between two means
## Summary statistics:
## n_female = 488, mean_female = 29.8135, sd_female = 14.2506
## n_male = 485, mean_male = 30.8412, sd_male = 14.228
## Observed difference between means (female-male) = -1.0277
##
## Standard error = 0.913
## 95 % Confidence interval = ( -2.8171 , 0.7617 )
The resulting confidence interval is (-2.8171, 0.7617), meaning that 95% of random samples will have as a statistical characteristic a mean group difference between -2.8171 and 0.7617; because the interval includes zero, we accept the null hypothesis and reject the alternative, and assert that there is no significant difference in the mean weight gain between the two groups.