Search
 
SCRIPT & CODE EXAMPLE
 

R

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)
# A tibble: 4 x 4
#    DOY LOCATION  slope     R2
#  <dbl>    <dbl>  <dbl>  <dbl>
#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
Comment

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)
# A tibble: 4 x 4
#    DOY LOCATION  slope     R2
#  <dbl>    <dbl>  <dbl>  <dbl>
#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
Comment

PREVIOUS NEXT
Code Example
R :: Add tab in string r 
R :: extract df from lm in r 
R :: How to create a new column with spark_apply 
R :: end the program in r 
R :: r max and min functions 
R :: how to exclude inf in r 
R :: rstudio refactor hotkey 
R :: "R" multi-line comment 
R :: combine scripts into a pipeline 
R :: next element in a loop if error in r 
R :: dplyr left join only certain columns 
R :: how to get the number of individual numbers in a vector in r 
Rust :: read file contents in rust 
Rust :: read file buffer rust 
Rust :: for loops in rust 
Rust :: create empty string rust 
Rust :: length of vector rust 
Rust :: how to implement the copy trait for a struct rust 
Rust :: class in rust 
Rust :: rust language 
Rust :: armanriazi•rust•interior-mutability•vs•inherited-mutability 
Rust :: rust Pause execution for several seconds 
Rust :: rust•armanriazi•type•wraper 
Rust :: armanriazi•rust•thread•spawning 
Rust :: rust compiler error 
Lua :: roblox jsonencode 
Lua :: roblox difference between __index and __newindex 
Lua :: remote function unable to cast value to object 
Lua :: remove from table lua 
Lua :: lua indexof 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =