Loading Libraries
All installed packages were loaded to be used in the R Program.
library(plm)
library(knitr)
library(stringr)
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plm':
##
## between, lag, lead
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
—- Data —-
df <- load('Ozone_Drought_Final.RData')
df2 <- combinedAir.final %>%
mutate(month = as.numeric(month))
df3 <- read.csv("region_code.csv")
df2$State.Code = as.numeric(df2$State.Code)
df4 <- df2 %>%
merge(df3, by = "State.Code")
— Calculating the Statistics (Total Monitor, Mean, SD) —
df5 <- df4 %>%
group_by(noaa_region,USDM.categorical) %>%
summarize(data_days = n_distinct(Date..Local.),
ozone_max = mean(Max.Ozone),
sd = sd(Max.Ozone))
## `summarise()` has grouped output by 'noaa_region'. You can override using the
## `.groups` argument.
df6 <- df5 %>%
group_by(noaa_region) %>%
mutate(percentage = (data_days/sum(data_days))*100)
df8 <- df4 %>%
group_by(USDM.categorical) %>%
summarize(data_days = n_distinct(Date..Local.),
ozone_max = mean(Max.Ozone),
sd = sd(Max.Ozone)) %>%
ungroup() %>%
mutate(percentage = (data_days/sum(data_days))*100,
noaa_region = "Overall")
df9 <- df6 %>%
rbind(df8)
df7 <- df9 %>%
pivot_wider(names_from = USDM.categorical, values_from = c(data_days, ozone_max, percentage, sd)) %>%
mutate(noaa_region = str_to_sentence(noaa_region)) %>%
mutate_if(is.numeric, funs(round(., 2)))
## `mutate_if()` ignored the following grouping variables:
## • Column `noaa_region`
## Warning: `funs()` was deprecated in dplyr 0.8.0.
## ℹ Please use a list of either functions or lambdas:
##
## # Simple named list: list(mean = mean, median = median)
##
## # Auto named with `tibble::lst()`: tibble::lst(mean, median)
##
## # Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
— Statistics Table —
# Move No Drought column to first column
df10 <- df7[, 1:4]
df11 <- df10[, c(1, 3, 2, 4)]
# Move the Overall row to the first row
ovr <- df11[df11$noaa_region == "Overall", ]
df12 <- rbind(ovr, df11[df11$noaa_region != "Overall", ])
kable(df12, caption = "Data Days", align = c("l", "c", "c", "c"))
Data Days
Overall |
6935 |
6935 |
6633 |
Northeast |
6935 |
2734 |
423 |
Northern_rockies |
6840 |
6546 |
2997 |
Northwest |
6380 |
4365 |
1786 |
Ohio_valley |
6935 |
4986 |
1418 |
South |
6935 |
6537 |
4488 |
Southeast |
6935 |
4896 |
2984 |
Southwest |
6172 |
6844 |
3864 |
Upper_midwest |
6918 |
4470 |
775 |
West |
5472 |
6100 |
3545 |
# Move No Drought column to first column
df13 <- df7[, c(1,5:7)]
df14 <- df13[, c(1, 3, 2, 4)]
# Move the Overall row to the first row
ovr <- df14[df14$noaa_region == "Overall", ]
df15 <- rbind(ovr, df14[df14$noaa_region != "Overall", ])
kable(df15, caption = "Ozone Max", align = c("l", "c", "c", "c"))
Ozone Max
Overall |
41.53 |
44.03 |
45.99 |
Northeast |
40.27 |
40.69 |
38.84 |
Northern_rockies |
40.45 |
41.71 |
43.53 |
Northwest |
35.59 |
39.12 |
41.76 |
Ohio_valley |
43.45 |
45.42 |
47.38 |
South |
39.51 |
40.97 |
42.37 |
Southeast |
41.12 |
45.10 |
46.90 |
Southwest |
46.40 |
46.33 |
50.41 |
Upper_midwest |
41.72 |
40.97 |
40.68 |
West |
41.86 |
44.91 |
45.18 |
# Move No Drought column to first column
df16 <- df7[, c(1,8:10)]
df17 <- df16[, c(1, 3, 2, 4)]
# Move the Overall row to the first row
ovr <- df17[df17$noaa_region == "Overall", ]
df18 <- rbind(ovr, df17[df17$noaa_region != "Overall", ])
kable(df18, caption = "Percentage Data Days", align = c("l", "c", "c", "c"))
Percentage Data Days
Overall |
33.82 |
33.82 |
32.35 |
Northeast |
68.72 |
27.09 |
4.19 |
Northern_rockies |
41.75 |
39.96 |
18.29 |
Northwest |
50.91 |
34.83 |
14.25 |
Ohio_valley |
51.99 |
37.38 |
10.63 |
South |
38.61 |
36.40 |
24.99 |
Southeast |
46.81 |
33.05 |
20.14 |
Southwest |
36.56 |
40.55 |
22.89 |
Upper_midwest |
56.88 |
36.75 |
6.37 |
West |
36.20 |
40.35 |
23.45 |
# Move No Drought column to first column
df18 <- df7[, c(1,11:13)]
df19 <- df18[, c(1, 3, 2, 4)]
# Move the Overall row to the first row
ovr <- df19[df19$noaa_region == "Overall", ]
df20 <- rbind(ovr, df19[df19$noaa_region != "Overall", ])
kable(df20, caption = "Standard Deviation", align = c("l", "c", "c", "c"))
Standard Deviation
Overall |
14.21 |
15.06 |
15.02 |
Northeast |
14.45 |
16.38 |
15.66 |
Northern_rockies |
10.26 |
10.99 |
10.53 |
Northwest |
12.33 |
12.84 |
12.05 |
Ohio_valley |
14.20 |
16.49 |
17.68 |
South |
14.21 |
14.58 |
15.11 |
Southeast |
13.24 |
14.21 |
16.20 |
Southwest |
12.56 |
12.23 |
12.37 |
Upper_midwest |
12.93 |
13.76 |
14.57 |
West |
16.38 |
16.73 |
15.33 |