Closed djhocking closed 9 years ago
I figured out a quick way to add the moving means with dplyr
and zoo
(and add lagged effects). Apparently there is also a RccpRoll
package that is supposed to be incredibly fast but I can't get it installed right now. I will have to wait to do the moving means until the database is set up and I can do it on the daymet data before joining with the observed data and before clipping the breakpoints so I'm not adding NA to the data.
Example
library(dplyr)
library(zoo)
df <- expand.grid(site = factor(seq(10)),
year = 2000:2004,
day = 1:50)
# use Poisson to make math easy to check moving means of temperature
df$temp <- rpois(dim(df)[1], 5)
# Assume rains 33% of the days and averages 5 mm each time but highly variable
df$precip <- rbinom(dim(df)[1], 1, 1/3) * rlnorm(dim(df)[1], log(5), 1)
# moving mean for that day and previous days (e.g. 5 represents the mean of that day and the for previous days)
df2 <- df %>%
group_by(site, year) %>%
arrange(site, year, day) %>%
mutate(temp.5 = rollsum(x = temp, 5, align = "right", fill = NA))
head(df2, 75)
# moving mean for the previous days not including the current day (e.g. 5 represents the mean of the 5 previous days)
df2 <- df2 %>%
group_by(site, year) %>%
mutate(temp.lag1 = lag(temp, n = 1)) %>%
mutate(temp.2 = rollapply(data = temp.lag1,
width = 2,
FUN = mean,
align = "right",
fill = NA,
na.rm = T))
head(df2, 75)
#combined
df2 <- df %>%
group_by(site, year) %>%
arrange(site, year, day) %>%
mutate(temp.5 = rollsum(x = temp, 5, align = "right", fill = NA),
temp.5.previous = rollapply(data = temp.lag1,
width = 2,
FUN = mean,
align = "right",
fill = NA,
na.rm = T)
precip.30 = rollsum(x = precip, 30, align = "right", fill = NA))
head(df2, 75)
Revise the scripts to calculate the moving means for airTemp (5-day) and prcp (30-day) to replace the 1 & 2 day lagged effects. This will require working with the longer records of Daymet rather than just for the days with observed water temperature data so that the first 4 and 29 days don't get NA.