我测量了一段时间内土壤培养物中甲烷的浓度(其中装有土壤的密闭罐子)。为了计算甲烷的产生率,我需要对甲烷浓度(ch4_umol)和时间(时间戳)之间的关系拟合二阶多项式回归模型。我想在数据集中添加两个新列:回归线斜率的值和Rsquared值。我想为每个“ jar_camp”计算这两个值。
有人可以帮忙吗?太棒了!
免责声明:我是新手,主要从事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))
答案 0 :(得分:1)
使用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
)开始的秒数,以便回归正常进行。