library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.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
cheese <- read_csv(url("http://jamessuleiman.com/teaching/datasets/cheese.csv"))
## Rows: 24 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (9): year, cheddar, mozzarella, swiss, blue, brick, muenster, neufchatel...
## 
## ℹ 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.
glimpse(cheese)
## Rows: 24
## Columns: 9
## $ year       <dbl> 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,…
## $ cheddar    <dbl> 9.04, 9.19, 9.51, 9.60, 10.01, 9.87, 9.89, 9.76, 9.38, 10.2…
## $ mozzarella <dbl> 7.89, 8.22, 8.16, 8.33, 8.74, 9.05, 9.35, 9.38, 9.45, 9.68,…
## $ swiss      <dbl> 1.09, 1.07, 0.99, 1.01, 1.09, 1.02, 1.12, 1.09, 1.13, 1.20,…
## $ blue       <dbl> 0.16, 0.17, 0.18, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ brick      <dbl> 0.04, 0.04, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03,…
## $ muenster   <dbl> 0.41, 0.39, 0.37, 0.34, 0.28, 0.30, 0.28, 0.28, 0.27, 0.25,…
## $ neufchatel <dbl> 2.04, 2.11, 2.25, 2.20, 2.26, 2.39, 2.21, 2.33, 2.30, 2.34,…
## $ hispanic   <dbl> NA, 0.25, 0.25, 0.27, 0.30, 0.33, 0.37, 0.42, 0.45, 0.48, 0…
deaths <- read_csv(url("http://jamessuleiman.com/teaching/datasets/Injury_Mortality__United_States.csv"))
## Rows: 98280 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): Sex, Age group (years), Race, Injury mechanism, Injury intent, Unit
## dbl (9): Year, Age Specific Rate, Age Specific Rate Standard Error, Age Spec...
## num (2): Deaths, Population
## 
## ℹ 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.
glimpse(deaths)
## Rows: 98,280
## Columns: 17
## $ Year                                       <dbl> 2016, 2015, 2014, 2013, 201…
## $ Sex                                        <chr> "Both sexes", "Both sexes",…
## $ `Age group (years)`                        <chr> "All Ages", "All Ages", "Al…
## $ Race                                       <chr> "All races", "All races", "…
## $ `Injury mechanism`                         <chr> "All Mechanisms", "All Mech…
## $ `Injury intent`                            <chr> "All Intentions", "All Inte…
## $ Deaths                                     <dbl> 231991, 214008, 199752, 192…
## $ Population                                 <dbl> 323127513, 321418820, 31885…
## $ `Age Specific Rate`                        <dbl> 71.795496, 66.582287, 62.64…
## $ `Age Specific Rate Standard Error`         <dbl> 0.1490602, 0.1439275, 0.140…
## $ `Age Specific Rate Lower Confidence Limit` <dbl> 71.503338, 66.300189, 62.37…
## $ `Age Specific Rate Upper Confidence Limit` <dbl> 72.087654, 66.864384, 62.92…
## $ `Age Adjusted Rate`                        <dbl> 68.98224, 63.86611, 60.1272…
## $ `Age Adjusted Rate Standard Error`         <dbl> 0.1460097, 0.1405070, 0.136…
## $ `Age Adjusted Rate Lower Confidence Limit` <dbl> 68.69606, 63.59072, 59.8592…
## $ `Age Adjusted Rate Upper Confidence Limit` <dbl> 69.26842, 64.14151, 60.3952…
## $ Unit                                       <chr> "per 100,000 population", "…
deaths_2 <- deaths %>% filter(`Injury mechanism` != "All Mechanisms" & 
                    `Age group (years)` == "All Ages" &
                    Race == "All races" &
                    `Injury intent` == "All Intentions" &
                    Sex == "Both sexes") %>%
  select(Year, `Injury mechanism`, Deaths) %>%
  rename(year = Year, death_type = `Injury mechanism`, death_count = Deaths) %>%
  pivot_wider(names_from = death_type, values_from = death_count)
death_by_cheese <- cheese %>% 
  select(year, muenster) %>% 
  inner_join(deaths_2, by = "year")
death_by_cheese %>% select(-year) %>% cor()
##                                 muenster  Cut/pierce    Drowning       Fall
## muenster                      1.00000000 -0.04990773  0.51393834  0.9356425
## Cut/pierce                   -0.04990773  1.00000000  0.17063452  0.2206411
## Drowning                      0.51393834  0.17063452  1.00000000  0.5567061
## Fall                          0.93564250  0.22064112  0.55670608  1.0000000
## Fire/hot object or substance -0.83707186 -0.09090047 -0.53402721 -0.8667681
## Firearm                       0.90453644  0.22761514  0.64881514  0.9257407
## Motor vehicle traffic        -0.78593273  0.22880203 -0.36438044 -0.7506909
## All Other Transport          -0.50103136 -0.31775277 -0.44540194 -0.6067667
## Poisoning                     0.90497837  0.30911141  0.63334924  0.9791790
## Suffocation                   0.93041389  0.17177657  0.54091944  0.9938990
## All Other Specified          -0.37671471 -0.16056723 -0.01576699 -0.4647084
## Unspecified                  -0.30377829 -0.14550347  0.05042755 -0.3734979
##                              Fire/hot object or substance    Firearm
## muenster                                      -0.83707186  0.9045364
## Cut/pierce                                    -0.09090047  0.2276151
## Drowning                                      -0.53402721  0.6488151
## Fall                                          -0.86676812  0.9257407
## Fire/hot object or substance                   1.00000000 -0.7172177
## Firearm                                       -0.71721766  1.0000000
## Motor vehicle traffic                          0.88872560 -0.5418749
## All Other Transport                            0.57250666 -0.4904084
## Poisoning                                     -0.78997216  0.9589757
## Suffocation                                   -0.86387501  0.9113808
## All Other Specified                            0.52456683 -0.3167888
## Unspecified                                    0.52310835 -0.1104524
##                              Motor vehicle traffic All Other Transport
## muenster                                -0.7859327          -0.5010314
## Cut/pierce                               0.2288020          -0.3177528
## Drowning                                -0.3643804          -0.4454019
## Fall                                    -0.7506909          -0.6067667
## Fire/hot object or substance             0.8887256           0.5725067
## Firearm                                 -0.5418749          -0.4904084
## Motor vehicle traffic                    1.0000000           0.4476599
## All Other Transport                      0.4476599           1.0000000
## Poisoning                               -0.6450346          -0.5999390
## Suffocation                             -0.7582158          -0.5886872
## All Other Specified                      0.4785087           0.3578379
## Unspecified                              0.5946724           0.5042059
##                               Poisoning Suffocation All Other Specified
## muenster                      0.9049784   0.9304139         -0.37671471
## Cut/pierce                    0.3091114   0.1717766         -0.16056723
## Drowning                      0.6333492   0.5409194         -0.01576699
## Fall                          0.9791790   0.9938990         -0.46470837
## Fire/hot object or substance -0.7899722  -0.8638750          0.52456683
## Firearm                       0.9589757   0.9113808         -0.31678878
## Motor vehicle traffic        -0.6450346  -0.7582158          0.47850867
## All Other Transport          -0.5999390  -0.5886872          0.35783788
## Poisoning                     1.0000000   0.9654581         -0.40415779
## Suffocation                   0.9654581   1.0000000         -0.47571858
## All Other Specified          -0.4041578  -0.4757186          1.00000000
## Unspecified                  -0.2982013  -0.3468012          0.52552447
##                              Unspecified
## muenster                     -0.30377829
## Cut/pierce                   -0.14550347
## Drowning                      0.05042755
## Fall                         -0.37349794
## Fire/hot object or substance  0.52310835
## Firearm                      -0.11045238
## Motor vehicle traffic         0.59467240
## All Other Transport           0.50420589
## Poisoning                    -0.29820134
## Suffocation                  -0.34680116
## All Other Specified           0.52552447
## Unspecified                   1.00000000
death_by_cheese %>%
  ggplot(aes(x = Poisoning, y = muenster)) +
  geom_point() +
  xlab("United States annual deaths caused by poisoning") +
  ylab("per capita muenster consumption in pounds") +
  theme_minimal()

death_by_cheese %>%
  ggplot(aes(x = Poisoning, y = muenster)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  xlab("United States annual deaths caused by poisoning") +
  ylab("per capita muenster consumption in pounds") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

death_by_cheese %>%
  ggplot(aes(x = Poisoning, y = muenster)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  xlab("United States annual deaths caused by poisoning") +
  ylab("per capita muenster consumption in pounds") +
  ggtitle("Muenster is poison to the body!") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'