Import dataset

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)
setwd("C:/PMS/Data_Science/Data101/Project_2")

Description: Main reason I picked the diabetes data set is to compare the data between two scenarios i.e hypothesis is Null or hypothesis is not Null. With this dataset, I can compare two groups between men and women with independent observations, using an Independent Two-Sample paired t-test

# Read data
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── 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
data <- read_csv("diabetes.prev.csv")
## Rows: 3143 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): State, County
## dbl (12): FIPS.Codes, num.men.diabetes, percent.men.diabetes, num.women.diab...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Compare male vs female diabetes percentages across counties 

male <- as.numeric(data$percent.men.diabetes)
female <- as.numeric(data$percent.women.diabetes)                     

# Remove missing values pairwise -  Cleaning dataset and conduct exploratory data analysis to better understand the data 
df <- na.omit(data.frame(male, female))

# Independent T- test  
result <- t.test(
  df$male,
  df$female,
  paired = TRUE,
  alternative = "two.sided",
  conf.level = 0.95)

print(result)
## 
##  Paired t-test
## 
## data:  df$male and df$female
## t = 52.47, df = 3142, p-value < 2.2e-16
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  0.9084511 0.9789813
## sample estimates:
## mean difference 
##       0.9437162

Code to satisfy the requirement of using dplyr functions (select(), filter(), mutate(), summarise(), mean(), max())

# Read data4df <- read.csv("diabetes.prev - Copy.csv")
#Data preparation and manipulation
df_analysis <- data %>%
# 1. Select variables needed for analysis
select(
    State,
    County,
    percent.men.diabetes,
    percent.women.diabetes,
    percent.men.obese,
    percent.women.obese
  ) %>%
# 2. Filter missing observations
filter(
    !is.na(percent.men.diabetes),
    !is.na(percent.women.diabetes)  ) %>%
# 3. Create new variables
mutate(
    diabetes_gap = percent.men.diabetes - percent.women.diabetes,
    obesity_gap  = percent.men.obese - percent.women.obese
  )
# Preview cleaned dataset
head(df_analysis)
## # A tibble: 6 × 8
##   State   County   percent.men.diabetes percent.women.diabetes percent.men.obese
##   <chr>   <chr>                   <dbl>                  <dbl>             <dbl>
## 1 Alabama Autauga…                 12.1                   11.6              31.3
## 2 Alabama Baldwin…                 12.4                   11.3              29  
## 3 Alabama Barbour…                 12.9                   15.7              37.7
## 4 Alabama Bibb Co…                 11                     11.3              40.2
## 5 Alabama Blount …                 14                     13.9              33.5
## 6 Alabama Bullock…                 15.3                   20.2              39.9
## # ℹ 3 more variables: percent.women.obese <dbl>, diabetes_gap <dbl>,
## #   obesity_gap <dbl>
# 4. Summary statistics using summarise(), mean(), and max()
summary_results <- df_analysis %>%
  summarise(
    mean_men_diabetes   = mean(percent.men.diabetes),
    mean_women_diabetes = mean(percent.women.diabetes),
    max_men_diabetes    = max(percent.men.diabetes),
    max_women_diabetes  = max(percent.women.diabetes),
    mean_diabetes_gap   = mean(diabetes_gap)
  )
print(summary_results)
## # A tibble: 1 × 5
##   mean_men_diabetes mean_women_diabetes max_men_diabetes max_women_diabetes
##               <dbl>               <dbl>            <dbl>              <dbl>
## 1              11.2                10.2             17.7               21.1
## # ℹ 1 more variable: mean_diabetes_gap <dbl>

Create equal variance Assumption. H₀: μ₁ = μ₂ (means are equal) H₁: μ₁ ≠ μ₂ (means are different)

# Equal Variance Assumption
t.test(male, female,
       var.equal = TRUE,
       alternative = "two.sided",
       conf.level = 0.95)
## 
##  Two Sample t-test
## 
## data:  male and female
## t = 16.197, df = 6284, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  0.8294995 1.0579329
## sample estimates:
## mean of x mean of y 
##  11.18775  10.24403
# Paired t-Test - read data
data <- read_csv("diabetes.prev.csv")
## Rows: 3143 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): State, County
## dbl (12): FIPS.Codes, num.men.diabetes, percent.men.diabetes, num.women.diab...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
t.test(data$percent.men.diabetes,
       data$percent.women.diabetes,
       paried = TRUE,
       alternative ="two.sided",
       conf.level = 0.95)
## 
##  Welch Two Sample t-test
## 
## data:  data$percent.men.diabetes and data$percent.women.diabetes
## t = 16.197, df = 6166.6, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  0.8294991 1.0579333
## sample estimates:
## mean of x mean of y 
##  11.18775  10.24403

H₀: Mean difference = 0 , H₁: Mean difference ≠ 0 = Decision rule

result <- t.test(data$percent.men.diabetes, 
                  data$percent.women.diabetes,
                  paired = TRUE)
                  
print(result)
## 
##  Paired t-test
## 
## data:  data$percent.men.diabetes and data$percent.women.diabetes
## t = 52.47, df = 3142, p-value < 2.2e-16
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  0.9084511 0.9789813
## sample estimates:
## mean difference 
##       0.9437162
if(result$p.value < 0.05){
cat("Reject H0: Significant difference in means\n")
} else {
  cat("Fail to Reject H0: No significant difference in means\n")
}
## Reject H0: Significant difference in means

Validate Assumption for paired differences

diff <- data$percent.men.diabetes - data$percent.women.diabetes
shapiro.test(diff)
## 
##  Shapiro-Wilk normality test
## 
## data:  diff
## W = 0.93009, p-value < 2.2e-16
hist(diff,
     main = "Distribution of Differences",
     xlab = "Male % - Female %")

Alternative paired if normality is violated

wilcox.test(data$percent.men.diabetes,
            data$percent.women.diabetes,
            paired = TRUE
            )
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  data$percent.men.diabetes and data$percent.women.diabetes
## V = 4368165, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0
# Independent 
wilcox.test(male, female)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  male and female
## W = 6232067, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0

Visualize the test results.

library(ggplot2)
library(tidyverse)

df <- data.frame(
  Group = rep(c("Men", "Women"), each = nrow(data)),
  Diabetes = c(data$percent.men.diabetes,
               data$percent.women.diabetes)
)

ggplot(df, aes(x = Group, y = Diabetes, fill = Group)) +
geom_boxplot(alpha = 0.7)+
  labs(
    title = "Diabetes Prevalance by Gender", 
    Y = "Diabetes Percent"
  ) +
    
  theme_minimal()
## Ignoring unknown labels:
## • Y : "Diabetes Percent"

Conclusion

Upon comparing the dataset male v/s female diabetes occurance within the same county it is usually treated as a paired T-test, since both measurement comes from the same county. Therefore the paired t-test is likely the most appropriate choice.