I am using the rollapply function to get the slope estimates of fluorescence across days over a a rolling window of 3 days (to find the maximal slope).
This is the code I am using: the cumulative_dataframe snippet is here: Cumulative_dataframe
ls <- cumulative_df %>%
group_by(id) %>%
filter(n() >= 3) %>%
filter(id!="Blank") %>%
group_split()
z_out <- data.frame(intercept = NA, mu = NA, rsqr = NA, id = NA)
k <- moving_window_point_number
for (i in seq_along(ls)) {
z <- ls[[i]] %>% select(day, lRFU)
tmp_df1 <- rollapply(z, width = k,
function(x) coef(lm(lRFU ~ day, data = as.data.frame(x))),
by.column = FALSE, align = "right")
tmp_df2 <- rollapply(z, width = k,
function(x) summary(lm(lRFU ~ day, data = as.data.frame(x)))$r.squared,
by.column = FALSE, align = "right")
tmps <- data.frame(intercept = tmp_df1[,1], mu = tmp_df1[,2], rsqr = tmp_df2) %>% arrange(-mu)
z_out[i,1:3] <- as.numeric(c(tmps[1,1], tmps[1,2], tmps[1,3]))
z_out[i,4] <- as.character(unique(ls[[i]]$id))
tmp_df1 <- NULL
tmp_df2 <- NULL
tmps <- NULL
z <- NULL
}
write_xlsx(z_out,
paste0(output_dir, cond_new, "gr_estimates_k", k, ".xlsx"))
I'm not sure how to do even begin to do this.