📅  最后修改于: 2022-03-11 14:51:59.103000             🧑  作者: Mango
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)
# A tibble: 4 x 4
# DOY LOCATION slope R2
#
#1 102 1 NA NA
#2 102 2 0.56 0.953
#3 212 1 0.149 0.966
#4 212 3 0.650 0.966