-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_adding_weather_data.R
54 lines (38 loc) · 1.7 KB
/
08_adding_weather_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## Adding weather predictors to data frame
## 24 Aug 2022
library(tidyverse)
# Read in daily info
new <- read_csv("files_for_models/temperature_meta.csv")
# Add in new weather data (format: list of vectors)
load("output/maxtemp_all.Rdata")
load("output/mintemp_all.Rdata")
load("output/meantemp_all.Rdata")
un.id <- unique(new$animal_id)
new$date <- as.Date(new$date)
wdat <- data.frame()
for (j in 1:length(un.id)) {
dat <- new[new$animal_id==un.id[j], ]
# Temperature variables
dat$mtemp <- airt[[j]]
dw <- aggregate(mtemp ~ animal_id + date, data=dat, FUN=mean) # Mean temperature (deg K)
dw$mtemp <- dw$mtemp - 273.15 # convert to degrees C
mint2 <- data.frame(animal_id=dat$animal_id, date=dat$date, mint=mint[[j]]) # Minimum temperature (deg K)
mint2$mint <- mint2$mint - 273.15
dmint <- aggregate(mint ~ animal_id + date, data=mint2, FUN=mean)
tmax <- data.frame(animal_id=dat$animal_id, date=dat$date, maxt=maxt[[j]]) # Maximum temperature (deg K)
tmax$maxt <- tmax$maxt - 273.15
dmaxt <- aggregate(maxt ~ animal_id + date, data=tmax, FUN=mean)
dw <- cbind(dw, dmint$mint, dmaxt$maxt)
names(dw)[4:5] <- c("mintemp", "maxtemp")
wdat <- rbind(wdat, dw)
}
# Read in precipitation data
precip <- read_csv("files_for_models/daily_precip.csv")
# join precipitation and temperature data
wdat <- left_join(precip, wdat, by=c("animal_id", "date"))
# save precipitation and weather data
# write_csv(wdat, "files_for_models/weather_covars.csv")
wdat <- read_csv("files_for_models/weather_covars.csv")
## Check correlation between minimum temperature and precipitation
cor.test(wdat$prcp, wdat$mintemp, alternative="two.sided", method="pearson", conf.level=0.95)
cor(wdat[,c(3,5)])