


########################## all years correlations weather vs rescue events

rm(list = ls())

library(ggplot2)

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

# default date name
# 
# not default
# 



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

###################------------------------ 2010 year rescue events
###----
## Preparing year 2010
d2010 = read.csv("rescue2010.csv", header=T, sep=";", fill=T) # import
data10 <- d2010[!is.na(d2010$Väljakutse.nr),] # removing rescue events with missing id
data10 = subset(data10, !duplicated(Väljakutse.nr)) # removing duplicated id-s

data10 = subset(data10, select=c("Sündmuse.liik..SOS.", "VALLA_NIMI", "Registreerimise.aeg")) # selecting only Type, Municipality and Date
names(data10) <- c("Event", "Location", "Date")


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

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


###################------------------------ 2012 year rescue events
###----
## Preparing year 2012
d2012 = read.csv("rescue2012.csv", header=T, sep=";", fill=T)
data12 <- d2012[!is.na(d2012$Väljakutse.nr),]
data12 = subset(data12, !duplicated(Väljakutse.nr))

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


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

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



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

allyears <- rbind(data10, data11, data12, data13) # combining all years into one data frame

##### Here the different types of rescue events, can be selected by subsetting by name
#events <- allyears # all rescue events
#events <- subset(allyears, grepl("liiklus", tolower(Event))) # road accidents
#events <- subset(allyears, grepl("loodus", tolower(Event))) # nature caused damages
"
# injuries
events <- subset(allyears, 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(allyears, grepl("alarm", tolower(Event))) # fire alarms
events <- subset(allyears, grepl("tulekahju", tolower(Event))) # all fires
#events <- subset(allyears, grepl("mets", tolower(Event))) # forest fires
#events <- subset(allyears, grepl("vahend", tolower(Event))) # vehicle fires
#events <- subset(allyears, grepl("hoones", tolower(Event))) # building fires
#events <- subset(allyears, grepl("väljas", tolower(Event))) # outside fires
"
# other rescue events (do not include previous ones)
events <- subset(allyears, !grepl('liiklus', tolower(Event)) & !grepl('loodus', tolower(Event))
& !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))
& !grepl('tulekahju', tolower(Event)) & !grepl('alarm', tolower(Event)))
"
#events <- subset(allyears, grepl("loom", tolower(Event))) # helpless animal/bird
#events <- subset(allyears, grepl("muud", tolower(Event))) # others (defined by the Estonian Rescue Board)
#events <- subset(allyears, grepl("abi osutamine", tolower(Event))) # assistance
#events <- subset(allyears, grepl("nafta", tolower(Event))) # oil spills
"
# Others that do not include the ones above
events <- subset(allyears, !grepl('liiklus', tolower(Event)) & !grepl('loodus', tolower(Event))
& !grepl('insult', tolower(Event)) & !grepl('pea', tolower(Event))
& !grepl('inimene', tolower(v)) & !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))
& !grepl('tulekahju', tolower(Event)) & !grepl('alarm', tolower(Event))
& !grepl('loom', tolower(Event)) & !grepl('muud', tolower(Event))
& !grepl('abi osutamine', tolower(Event)) & !grepl('nafta', tolower(Event)))
"

## Dates
rescueevents <- subset(events, select=c("Location", "Date")) # next we do not need type column anymore

rescueevents$Location <- tolower(rescueevents$Location)
rescueevents <- subset(rescueevents, Location == 'tallinn') # here we can subset by municipality/city

rescueevents$Year <- format(as.Date(rescueevents$Date, format="%d.%m.%Y"), "%Y") # creating new column Year
rescueevents$Date <- format(as.Date(rescueevents$Date, format="%d.%m.%Y"), "%B") # creting new column Month

rescueevents <- ddply(rescueevents, .(rescueevents$Year, rescueevents$Date), nrow)
names(rescueevents) <- c("Year", "Month", "Number.of.accidents")

###----
##### weather data

weatherall <- read.csv("weather_months.csv", header=T, sep=";", fill=T) # importing weather data
weather <- subset(weatherall, Year == '2010' | Year == '2011' | Year == '2012' | Year == '2013') # selecting only years 2010-2013

weather$Municipality <- tolower(weather$Municipality)
weather <- subset(weather, Municipality == 'tallinn') # from the weather data also select the same city

# all weather indicators should be numeric values
weather$Amount.of.precipitation <- as.numeric(gsub(",",".",weather$Amount.of.precipitation,fixed=TRUE))
weather$Days.with.precipitation <- as.numeric(gsub(",",".",weather$Days.with.precipitation,fixed=TRUE))
weather$Average.temperature <- as.numeric(gsub(",",".",weather$Average.temperature,fixed=TRUE))
weather$Max.temperature <- as.numeric(gsub(",",".",weather$Max.temperature,fixed=TRUE))
weather$Min.temperature <- as.numeric(gsub(",",".",weather$Min.temperature,fixed=TRUE))
weather$Sunshine.hours <- as.numeric(gsub(",",".",weather$Sunshine.hours,fixed=TRUE))
weather$Average.humidity <- as.numeric(gsub(",",".",weather$Average.humidity,fixed=TRUE))

weather$Month <- factor(weather$Month, levels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))

result = merge(weather, rescueevents, by = c("Year", "Month"), all=T) # merging rescue events and weather into one dataset
result[is.na(result)] <- 0

# correlations
cor(result$Amount.of.precipitation, result$Number.of.accidents)
cor(result$Days.with.precipitation, result$Number.of.accidents)
cor(result$Average.temperature, result$Number.of.accidents)
cor(result$Max.temperature, result$Number.of.accidents)
cor(result$Min.temperature, result$Number.of.accidents)
cor(result$Sunshine.hours, result$Number.of.accidents)
cor(result$Average.humidity, result$Number.of.accidents)

ggplot(result, aes(Month, Number.of.accidents)) + geom_bar(stat="identity") + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + ggtitle("2010-2013")



# plotting correlation line by fitting the logistic model
lmlm = lm(Number.of.accidents~Max.temperature, data=result)
plot(result$Max.temperature, result$Number.of.accidents, xlab="Maximum temperature", ylab="Number of fire accidents", main="Fires vs maximum temperature, Tallinn 2010-2013")
abline(lmlm)







