library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ 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
install.packages("pastecs")
## Installing package into 'C:/Users/Campo/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'pastecs' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Campo\AppData\Local\Temp\RtmpAbdXxQ\downloaded_packages
library(pastecs)
## 
## Attaching package: 'pastecs'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
suicide_data <- read_csv("C:/Users/Campo/Downloads/Death_rates_for_suicide__by_sex__race__Hispanic_origin__and_age__United_States.csv")
## Rows: 6390 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): INDICATOR, UNIT, STUB_NAME, STUB_LABEL, AGE, FLAG
## dbl (7): UNIT_NUM, STUB_NAME_NUM, STUB_LABEL_NUM, YEAR, YEAR_NUM, AGE_NUM, E...
## 
## ℹ 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.
print(head(suicide_data))
## # A tibble: 6 × 13
##   INDICATOR     UNIT  UNIT_NUM STUB_NAME STUB_NAME_NUM STUB_LABEL STUB_LABEL_NUM
##   <chr>         <chr>    <dbl> <chr>             <dbl> <chr>               <dbl>
## 1 Death rates … Deat…        1 Total                 0 All perso…              0
## 2 Death rates … Deat…        1 Total                 0 All perso…              0
## 3 Death rates … Deat…        1 Total                 0 All perso…              0
## 4 Death rates … Deat…        1 Total                 0 All perso…              0
## 5 Death rates … Deat…        1 Total                 0 All perso…              0
## 6 Death rates … Deat…        1 Total                 0 All perso…              0
## # ℹ 6 more variables: YEAR <dbl>, YEAR_NUM <dbl>, AGE <chr>, AGE_NUM <dbl>,
## #   ESTIMATE <dbl>, FLAG <chr>
print(colnames(suicide_data))
##  [1] "INDICATOR"      "UNIT"           "UNIT_NUM"       "STUB_NAME"     
##  [5] "STUB_NAME_NUM"  "STUB_LABEL"     "STUB_LABEL_NUM" "YEAR"          
##  [9] "YEAR_NUM"       "AGE"            "AGE_NUM"        "ESTIMATE"      
## [13] "FLAG"
estimate_desc <- pastecs::stat.desc(suicide_data$ESTIMATE)

print(estimate_desc)
##      nbr.val     nbr.null       nbr.na          min          max        range 
## 5.484000e+03 0.000000e+00 9.060000e+02 3.000000e-01 7.480000e+01 7.450000e+01 
##          sum       median         mean      SE.mean CI.mean.0.95          var 
## 7.518460e+04 1.050000e+01 1.370981e+01 1.557215e-01 3.052759e-01 1.329825e+02 
##      std.dev     coef.var 
## 1.153181e+01 8.411353e-01
suicide_data_clean <- suicide_data %>%
  filter(!is.na(ESTIMATE))

install.packages("dplyr")
## Warning: package 'dplyr' is in use and will not be installed
library(dplyr)

suicide_data_clean <- suicide_data %>%
  filter(!is.na(ESTIMATE))

install.packages("ggplot2")
## Warning: package 'ggplot2' is in use and will not be installed
library(ggplot2)

ggplot(suicide_data_clean, aes(x = ESTIMATE)) +
  geom_histogram(binwidth = 1, fill = "blue", color = "black") +
  labs(
    title = "Histogram of Suicide Death Rates",
    x = "Death Rate (Estimate)",
    y = "Frequency"
  ) +
  theme_minimal()

suicide_data_clean <- suicide_data_clean %>%
  mutate(log_estimate = log(ESTIMATE + 1))

ggplot(suicide_data_clean, aes(x = log_estimate)) +
  geom_histogram(binwidth = 0.1, fill = "green", color = "black") +
  labs(
    title = "Histogram of Log-Transformed Suicide Death Rates",
    x = "Log(Death Rate (Estimate))",
    y = "Frequency"
  ) +
  theme_minimal()