Skip to content

Instantly share code, notes, and snippets.

@hhl60492
Last active August 17, 2018 04:54
Show Gist options
  • Save hhl60492/d08502086cb924085252bec69b40a08c to your computer and use it in GitHub Desktop.
Save hhl60492/d08502086cb924085252bec69b40a08c to your computer and use it in GitHub Desktop.
require(extRemes)
# Sometimes R can be a bit tricky...
df <- read.csv('crimes_daily.csv')
# convert Date field in CSV to R date format
df$DATE <- as.Date(df$DATE)
# Get months
df$Month <- months(df$DATE)
# Get years
df$Year <- format(df$DATE,format="%y")
# number of days to aggregate
n <- 30
# aggregate the Monthly Max
month_bm <- aggregate(df$daily.reported.crimes,list(rep(1:(nrow(df)%/%n+1),each=n,len=nrow(df))),max)[-1];
colnames(month_bm)[1] <- "month_max"
# fit te GEV model using MLE
fit_mle <- fevd(as.vector(month_bm$month_max), method = "MLE", type="GEV", period.basis = "month")
# print the GEV model diagnstic plots
plot(fit_mle)
# return levels:
rl_mle <- return.level(fit_mle, conf = 0.05, return.period= c(3,6,12,24,48,120))
# print the L-moment model diagnstic plots
fit_lmom <- fevd(as.vector(month_bm$month_max), method = "Lmoments", type="GEV", period.basis = "month")
# diagnostic plots
plot(fit_lmom)
# return levels:
rl_lmom <- return.level(fit_lmom, conf = 0.05, return.period= c(3,6,12,24,48,120))
rl_lmom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment