library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ 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
library(gghighlight)
library(leaflet)
library(IRdisplay)
options(scipen = 999)
options(warn = -1)
library(lubridate)
library(viridis)
## Loading required package: viridisLite
library(data.table)
## 
## Attaching package: 'data.table'
## 
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## 
## The following object is masked from 'package:purrr':
## 
##     transpose
library(caTools)
library(kernlab)
## 
## Attaching package: 'kernlab'
## 
## The following object is masked from 'package:purrr':
## 
##     cross
## 
## The following object is masked from 'package:ggplot2':
## 
##     alpha
library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## 
## The following object is masked from 'package:purrr':
## 
##     lift
library(ROSE)
## Loaded ROSE 0.0-4
## Reading in files

list.files(path = "../input")
## character(0)
earthquake <- read.csv("all_month.csv")
glimpse(earthquake)
## Rows: 11,174
## Columns: 22
## $ time            <chr> "2023-08-26T02:45:19.720Z", "2023-08-26T02:26:19.020Z"…
## $ latitude        <dbl> 38.82050, 33.17883, 59.64470, 60.51420, 38.82233, 38.8…
## $ longitude       <dbl> -122.8157, -116.0118, -153.3347, -147.8647, -122.8123,…
## $ depth           <dbl> 2.860, 4.680, 19.500, 13.300, 3.270, 1.690, 4.660, 0.2…
## $ mag             <dbl> 0.76, 1.02, 1.10, 1.90, 1.07, 1.06, 0.80, 1.80, 1.30, …
## $ magType         <chr> "md", "ml", "ml", "ml", "md", "md", "ml", "ml", "ml", …
## $ nst             <int> 7, 21, NA, NA, 21, 20, 24, NA, NA, 70, 18, 13, 19, NA,…
## $ gap             <dbl> 185.00, 55.00, NA, NA, 60.00, 48.00, 47.00, NA, NA, 12…
## $ dmin            <dbl> 0.005239, 0.029080, NA, NA, 0.002058, 0.008963, 0.0699…
## $ rms             <dbl> 0.0100, 0.3000, 0.3300, 0.6100, 0.0300, 0.0300, 0.1800…
## $ net             <chr> "nc", "ci", "ak", "ak", "nc", "nc", "ci", "ak", "ak", …
## $ id              <chr> "nc73929256", "ci39651474", "ak023axn7bzj", "ak023axn6…
## $ updated         <chr> "2023-08-26T02:46:55.405Z", "2023-08-26T02:29:47.330Z"…
## $ place           <chr> "7 km NW of The Geysers, CA", "12 km ENE of Ocotillo W…
## $ type            <chr> "earthquake", "earthquake", "earthquake", "earthquake"…
## $ horizontalError <dbl> 0.82, 0.52, NA, NA, 0.24, 0.21, 0.34, NA, NA, 9.16, 0.…
## $ depthError      <dbl> 1.920, 1.120, 0.200, 0.200, 0.480, 0.440, 0.740, 0.500…
## $ magError        <dbl> 0.0400000, 0.1150000, NA, NA, 0.2100000, 0.1100000, 0.…
## $ magNst          <int> 8, 18, NA, NA, 22, 23, 15, NA, NA, 374, 6, 11, 13, NA,…
## $ status          <chr> "automatic", "automatic", "automatic", "automatic", "a…
## $ locationSource  <chr> "nc", "ci", "ak", "ak", "nc", "nc", "ci", "ak", "ak", …
## $ magSource       <chr> "nc", "ci", "ak", "ak", "nc", "nc", "ci", "ak", "ak", …
head(earthquake)
##                       time latitude longitude depth  mag magType nst gap
## 1 2023-08-26T02:45:19.720Z 38.82050 -122.8157  2.86 0.76      md   7 185
## 2 2023-08-26T02:26:19.020Z 33.17883 -116.0118  4.68 1.02      ml  21  55
## 3 2023-08-26T02:18:04.634Z 59.64470 -153.3347 19.50 1.10      ml  NA  NA
## 4 2023-08-26T02:15:02.890Z 60.51420 -147.8647 13.30 1.90      ml  NA  NA
## 5 2023-08-26T02:10:28.180Z 38.82233 -122.8123  3.27 1.07      md  21  60
## 6 2023-08-26T02:05:14.680Z 38.83150 -122.8150  1.69 1.06      md  20  48
##       dmin  rms net           id                  updated
## 1 0.005239 0.01  nc   nc73929256 2023-08-26T02:46:55.405Z
## 2 0.029080 0.30  ci   ci39651474 2023-08-26T02:29:47.330Z
## 3       NA 0.33  ak ak023axn7bzj 2023-08-26T02:19:24.920Z
## 4       NA 0.61  ak ak023axn6opd 2023-08-26T02:17:11.425Z
## 5 0.002058 0.03  nc   nc73929241 2023-08-26T02:34:15.415Z
## 6 0.008963 0.03  nc   nc73929236 2023-08-26T02:22:14.344Z
##                             place       type horizontalError depthError
## 1      7 km NW of The Geysers, CA earthquake            0.82       1.92
## 2 12 km ENE of Ocotillo Wells, CA earthquake            0.52       1.12
## 3  46 km ESE of Pedro Bay, Alaska earthquake              NA       0.20
## 4      50 km N of Chenega, Alaska earthquake              NA       0.20
## 5      7 km NW of The Geysers, CA earthquake            0.24       0.48
## 6      8 km NW of The Geysers, CA earthquake            0.21       0.44
##   magError magNst    status locationSource magSource
## 1    0.040      8 automatic             nc        nc
## 2    0.115     18 automatic             ci        ci
## 3       NA     NA automatic             ak        ak
## 4       NA     NA automatic             ak        ak
## 5    0.210     22 automatic             nc        nc
## 6    0.110     23 automatic             nc        nc
options(repr.plot.width=12, repr.plot.height=6)

ggplot(earthquake, aes(time, mag)) + 
geom_line(color = 'steelblue')+ theme_bw()+ 
ggtitle("Earthquake Magnitude")+
xlab('Date')+
ylab('Magnitude')+
theme(plot.title = element_text(hjust = 0.5))
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?

#Location
earthquake$location <- sub('.*,\\s*','', earthquake$place)

#Time of the day in 'Hour'
earthquake$hour <- ymd_hms(earthquake$time)
earthquake$hour <- hour(earthquake$hour)

#Visualizing the number of quakes that have happened at a particular time
earthquake %>% 
filter(!is.na(mag))%>%
group_by(hour)%>%
summarise(count = length(id),max_magnitude = max(mag))%>%
ggplot(aes(hour,count, color = max_magnitude))+geom_line()+
scale_color_viridis(direction = -1)+
scale_x_continuous(breaks=seq(0,23,1))+
xlab("Time of the day")+
ylab("Number of earthquakes")+
ggtitle("Number of quakes as per time of the day")+
theme_bw()+
theme(plot.title = element_text(hjust = 0.5))

earthquake %>% 
group_by(location) %>% 
filter(!(is.na(mag)))%>%
summarise(Number_of_quakes = length(location), 
          Average_Magnitude = mean(mag))%>%
mutate(Percent = round(prop.table(Number_of_quakes)*100,2))%>%
arrange(desc(Number_of_quakes))%>% top_n(25, Number_of_quakes)
## # A tibble: 25 × 4
##    location         Number_of_quakes Average_Magnitude Percent
##    <chr>                       <int>             <dbl>   <dbl>
##  1 Alaska                       4066             1.18    36.4 
##  2 CA                           2950             1.10    26.4 
##  3 Hawaii                        892             1.40     7.98
##  4 Nevada                        302             0.752    2.7 
##  5 Washington                    283             0.869    2.53
##  6 Montana                       235             0.956    2.1 
##  7 Alaska Peninsula              227             0.570    2.03
##  8 Oklahoma                      190             1.43     1.7 
##  9 Southern Alaska               147             1.04     1.32
## 10 Puerto Rico                   143             2.60     1.28
## # ℹ 15 more rows
ggplot(earthquake, aes(mag,depth,color = hour))+
geom_jitter(alpha = 0.5)+
gghighlight(max(depth>200)| max(mag>4))+
scale_x_continuous(breaks=seq(0,9,1))+
scale_color_viridis(direction = -1)+
theme_bw()+
xlab('Magnitude')+
ylab('Depth')+
ggtitle('Magnitude Vs. Depth in Km')+
theme(plot.title = element_text(hjust = 0.5))

earthquake %>%
group_by(type)%>%
summarise(count = length(type))%>%
mutate(Percent = prop.table(count*100))%>%
ggplot(aes(type,Percent, fill = type))+
geom_col()+theme_bw()+
xlab('Type of Seismic Activity')+
ylab('Percent')+
ggtitle('Percent of Seismic Activity')+
theme(plot.title = element_text(hjust = 0.5))

ggplot(earthquake, aes('',depth, color = type))+geom_boxplot()+
scale_y_continuous(limits = c(0, 100))+theme_bw()+
xlab('Type of Seismic Activity')+
ggtitle('Depth of Seismic Activity')

ggplot(earthquake, aes('',mag, color = type))+geom_boxplot()+
scale_y_continuous(limits = c(0, 8))+theme_bw()+
xlab('Type of Seismic Activity')+
ggtitle('Magnitude of Seismic Activity')