0

我正在尝试从许多线性回归模型中提取斜率值。我绘制了不同日期的丙酮排放量与水含量的关系。

我有这些图表和模型

在此处输入图像描述 我尝试使用以下代码提取斜率值:

Library(broom)
Library(tidyverse)

lm_table <- df %>%
  nest_by(days) %>% 
  summarise(mdl = list(lm(water_content ~ acetone, data)), .groups = "drop") %>% 
  mutate(adjrsquared = map_dbl(mdl, ~summary(.)$adj.r.squared ),
         mdl = map(mdl, broom::tidy)) %>% 
  unnest(mdl)%>%
  filter(term=="acetone")

还有这段代码:

lm_table2 <- df %>%
  nest_by(days) %>%
  mutate(model = list(lm(water_content ~ acetone, data)),
         coefficients2 = list(tidy(model)))

coefficients2 = lm_table2 %>% 
  unnest(coefficients2)

然而,这两个代码给出的斜率值与我从图中得到的不同。关于为什么会这样的任何想法?

这是数据

df <- structure(list(i.x45.03 = c(22, 17, 11, 1782, 1767, 250, 3568, 
79, 219, 855, 12009, 395, 1552, 705, 2282, 84, 3396, 252, 2058, 
1480, 5, 745, 2573, 1005, 946, 3320, 5406, 2192, 20, 1207, 9519, 
66, 463, 250, 1095, 16556, 88, 2695, 275, 16, 1577, 29, 3221, 
25, 6295, 2, 63, 123, 8, 1, 37, 5308, 4546, 994, 4567, 421, 0, 
1938, 19480, 1027, 3474, 1982, 2819, 69, 27733, 2152, 15429, 
996, 8, 3435, 8748, 17062, 269, 26188, 35823, 2572, 67, 761, 
13493, 1, 1, 1, 16, 9, 29, 89, 20, 11, 21644, 3, 37, 13, 0, 0, 
0, 0, 3, 30, 19, 0, 0, 242, 7246, 1, 20081, 77, 0, 0, 0, 5878, 
0, 0, 22, 2, 4, 1, 93, 12, 2, 73, 0, 19, 0, 0, 2, 48, 3, 0, 0, 
0, 0, 22, 4, 0, 0, 0, 0, 0, 0, 1, 87, 0, 0, 3, 0, 0, 4, 1, 0, 
82, 7, 0, 0, 0, 7, 22, 34, 17, 0, 0, 0, 0, 0, 2, 19, 3, 0, 990, 
0, 0, 0, 0, 84, 9, 0, 5, 1246, 1944, 633, 23640, 262, 5399, 83, 
19, 4417, 125, 7801, 69, 6755, 6, 39, 262), i.water_content_percent_es = c(98, 
39, 85, 14, 21, 28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 
17, 10, 75, 52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 
32, 40, 79, 22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 98, 39, 
85, 14, 21, 28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 17, 
10, 75, 52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 39, 85, 14, 21, 
28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 98, 23, 8, 17, 10, 75, 
52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 32, 40, 79, 
22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 98, 39, 85, 14, 21, 
28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 17, 10, 75, 52, 
13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 32, 40, 79, 22, 
49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 31, 35, 19, 32, 40, 79, 
22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91), daysincubated4 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 24, 24, 24, 24, 
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 116, 116, 116, 
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 
116, 116, 116, 116, 116, 116, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4), days = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 24, 24, 24, 116, 116, 116, 116, 116, 116, 116, 116, 116, 
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), water_content = c(98, 
39, 85, 14, 21, 28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 
17, 10, 75, 52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 
32, 40, 79, 22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 98, 39, 
85, 14, 21, 28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 17, 
10, 75, 52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 39, 85, 14, 21, 
28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 98, 23, 8, 17, 10, 75, 
52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 32, 40, 79, 
22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 98, 39, 85, 14, 21, 
28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 17, 10, 75, 52, 
13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 32, 40, 79, 22, 
49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 31, 35, 19, 32, 40, 79, 
22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91), acetone = c(22, 
17, 11, 1782, 1767, 250, 3568, 79, 219, 855, 12009, 395, 1552, 
705, 2282, 84, 3396, 252, 2058, 1480, 5, 745, 2573, 1005, 946, 
3320, 5406, 2192, 20, 1207, 9519, 66, 463, 250, 1095, 16556, 
88, 2695, 275, 16, 1577, 29, 3221, 25, 6295, 2, 63, 123, 8, 1, 
37, 5308, 4546, 994, 4567, 421, 0, 1938, 19480, 1027, 3474, 1982, 
2819, 69, 27733, 2152, 15429, 996, 8, 3435, 8748, 17062, 269, 
26188, 35823, 2572, 67, 761, 13493, 1, 1, 1, 16, 9, 29, 89, 20, 
11, 21644, 3, 37, 13, 0, 0, 0, 0, 3, 30, 19, 0, 0, 242, 7246, 
1, 20081, 77, 0, 0, 0, 5878, 0, 0, 22, 2, 4, 1, 93, 12, 2, 73, 
0, 19, 0, 0, 2, 48, 3, 0, 0, 0, 0, 22, 4, 0, 0, 0, 0, 0, 0, 1, 
87, 0, 0, 3, 0, 0, 4, 1, 0, 82, 7, 0, 0, 0, 7, 22, 34, 17, 0, 
0, 0, 0, 0, 2, 19, 3, 0, 990, 0, 0, 0, 0, 84, 9, 0, 5, 1246, 
1944, 633, 23640, 262, 5399, 83, 19, 4417, 125, 7801, 69, 6755, 
6, 39, 262)), row.names = c(NA, -192L), class = "data.frame")

我制作的图表的代码是:

library(ggpmisc)
library(tidyverse)

formula <- y~x
ggplot(df, aes(water_content, acetone)) +
  geom_point() +
  geom_smooth(method = "lm",formula = y~x) +
  theme_bw()+
  facet_wrap(~days, scales = "free")+
  stat_poly_eq(
    aes(label = paste(stat(adj.rr.label), stat(eq.label), stat(p.value.label), sep = "*\", \"*")),
    formula = formula, parse = TRUE, size=3)

任何想法为什么我没有得到相同的斜率值?

非常感谢所有帮助!

4

1 回答 1

1

你交换了xy。可能是因为使用了复杂的“tidyverse”编码,这并不明显。

library(nlme)
lmList(acetone ~ water_content | days, data = df)

Call:
  Model: acetone ~ water_content | days 
   Data: df 

Coefficients:
    (Intercept) water_content
0    3314.26811    -31.663431
4   12046.87296   -154.277916
24   3103.13075    -44.368527
116    63.82385     -0.792739

Degrees of freedom: 192 total; 184 residual
Residual standard error: 4538.636
于 2021-02-08T17:13:45.673 回答