如何从tidyverse中的线性回归计算几个斜率 [英] How to calculate several slopes from linear regressions in tidyverse

查看:34
本文介绍了如何从tidyverse中的线性回归计算几个斜率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着时间的推移,我测量了土壤孵化器(装有土壤的封闭罐子)中的甲烷浓度.为了计算甲烷生成率,我需要将二阶多项式回归模型拟合到甲烷浓度 (ch4_umol) 和时间 (stamp) 之间的关系.我想为我的数据集创建两个新列:回归线斜率的值和 Rsquared 值.我想为每个jar_camp"计算这两个值.

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!

免责声明:我是新手,主要使用 tidyverse.

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

我的数据如下所示:

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))

推荐答案

with tidyverse/purrr:

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   

我将 stamp 转换为从组开始的秒数 (dt),以便回归正常工作.

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

这篇关于如何从tidyverse中的线性回归计算几个斜率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆