---
title: "Interpolating"
author: "Steffi LaZerte"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{interpolation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


```{r, include = FALSE}
knitr::opts_chunk$set(cache = FALSE)
options(tibble.max_extra_cols = 0)
```

## Packages

You'll need several packages from the **tidyverse** in addition to **`weathercan`** to complete the following analysis.

```{R pck, message = FALSE}
library(weathercan)
library(ggplot2)
library(dplyr)
```

## General usage
You can merge weather data with other data frames by linearly interpolating between points.

For example, here we have a dataset of weather data from Kamloops

```{r}
glimpse(kamloops)
```

As well as a data set of finch visits to an RFID feeder
```{r}
glimpse(finches)
```

Although the times in the weather data do not exactly match those in the finch data, we can merge them together through linear [interpolation](https://en.wikipedia.org/wiki/Linear_interpolation). This function uses the `approx` function from the `stats` package under the hood.

Here we specify that we only want the temperature (`temp`) column:

```{r, out.width = "100%", fig.width = 8, dpi = 200}
finches_temperature <- weather_interp(data = finches, weather = kamloops, cols = "temp")
summary(finches_temperature)
glimpse(finches_temperature)

ggplot(data = finches_temperature, aes(x = temp, fill = animal_id)) +
  theme_bw() +
  theme(legend.position = "none") + 
  geom_histogram(binwidth = 1) +
  labs(x = "Temperature (C)", y = "Activity Count", fill = "Finch ID")
```

Or summarized:
```{r, out.width = "100%", fig.width = 8, dpi = 200}
finches_temperature <- finches_temperature %>%
  group_by(date) %>%
  summarize(n = length(time),
            temp = mean(temp))

ggplot(data = finches_temperature, aes(x = date, y = n)) +
  theme_bw() +
  theme(legend.position = "top") +
  geom_point(aes(shape = "Activity")) +
  geom_line(aes(y = temp * 100, colour = "Temperature")) +
  scale_colour_discrete(name = "") +
  scale_shape_discrete(name = "") +
  scale_y_continuous(name = "Activity", sec.axis = sec_axis(~. / 100, name = "Temperature (C)"))
```

## Data gaps

By default, gaps of 2 hours (or 2 days, with a daily scale) will be interpolated over (i.e. they will be filled with values interpolated from either side of the gap), but longer gaps will be skipped and filled with `NA`s. You can adjust this behaviour with `na_gap`. Note that as Environment and Climate Change Canada data is downloaded on an hourly scale, it makes no sense to apply `na_gap` values of less than 1.

In this example, note the larger number of `NA`s in `temp` and how it corresponds to the missing variables in the weather dataset:

```{r}
finches_temperature <- weather_interp(data = finches, weather = kamloops, 
                                      cols = "temp", na_gap = 1)
summary(finches_temperature)

finches_temperature %>% 
  select(date, time, temp) %>%
  filter(is.na(temp))

kamloops %>%
  select(time, temp) %>%
  filter(is.na(temp))
```

## Multiple weather columns

We could also add in more than one column at a time:

```{r, out.width = "100%", fig.width = 8, dpi = 200}
finches_weather <- weather_interp(data = finches, weather = kamloops,
                                  cols = c("temp", "wind_spd"))
summary(finches_weather)
glimpse(finches_weather)


finches_weather <- finches_weather %>%
  group_by(date) %>%
  summarize(n = length(time),
            temp = mean(temp),
            wind_spd = mean(wind_spd))

ggplot(data = finches_weather, aes(x = date, y = n)) +
  theme_bw() +
  theme(legend.position = "top") +
  geom_bar(stat = "identity") +
  geom_line(aes(y = temp * 50, colour = "Temperature"), size = 2) +
  geom_line(aes(y = wind_spd * 50, colour = "Wind Speed"), size = 2) +
  scale_colour_discrete(name = "") +
  scale_y_continuous(name = "Activity Counts", sec.axis = sec_axis(~. / 50, name = "Temperature (C) / Wind Speed (km/h)"))
```

