Example: slope by row r
DOY<-c(102,102,102,102,102,102,102,102,102,102,212,212,212,212,212,212, 212,212,212,212)
LOCATION <- c(1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,3,3,3,3,3)
response <-c(NA,NA,NA,NA,NA,7,10,15,20,30,2,4,6,NA,8,10,15,20,30,NA)
ts <- c(0,10,20,30, 40,0,10,20,30,40,0,10,20,30,40,0,10,20,30,40)
test.data <- data.frame(cbind(DOY, LOCATION, response, ts))
library(tidyverse)
library(broom)
pos1 <- possibly(lm, otherwise = NULL)
prsq <- possibly(pull, otherwise = NA)
test.data %>%
group_by(DOY, LOCATION) %>%
nest %>%
mutate(model = map(data, ~ pos1(response~ ts, data = .x)),
slope = map_dbl(model, ~
.x %>%
tidy %>%
filter(term == 'ts') %>%
prsq(estimate)),
R2 = map_dbl(model, ~
.x %>%
glance %>%
prsq(r.squared))) %>%
select(-data, -model)