#Research Question: How do developmental disability support expenditures differ among ethnic groups?
#Dataset: https://www.openintro.org/data/index.php?data=dds.discr

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.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
dds_data <- read.csv("dds.discr.csv")
#Examining Dataset

dim(dds_data)
## [1] 1000    6
head(dds_data)
##      id age.cohort age gender expenditures          ethnicity
## 1 10210      13-17  17 Female         2113 White not Hispanic
## 2 10409      22-50  37   Male        41924 White not Hispanic
## 3 10486        0-5   3   Male         1454           Hispanic
## 4 10538      18-21  19 Female         6400           Hispanic
## 5 10568      13-17  13   Male         4412 White not Hispanic
## 6 10690      13-17  15 Female         4566           Hispanic
str(dds_data)
## 'data.frame':    1000 obs. of  6 variables:
##  $ id          : int  10210 10409 10486 10538 10568 10690 10711 10778 10820 10823 ...
##  $ age.cohort  : chr  "13-17" "22-50" "0-5" "18-21" ...
##  $ age         : int  17 37 3 19 13 15 13 17 14 13 ...
##  $ gender      : chr  "Female" "Male" "Male" "Female" ...
##  $ expenditures: int  2113 41924 1454 6400 4412 4566 3915 3873 5021 2887 ...
##  $ ethnicity   : chr  "White not Hispanic" "White not Hispanic" "Hispanic" "Hispanic" ...
#Dataset contains 1,000 observations and 6 variables. Each row represents one individual receiving developmental disability services,including their age, gender, expenditures, and ethnicity. 
#Cleaning Dataset
dds_clean <- dds_data  |>
  select(ethnicity, expenditures) |>
  filter(!is.na(ethnicity), !is.na(expenditures)) |>
  mutate(ethnicity = as.factor(ethnicity))

#Data now includes variables of interest and removes missing observations. 


dds_clean |>
  group_by(ethnicity) |>
  summarise(
    count = n(),
    mean_expenditures = mean(expenditures)
  )
## # A tibble: 8 × 3
##   ethnicity          count mean_expenditures
##   <fct>              <int>             <dbl>
## 1 American Indian        4            36438.
## 2 Asian                129            18392.
## 3 Black                 59            20885.
## 4 Hispanic             376            11066.
## 5 Multi Race            26             4457.
## 6 Native Hawaiian        3            42782.
## 7 Other                  2             3316.
## 8 White not Hispanic   401            24698.
#Summary of ethinicity counts and mean expenditures. Mean expenditures differ widely across ethnic groups, where White not Hispanic contain the highest observation count and Native Hawaiian having the lowest observation count of the listed ethnicities. Notably, several ethnicites have low observation counts but high mean expenditures, such as American Indian. 
#ANOVA

anova <- aov(expenditures ~ ethnicity, data = dds_clean)
summary(anova)
##              Df    Sum Sq   Mean Sq F value Pr(>F)    
## ethnicity     7 4.498e+10 6.425e+09   18.94 <2e-16 ***
## Residuals   992 3.366e+11 3.393e+08                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#The demonstrated p-value is far less than the significanc level of 0.05 and is thus statistically significant, rejecting the null hypothesis. 
#Tukey HSD
tukey <- TukeyHSD(anova) |>
  sort( decreasing = TRUE)

#Ordering our Tukey HSD results by descending difference order
tukey_df <- as.data.frame(tukey$"ethnicity")
sorted_tukey <- tukey_df |>
  arrange(desc(diff))

sorted_tukey
##                                          diff         lwr       upr
## Native Hawaiian-Multi Race          38325.603   4211.8443 72439.361
## Native Hawaiian-Hispanic            31716.764   -712.9354 64146.464
## Native Hawaiian-Asian               24389.961  -8284.5690 57064.492
## Native Hawaiian-Black               21897.740 -11214.3871 55009.867
## White not Hispanic-Other            21381.049 -18278.0837 61040.181
## White not Hispanic-Multi Race       20240.818   8918.5625 31563.073
## White not Hispanic-Hispanic         13631.979   9615.7153 17648.244
## Native Hawaiian-American Indian      6344.083 -36386.2486 49074.415
## White not Hispanic-Asian             6305.177    642.1478 11968.205
## White not Hispanic-Black             3812.955  -3988.1940 11614.105
## Black-Asian                          2492.221  -6300.7558 11285.198
## Other-Multi Race                    -1140.231 -42194.2058 39913.744
## Multi Race-Hispanic                 -6608.838 -17953.9899  4736.313
## Hispanic-Asian                      -7326.803 -13035.4709 -1618.135
## Other-Hispanic                      -7749.069 -47414.7442 31916.606
## Hispanic-Black                      -9819.024 -17653.3669 -1984.681
## White not Hispanic-American Indian -11740.701 -39853.4429 16372.040
## Multi Race-Asian                   -13935.641 -25962.7764 -1908.506
## Other-Asian                        -15075.872 -54941.9641 24790.220
## Black-American Indian              -15553.657 -44459.9327 13352.619
## Multi Race-Black                   -16427.862 -29597.5252 -3258.200
## Other-Black                        -17568.093 -57793.6240 22657.438
## Asian-American Indian              -18045.878 -46449.8345 10358.079
## White not Hispanic-Native Hawaiian -18084.785 -50506.4814 14336.912
## Hispanic-American Indian           -25372.681 -53494.6515  2749.290
## Multi Race-American Indian         -31981.519 -62029.9521 -1933.086
## Other-American Indian              -33121.750 -81573.3922 15329.892
## Other-Native Hawaiian              -39465.833 -90538.3486 11606.682
##                                           p adj
## Native Hawaiian-Multi Race         1.538550e-02
## Native Hawaiian-Hispanic           6.054017e-02
## Native Hawaiian-Asian              3.128885e-01
## Native Hawaiian-Black              4.764124e-01
## White not Hispanic-Other           7.273480e-01
## White not Hispanic-Multi Race      1.967605e-06
## White not Hispanic-Hispanic        3.179679e-13
## Native Hawaiian-American Indian    9.998302e-01
## White not Hispanic-Asian           1.704754e-02
## White not Hispanic-Black           8.157783e-01
## Black-Asian                        9.892957e-01
## Other-Multi Race                   1.000000e+00
## Multi Race-Hispanic                6.411119e-01
## Hispanic-Asian                     2.610206e-03
## Other-Hispanic                     9.989583e-01
## Hispanic-Black                     3.719715e-03
## White not Hispanic-American Indian 9.102784e-01
## Multi Race-Asian                   1.065510e-02
## Other-Asian                        9.457380e-01
## Black-American Indian              7.293359e-01
## Multi Race-Black                   3.984018e-03
## Other-Black                        8.887596e-01
## Asian-American Indian              5.306078e-01
## White not Hispanic-Native Hawaiian 6.910968e-01
## Hispanic-American Indian           1.118734e-01
## Multi Race-American Indian         2.764856e-02
## Other-American Indian              4.310424e-01
## Other-Native Hawaiian              2.691726e-01
 #Comparison of means across all ethnicities in descending order. Significant differences were found between several groups, specifically Native Hawaiian, White not Hispanic, Asian, Multi Race, and Hispanic ethnicities.
#Boxplot visualization
ggplot(dds_clean, aes(x = ethnicity, y = expenditures)) +
  geom_boxplot() +
  labs(
    title = "Annual Expenditures by Ethnicity",
    x = "Ethnicity",
    y = "Annual Expenditures ($)"
  )

#The boxplot shows visible differences in annual expenditures across ethnic groups. White not Hispanic generally has higher expenditures, while Hispanic and Multi Race tend to have lower expenditures. The Hispanic group also contains many higher value outliers. Notably, American Indian and Native Hawaiian groups have a higher annual expenditure than the others, although the lower observation count must be considered when conducting further interpretation.