---
title: "01_Draft_Report"
author: "Christine"
date: "March 15, 2022"
output: html_document
---

Data: 5 measuring stations, temperature values & dates

## Description - this needs to be updated 

- Variable:  daily maximum temperature measurement 

- Five stations located in south of France and north of Spain 

- Period: 5000 daily temperature measurements recorded between 1950 and 2016 
 (not sure if this still applies: keeping only those records for which all five stations have measurements, resulting in 24472 daily measurements) 


- Rounding: because the data were truncated to the nearest ppb, a uniform random variable on the interval [−0.5, 0.5] was added to the data so that they behave more like the underlying continuous variable. ??? not sure for temperature data

```{r}

## Load data and display first and last 5 lines (also for dates to see the time range)


load("Datasets/14_Temperature.RData")
data.Temp <- dataset
dates.Temp <- dates
head(data.Temp); tail(data.Temp)
head.temp <- head(dates.Temp); tail(dates.Temp)
```


```{r}

## Load graphical package lattice 
library(lattice)

## Times series at each of the 5 stations

dates.Temp=as.Date(as.POSIXct(dates.Temp, 'UTC'))
xyplot(data.Temp[,1]~ dates.Temp,pch=19,ylab="City 1")
xyplot(data.Temp[,2]~ dates.Temp,pch=19,ylab="City 2")
xyplot(data.Temp[,3]~ dates.Temp,pch=19,ylab="City 3")
xyplot(data.Temp[,4]~ dates.Temp,pch=19,ylab="City 4")
xyplot(data.Temp[,5]~ dates.Temp,pch=19,ylab="City 5")
```

## Examine dependence on weekdays

Temperature corresponding to weekday? 

```{r}
## data object is a matrix, create a data frame
class(data.Temp)
# attach names to columns 
colnames(data.Temp) <- c("City1","City2", "City3", "City4", "City5")
Temp=as.data.frame(data.Temp)
names(Temp)
```

```{r}

## in case the locale is not in english
Sys.setlocale("LC_TIME", "en_US.UTF-8")
```

```{r}

## dependence on weekdays: on Sat and Sun there is decrease of NO2
day= factor( weekdays(dates.Temp,abbreviate=TRUE), levels=c("Mon","Tue","Wed","Thu", "Fri", "Sat", "Sun"),ordered=TRUE)

bwplot(day~Temp$City1, main="Temperature in City 1",xlab="Temperature")

```



## Examine dependence on month

Strangely the median is highest for Feb, Mar, Apr for all series

```{r}
month =factor( months(dates,abbreviate=TRUE),levels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE)

bwplot(month~Temp$City1);bwplot(month~Temp$City2);bwplot(month~Temp$City3);bwplot(month~Temp$City4);bwplot(month~Temp$City5)

```

## QQ plots

Empirical quantiles vs normal quantiles

```{r}
qqmath(Temp$City1[month=="Jul"])
```

Empirical quantiles vs exponential quantiles

```{r}
qqmath(Temp$City1[month=="Jul"],distri=qexp)
```


## Extreme value distribution fit

We use the library ismev that goes with the book by Stuart Coles

```{r}
# the library ismev needs to be downloaded from CRAN to your machine
# install.packages("ismev")

## Load library ismev
library(ismev)
```

### GEV modelling for each station

Modelling all data without distinguishing days and months, the tail is not fitted well at all for each of the series. Whatever... 

```{r]
## all days and months together: 
## gev.diag(gev.fit(Temp$City1));gev.diag(gev.fit(Temp$City2));gev.diag(gev.fit(Temp$City3));gev.diag(gev.fit(Temp$City4));gev.diag(gev.fit(Temp$City5))
```

### GEV modelling performed for each month

Better fits are obtained for individual months

```{r}
## for a specific month
gev.diag(gev.fit(Temp$City1[month=="Jul"]));gev.diag(gev.fit(Temp$City2[month=="Jul"]));
gev.diag(gev.fit(Temp$City3[month=="Jul"]));gev.diag(gev.fit(Temp$City4[month=="Jul"]));
gev.diag(gev.fit(Temp$City5[month=="Jul"]))

```

## Mean excess plot

```{r}
mrl.plot(Temp$City1[month=="Jul"])
```


## Scatter plot matrix for the 5 stations

No distinction of months.

Observe the extremes: are they common for a pair of stations ?

```{r}
splom(Temp[,1:5],pch=19)

```


## Scatter plot matrix for the 5 stations

Using a different colour for each month.


```{r}
splom(Temp[,1:5],group=as.factor(month),auto.key=TRUE)
```


```{r}

#summary of cities over all years: 
summary(Temp)

#combine dates & Temp data, add column for year as well as one for month for easier visualisation
Temp1 <- Temp
Temp1$dates <- dates.Temp
Temp1$month <- months(dates.Temp, abbreviate = TRUE)
Temp1$year <- format(as.Date(dates.Temp, format="%d/%m/%Y"),"%Y")
summary(Temp1)


```

```{r}

# nice graphics from Temp1 (use ggplot2)

ggplot() + geom_point(aes(x=Temp1$dates, y=Temp1$City1), shape=1, color = 'lightblue') + geom_hline(yintercept=mean(Temp1$City1)) + geom_hline(yintercept = mean(Temp1$City1)+sqrt(var(Temp1$City1))*1.96,colour = 'darkgreen') + geom_hline(yintercept = mean(Temp1$City1)-sqrt(var(Temp1$City1))*1.96,colour = 'darkgreen')

# x: months, y: Temp, factor: city, mean:over all? --> we have to consider a seasonal trend in our data! 
ggplot() + geom_point(aes(x=Temp1$month, y=Temp1$City1), shape=1, color = 'lightblue') + geom_hline(yintercept=mean(Temp1$City1)) + geom_hline(yintercept = mean(Temp1$City1)+sqrt(var(Temp1$City1))*1.96,colour = 'darkgreen') + geom_hline(yintercept = mean(Temp1$City1)-sqrt(var(Temp1$City1))*1.96,colour = 'darkgreen')


```







