
########################## 2013 month means

rm(list = ls())

library(plyr)

Sys.setlocale("LC_TIME", "C")

## Work
setwd("C:/Users/vovvie/Dropbox/master rescue/thesis files visbek/code")




##################--------------------

##### parties data import

parties = read.csv("parties_celebrations.csv", header=T, sep=";", fill=T)
parties = parties[c("Linn", "Kuupäev")] # extracting Location and Date columns
parties[parties==""] <- NA
parties <- na.omit(parties)
parties$Kuupäev = as.Date(parties$Kuupäev, "%d.%m.%Y")
parties$Month <- format(as.Date(parties$Kuupäev, format="%d.%m.%Y"), "%B") # also adding Month column

##### rescue data
rescue = read.csv("rescue2013.csv", header=T, sep=";", fill=T)
rescue2013 <- rescue[!is.na(rescue$VÄLJAKUTSE_NR),]
rescue2013 = subset(rescue2013, !duplicated(VÄLJAKUTSE_NR))

rescue2013 = subset(rescue2013, select=c("SÜNDMUS", "Vald", "REGISTREERIMISE_AEG"))
names(rescue2013) <- c("Event", "Location", "Date")

##### choosing event type here
#events <- subset(rescue2013, grepl("liiklus", tolower(Event)))
#events <- subset(rescue2013, grepl("loodus", tolower(Event)))
"
events <- subset(rescue2013, grepl('insult', tolower(Event)) | grepl('pea', tolower(Event)) 
| grepl('inimene', tolower(Event)) | grepl('nõrkus', tolower(Event))
| grepl('valu', tolower(Event)) | grepl('kõht', tolower(Event))
| grepl('hingam', tolower(Event)) | grepl('kael', tolower(Event))
| grepl('teadvus', tolower(Event)) | grepl('vere', tolower(Event))
| grepl('jäse', tolower(Event)) | grepl('kõrgus', tolower(Event))
| grepl('iiveldus', tolower(Event)) | grepl('lamab', tolower(Event)) 
| grepl('torke', tolower(Event)) | grepl('rütm', tolower(Event)))
"
events <- subset(rescue2013, grepl("tulekahju", tolower(Event)))
#events <- subset(rescue2013, grepl('inimene', tolower(Event)))
#events <- rescue2013

events$Month <- format(as.Date(events$Date, format="%d.%m.%Y"), "%B")

##### Loop for all 12 months
for (mm in c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")) {
  print(mm)
  
  # here we subset only given month and counr rescue evets on unique days of this month
  event_month = subset(events, Month==mm)
  event_month <- subset(event_month, select=c("Date"))
  event_month$Date <- format(as.Date(event_month$Date, format="%d.%m.%Y"), "%Y.%m.%d")
  print(paste("Number of rescue events", toString(nrow(event_month)), sep=" "))
  
  event_month <- unique(count(event_month, "Date"))
  print(paste("Number of days with rescue events", toString(nrow(event_month)), sep=" "))
  names(event_month) <- c("Day", "Number.of.rescue.events")
  
  ##### parties
  
  # here we also take the month and count the number of public events on each unique day of the month
  party = subset(parties, Kuupäev >= '2013-01-01' & Kuupäev <= '2013-12-31') # selecting only the year 2013
  party = subset(party, Month==mm)
  print(paste("Number of public events", toString(nrow(party)), sep=" "))
  
  party <- subset(party, select=c("Kuupäev"))
  party$Kuupäev <- format(as.Date(party$Kuupäev, format="%Y-%m-%d"), "%Y.%m.%d")
  party <- unique(count(party, "Kuupäev"))
  names(party) <- c("Day", "Number.of.public.events")
  
  ##### Merge
  
  # finally we merge the data, and finf the mean number of rescue events on days with parties and without
  result = merge(event_month, party, by = c("Day"), all=T)
  
  month_mean_with_parties = subset(result, !is.na(Number.of.public.events))
  month_mean_with_parties[is.na(month_mean_with_parties[["Number.of.rescue.events"]]), "Number.of.rescue.events"] <- 0
  print(paste("With parties", toString(nrow(month_mean_with_parties)), sep=" "))
  
  month_mean_without_parties = subset(result, is.na(Number.of.public.events))
  print(paste("Without parties", toString(nrow(month_mean_without_parties)), sep=" "))
  
  ##### Average
  
  print(paste("Mean 1 ", toString(mean(month_mean_with_parties$Number.of.rescue.events)), sep=" "))
  print(paste("Mean 2 ", toString(mean(month_mean_without_parties$Number.of.rescue.events)), sep=" "))
  print(NULL)
}

# as there are 12 months, we get 2 sequences of means with length of 12
