How to calculate several slopes from linear regressions in tidyverse

大兔子大兔子 提交于 2021-01-29 15:31:18

问题


I have measured the methane concentration in soil incubations (closed jars with soil in them) over time. To calculate the methane production rate I need to fit a second‐order polynomial regression model to the relationship between methane concentration (ch4_umol) and time (stamp). I would like to make two new columns to my dataset: The value of the regression line slope and the Rsquared value. I would like to calculate these two values for each "jar_camp".

Can anyone help with this? That would be awesome!

Disclaimer: I'm a newbie and I primarily work with tidyverse.

My data looks like this:

structure(list(jar_camp = c("1_pf1.1", "1_pf1.1", "1_pf1.1", 
"1_pf1.1", "1_pf1.1", "1_pf1.1", "2_pf1.1", "2_pf1.1", "2_pf1.1", 
"2_pf1.1", "2_pf1.1", "2_pf1.1", "3_pf1.1", "3_pf1.1", "3_pf1.1", 
"3_pf1.1", "3_pf1.1", "1_pf2.1", "1_pf2.1", "1_pf2.1", "1_pf2.1", 
"1_pf2.1", "1_pf2.1", "2_pf2.1", "2_pf2.1", "2_pf2.1", "2_pf2.1", 
"2_pf2.1", "2_pf2.1", "3_pf2.1", "3_pf2.1", "3_pf2.1", "3_pf2.1", 
"3_pf2.1"), jar = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 
3, 3, 3, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3), 
    campaign = c("pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", 
    "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", 
    "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf2.1", "pf2.1", 
    "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", 
    "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", 
    "pf2.1"), stamp = structure(c(1546688646, 1546688647, 1546688649, 
    1546688651, 1546688653, 1546688654, 1546689321, 1546689323, 
    1546689324, 1546689326, 1546689328, 1546689329, 1546689877, 
    1546689878, 1546689880, 1546689882, 1546689884, 1547031076, 
    1547031077, 1547031079, 1547031081, 1547031083, 1547031084, 
    1547032136, 1547032137, 1547032139, 1547032141, 1547032143, 
    1547032144, 1547033073, 1547033075, 1547033076, 1547033078, 
    1547033080), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    ch4_umol = c(74.982885373, 74.315864696, 75.405874095, 73.876607177, 
    74.153176726, 74.429746275, 159.645704961, 159.661973758, 
    159.678242555, 159.694511352, 159.710780149, 159.75958654, 
    134.673101566, 135.779379762, 135.584154198, 135.600422995, 
    136.6578948, 455.542584797, 455.656466376, 455.998111113, 
    455.998111113, 455.623928782, 455.591391188, 461.838609236, 
    461.887415627, 461.985028409, 461.789802845, 461.627114875, 
    461.789802845, 441.356193813, 440.982011482, 441.20977464, 
    441.112161858, 441.112161858)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -34L))

回答1:


with tidyverse / purrr:

data <-structure(list(jar_camp = c("1_pf1.1", "1_pf1.1", "1_pf1.1", 
                                   "1_pf1.1", "1_pf1.1", "1_pf1.1", "2_pf1.1", "2_pf1.1", "2_pf1.1", 
                                   "2_pf1.1", "2_pf1.1", "2_pf1.1", "3_pf1.1", "3_pf1.1", "3_pf1.1", 
                                   "3_pf1.1", "3_pf1.1", "1_pf2.1", "1_pf2.1", "1_pf2.1", "1_pf2.1", 
                                   "1_pf2.1", "1_pf2.1", "2_pf2.1", "2_pf2.1", "2_pf2.1", "2_pf2.1", 
                                   "2_pf2.1", "2_pf2.1", "3_pf2.1", "3_pf2.1", "3_pf2.1", "3_pf2.1", 
                                   "3_pf2.1"), jar = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 
                                                       3, 3, 3, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3), 
                      campaign = c("pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", 
                                   "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", 
                                   "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf1.1", "pf2.1", "pf2.1", 
                                   "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", 
                                   "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", "pf2.1", 
                                   "pf2.1"), stamp = structure(c(1546688646, 1546688647, 1546688649, 
                                                                 1546688651, 1546688653, 1546688654, 1546689321, 1546689323, 
                                                                 1546689324, 1546689326, 1546689328, 1546689329, 1546689877, 
                                                                 1546689878, 1546689880, 1546689882, 1546689884, 1547031076, 
                                                                 1547031077, 1547031079, 1547031081, 1547031083, 1547031084, 
                                                                 1547032136, 1547032137, 1547032139, 1547032141, 1547032143, 
                                                                 1547032144, 1547033073, 1547033075, 1547033076, 1547033078, 
                                                                 1547033080), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                      ch4_umol = c(74.982885373, 74.315864696, 75.405874095, 73.876607177, 
                                   74.153176726, 74.429746275, 159.645704961, 159.661973758, 
                                   159.678242555, 159.694511352, 159.710780149, 159.75958654, 
                                   134.673101566, 135.779379762, 135.584154198, 135.600422995, 
                                   136.6578948, 455.542584797, 455.656466376, 455.998111113, 
                                   455.998111113, 455.623928782, 455.591391188, 461.838609236, 
                                   461.887415627, 461.985028409, 461.789802845, 461.627114875, 
                                   461.789802845, 441.356193813, 440.982011482, 441.20977464, 
                                   441.112161858, 441.112161858)), class = c("tbl_df", "tbl", 
                                                                             "data.frame"), row.names = c(NA, -34L))
library(tidyverse)
data <- data %>% group_by(campaign,jar_camp) %>% summarize(ch4_umol,dt=as.numeric(difftime(stamp,min(stamp)))) %>% ungroup()

calclm <- function(data) {
  campaign = data$campaign[[1]]
  lm <-lm(formula = ch4_umol ~ dt , data = data)
  lm.summary = summary(lm)
  list(campaign = campaign,intercept=lm$coefficients[[1]],slope=lm$coefficients[[2]] ,r.squared = lm.summary$r.squared)
}


res <- data %>% split(.$jar_camp) %>% map(~calclm(.x)) %>% bind_rows(.id="jar_camp")
res

jar_camp campaign intercept    slope r.squared
  <chr>    <chr>        <dbl>    <dbl>     <dbl>
1 1_pf1.1  pf1.1         74.9 -0.0813   0.215   
2 1_pf2.1  pf2.1        456.   0.00188  0.000854
3 2_pf1.1  pf1.1        160.   0.0126   0.906   
4 2_pf2.1  pf2.1        462.  -0.0225   0.371   
5 3_pf1.1  pf1.1        135.   0.201    0.665   
6 3_pf2.1  pf2.1        441.  -0.0235   0.209   

I converted stamp to seconds from begin of group (dt) so that the regression works properly.



来源:https://stackoverflow.com/questions/63072261/how-to-calculate-several-slopes-from-linear-regressions-in-tidyverse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!