
########################## 2011 weeks mean

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")

##### rescue data
rescue = read.csv("rescue2011.csv", header=T, sep=";", fill=T)
rescue2011 <- rescue[!is.na(rescue$Väljakutse.nr),]
rescue2011 = subset(rescue2011, !duplicated(Väljakutse.nr))

rescue2011 = subset(rescue2011, select=c("Sündmuse.liik..SOS.", "VALLA_NIMI", "Registreerimise.aeg"))
names(rescue2011) <- c("Event", "Location", "Date")

##### choosing event type here
#events <- subset(rescue2011, grepl("liiklus", tolower(Event)))
#events <- subset(rescue2011, grepl("loodus", tolower(Event)))
"
events <- subset(rescue2011, 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(rescue2011, grepl("tulekahju", tolower(Event)))
#events <- subset(rescue2011, grepl('inimene', tolower(Event)))
#events <- rescue2011

events <- subset(events, select=c("Date"))
events$Date <- format(as.Date(events$Date, format="%d.%m.%Y"), "%Y-%m-%d")

##### Loop for all weeks of the year
for (i in 1:51) {
  print(paste("Week", toString(i), sep=" "))
  
  # in case of this year, last sunday of the last week is the first sunday of the year 2012, so we cant get all weeks from on loop
  first_week = toString(as.Date(strptime(paste0(2011, "Monday", i), "%Y%A%U"), format="%Y-%m-%d"))
  last_week = toString(as.Date(strptime(paste0(2011, "Sunday", i+1), "%Y%A%U"), format="%Y-%m-%d"))
  #first_week = toString(as.Date(strptime(paste0(2011, "Monday", 52), "%Y%A%U"), format="%Y-%m-%d"))
  #last_week = toString(as.Date(strptime(paste0(2012, "Sunday", 1), "%Y%A%U"), format="%Y-%m-%d"))
  
  rescue_week = subset(events, Date >= first_week & Date <= last_week)
  print(paste("Number of rescue events", toString(nrow(rescue_week)), sep=" "))
  
  rescue_week$Date <- format(as.Date(rescue_week$Date, format="%Y-%m-%d"), "%Y.%m.%d")
  
  rescue_week <- unique(count(rescue_week, "Date")) 
  print(paste("Number of days with rescue events", toString(nrow(rescue_week)), sep=" "))
  
  names(rescue_week) <- c("Weekday", "Number.of.rescue.events") # count again the number of rescue events
  
  ##### parties
  
  party = subset(parties, Kuupäev >= first_week & Kuupäev <= last_week) # selecting only the year 2011
  print(paste("Number of public events", toString(nrow(party)), sep=" "))
  
  party_week <- subset(party, select=c("Kuupäev"))
  party_week$Kuupäev <- format(as.Date(party_week$Kuupäev, format="%Y-%m-%d"), "%Y.%m.%d")
  
  party_week <- unique(count(party_week, "Kuupäev")) 
  
  names(party_week) <- c("Weekday", "Number.of.public.events")
  
  ##### Merge
  
  # similarly to months and weekdays we get a merged data frame with all days of the week, and then we get the means
  result = merge(rescue_week, party_week, by = c("Weekday"), all=T) 
  
  week_mean_with_parties = subset(result, !is.na(Number.of.public.events))
  if (nrow(week_mean_with_parties) > 0) {
    week_mean_with_parties[is.na(week_mean_with_parties[["Number.of.rescue.events"]]), "Number.of.rescue.events"] <- 0
  }
  print(paste("With parties", toString(nrow(week_mean_with_parties)), sep=" "))
  
  week_mean_without_parties = subset(result, is.na(Number.of.public.events)) 
  print(paste("Without parties", toString(nrow(week_mean_without_parties)), sep=" "))
  
  ##### Mean
  
  print(paste("Mean 2 ", toString(mean(week_mean_with_parties$Number.of.rescue.events)), sep=" "))
  print(paste("Mean 2 ", toString(mean(week_mean_without_parties$Number.of.rescue.events)), sep=" "))
  print(NULL)
}

# as there are 54 week in the year, this loop produces two sequences of 54 means.




