This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.0 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.2 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
data(ncbirths)
births_clean <- ncbirths %>%
select(habit, lowbirthweight)
births_clean <- births_clean %>%
filter(!is.na(habit), !is.na(lowbirthweight))
birth_table <- births_clean %>%
group_by(habit, lowbirthweight) %>%
summarise(count = n(), .groups = "drop")
birth_table_matrix <- table(births_clean$habit, births_clean$lowbirthweight)
birth_table_matrix
##
## low not low
## nonsmoker 92 781
## smoker 18 108
##Null Hypothesis = no association between a mother that smokes vs a baby’s low weight at birth
##Alternative hypothesis = There is a connection between a mother that smokes and a baby’s low weight at birth
birth_table_matrix <- table(births_clean$habit,
births_clean$lowbirthweight)
chi_test <- chisq.test(birth_table_matrix)
chi_test$expected
##
## low not low
## nonsmoker 96.12613 776.8739
## smoker 13.87387 112.1261
ggplot(births_clean,
aes(x = habit,
fill = lowbirthweight))+
geom_bar(position = "Dodge")+
labs(title = "Smoking Status and Low Birth Weight",
x = "Smoking Status",
y = "Count",
fill = "Low Birth Weight")
chi_test
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: birth_table_matrix
## X-squared = 1.2187, df = 1, p-value = 0.2696