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
── 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
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.
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.
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.
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
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.
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.
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))