Data110_Fernandez_Maira_Project1

Author

Maira Fernandez

Causes of Death

According to the department of Health and Human Services, it has been observed a potentially excess of deaths from the five leading causes of death in the Non metropolitan areas of the United States. The data set shows also, information about the differences between Observed and Expected deaths reported by state. The centers for Disease Control and Prevention argues that many potentially excess of deaths can be prevented improving public health programs. They gathered the mortality data from the National Vital Statistics System. Defined the cause of death based on the International Classification of Diseases. The locality is based on the Office of Management and Budget from 2013. The Potentially excess of death for each state is calculated subtracting deaths at the benchmark rates from observed deaths. Finally all the information and the sources used to make this analysis can be found at this link: https://catalog.data.gov/dataset/nchs-potentially-excess-deaths-from-the-five-leading-causes-of-death

#I loaded these four libraries for the project

library(dslabs)
library(ggplot2)
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(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.2     ✔ tidyr     1.3.2
✔ readr     2.2.0     
── 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

#I upload the data set coping the link of the data from the file explorer in my disc C

data_cancer<- read.csv("C:/Users/MFERN123/DATA_110/export.csv") 
head(data_cancer)
  Year Cause.of.Death   State State.FIPS.Code HHS.Region Age.Range  Benchmark
1 2005         Cancer Alabama              AL          4      0-49 2005 Fixed
2 2005         Cancer Alabama              AL          4      0-49 2005 Fixed
3 2005         Cancer Alabama              AL          4      0-49 2005 Fixed
4 2005         Cancer Alabama              AL          4      0-49 2010 Fixed
5 2005         Cancer Alabama              AL          4      0-49 2010 Fixed
6 2005         Cancer Alabama              AL          4      0-49 2010 Fixed
         Locality Observed.Deaths Population Expected.Deaths
1             All             756  3,148,377             451
2    Metropolitan             556  2,379,871             341
3 Nonmetropolitan             200    768,506             111
4             All             756  3,148,377             421
5    Metropolitan             556  2,379,871             318
6 Nonmetropolitan             200    768,506             103
  Potentially.Excess.Deaths Percent.Potentially.Excess.Deaths
1                       305                             40.3%
2                       217                               39%
3                        89                             44.5%
4                       335                             44.3%
5                       238                             42.8%
6                        97                             48.5%

#Exploration

I did the exploration of the data set and since we will need to type the names of the variables, I decided to run “names” to see if how they are written. So that I will do not make mistakes with Upercase or Lowercase during the coding part.

names(data_cancer)
 [1] "Year"                              "Cause.of.Death"                   
 [3] "State"                             "State.FIPS.Code"                  
 [5] "HHS.Region"                        "Age.Range"                        
 [7] "Benchmark"                         "Locality"                         
 [9] "Observed.Deaths"                   "Population"                       
[11] "Expected.Deaths"                   "Potentially.Excess.Deaths"        
[13] "Percent.Potentially.Excess.Deaths"

#Exploration

Then I run “dim” to see the size of the data frame and “glimpse” to see if the variables were categorical or numerical

dim(data_cancer)
[1] 205920     13

#Exploration

After running “glimpse” I realized that many numerical variables like Population, Pottentially Excess of Deaths, Observed Deaths, and Percentage were categorical in this data set.

glimpse(data_cancer)
Rows: 205,920
Columns: 13
$ Year                              <int> 2005, 2005, 2005, 2005, 2005, 2005, …
$ Cause.of.Death                    <chr> "Cancer", "Cancer", "Cancer", "Cance…
$ State                             <chr> "Alabama", "Alabama", "Alabama", "Al…
$ State.FIPS.Code                   <chr> "AL", "AL", "AL", "AL", "AL", "AL", …
$ HHS.Region                        <int> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
$ Age.Range                         <chr> "0-49", "0-49", "0-49", "0-49", "0-4…
$ Benchmark                         <chr> "2005 Fixed", "2005 Fixed", "2005 Fi…
$ Locality                          <chr> "All", "Metropolitan", "Nonmetropoli…
$ Observed.Deaths                   <chr> "756", "556", "200", "756", "556", "…
$ Population                        <chr> "3,148,377", "2,379,871", "768,506",…
$ Expected.Deaths                   <chr> "451", "341", "111", "421", "318", "…
$ Potentially.Excess.Deaths         <chr> "305", "217", "89", "335", "238", "9…
$ Percent.Potentially.Excess.Deaths <chr> "40.3%", "39%", "44.5%", "44.3%", "4…

To preserve the original data set , I created a new table, selecting just the variables that I wanted to use with function “select”.

new_table1 <- select(data_cancer, State, Cause.of.Death,Observed.Deaths, Expected.Deaths, Percent.Potentially.Excess.Deaths )
head(new_table1)
    State Cause.of.Death Observed.Deaths Expected.Deaths
1 Alabama         Cancer             756             451
2 Alabama         Cancer             556             341
3 Alabama         Cancer             200             111
4 Alabama         Cancer             756             421
5 Alabama         Cancer             556             318
6 Alabama         Cancer             200             103
  Percent.Potentially.Excess.Deaths
1                             40.3%
2                               39%
3                             44.5%
4                             44.3%
5                             42.8%
6                             48.5%

Then I cleaned this data, mutating into a new table to convert the categorical variables Observed Deaths and Expected Deaths into numerical variables.

new_table1 <- new_table1 |> 
  mutate(Observed.Deaths = as.numeric(Observed.Deaths), 
         Expected.Deaths = as.numeric(Expected.Deaths)) 
Warning: There were 2 warnings in `mutate()`.
The first warning was:
ℹ In argument: `Observed.Deaths = as.numeric(Observed.Deaths)`.
Caused by warning:
! NAs introduced by coercion
ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
head(new_table1)
    State Cause.of.Death Observed.Deaths Expected.Deaths
1 Alabama         Cancer             756             451
2 Alabama         Cancer             556             341
3 Alabama         Cancer             200             111
4 Alabama         Cancer             756             421
5 Alabama         Cancer             556             318
6 Alabama         Cancer             200             103
  Percent.Potentially.Excess.Deaths
1                             40.3%
2                               39%
3                             44.5%
4                             44.3%
5                             42.8%
6                             48.5%

For this chunk I filtered the table by Cause of Death, so I will have only five columns per state. Then I grouped the table by state and cause of death; and I summarized the observed deaths. I called that chunk Cause of Death to create my visualization.

Cause_of_Death <- new_table1 %>% 
  filter(Cause.of.Death %in% c("Cancer","Stroke","Heart Disease","Unintentional Injury","Chronic Lower Respiratory Disease")) %>% 
  group_by(State, Cause.of.Death)%>% 
  summarise(Observed.Deaths = sum(Observed.Deaths,na.rm = TRUE), 
    Expected.Deaths = sum(Expected.Deaths,na.rm = TRUE),.groups = "drop") 
Cause_of_Death
# A tibble: 260 × 4
   State   Cause.of.Death                    Observed.Deaths Expected.Deaths
   <chr>   <chr>                                       <dbl>           <dbl>
 1 Alabama Cancer                                     134022          143772
 2 Alabama Chronic Lower Respiratory Disease          201246          178514
 3 Alabama Heart Disease                              145479          199648
 4 Alabama Stroke                                     244830          168765
 5 Alabama Unintentional Injury                       161040          378761
 6 Alaska  Cancer                                     227376          195802
 7 Alaska  Chronic Lower Respiratory Disease           31863           21313
 8 Alaska  Heart Disease                              155472          115481
 9 Alaska  Stroke                                      27912           19439
10 Alaska  Unintentional Injury                       153426           59255
# ℹ 250 more rows

I created my visualization with the Cause of Death table, I wanted to see the difference between expected deaths and observed deaths by Cause of Deaths. So that, I will have five bars with two colors. For this graph we can see that the observed deaths for the five principal causes of deaths were more than the expected deaths.

ggplot(Cause_of_Death, aes(x = Cause.of.Death)) + 
  geom_col(aes(y = Observed.Deaths, fill = "Observed Deaths"),position = "dodge") + 
  geom_col(aes(y = Expected.Deaths, fill = "Expected Deaths"),position = "dodge") +
  scale_fill_manual(values = c("Observed Deaths"="skyblue","Expected Deaths"= "navy"))+
  labs(title = "Observed vs. Expected Deaths by Cause", 
    x = "Cause of Death", 
    y = "Deaths", 
    fill = "Differences Between", 
    caption = "U.S. Department of Health and Human Services") + 
  theme_gray() + 
  theme(axis.text.x = element_text(angle = 50, hjust = 0.8))

For my second visualization I wanted to use other variables, so I decided to create a table selecting State, Cause of Death, Observed Deaths, and Age Range. I named this table new table 2

new_table2<-select(data_cancer,State,Cause.of.Death,Observed.Deaths,Age.Range)
head(new_table2)
    State Cause.of.Death Observed.Deaths Age.Range
1 Alabama         Cancer             756      0-49
2 Alabama         Cancer             556      0-49
3 Alabama         Cancer             200      0-49
4 Alabama         Cancer             756      0-49
5 Alabama         Cancer             556      0-49
6 Alabama         Cancer             200      0-49

For this next table I change the Observed Deaths that were in a categorical format to numerical variable with the function mutate.

new_table2 <- new_table2 |> 
  mutate(Observed.Deaths = as.numeric(Observed.Deaths))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Observed.Deaths = as.numeric(Observed.Deaths)`.
Caused by warning:
! NAs introduced by coercion
head(new_table2)
    State Cause.of.Death Observed.Deaths Age.Range
1 Alabama         Cancer             756      0-49
2 Alabama         Cancer             556      0-49
3 Alabama         Cancer             200      0-49
4 Alabama         Cancer             756      0-49
5 Alabama         Cancer             556      0-49
6 Alabama         Cancer             200      0-49

I filtered the next table by Cause of Death “Cancer” and Age Range 0 to 49. Then I grouped by State, then I summarize it by numerical variables Observed Deaths with the function “summarise”. However, when I created the bar chart I realized that between the States it was a observation of “United States”. So when I was filtering by Cause of Death and Age Range, I use the function ! to exclude United States.

deaths_by_cancer_by_state_by_0_49<- new_table2 %>% 
  filter(Cause.of.Death=="Cancer",Age.Range=="0-49",State!="United States") %>% 
  group_by(State)%>% 
  summarise(Observed.Deaths = sum(Observed.Deaths,na.rm= TRUE),.groups = "drop") 
deaths_by_cancer_by_state_by_0_49
# A tibble: 51 × 2
   State                   Observed.Deaths
   <chr>                             <dbl>
 1 "Alabama"                         44208
 2 "Alaska"                           4836
 3 "Arizona"                         41598
 4 "Arkansas"                        26118
 5 "California"                       2802
 6 "Colorado"                        30264
 7 "Connecticut"                     23910
 8 "Delaware"                         7182
 9 "District of\nColumbia"            5118
10 "Florida"                          3486
# ℹ 41 more rows

Finally, I created a bar chart visualization that shows the observed deaths from Cancer by state for people between 0 to 49 years old.

ggplot(deaths_by_cancer_by_state_by_0_49, aes(x =State)) + 
  geom_col(aes(y = Observed.Deaths,fill="State"),position = "dodge")+
labs(title = "Observed Deaths by Cancer in people from 0 to 49 years old by State", 
       x = "State", 
       y = "Observed Deaths",
     fill="States",
       caption = "U.S. Department of Health and Human Services")+ scale_fill_viridis_d()+
theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust = 0.5))