【问题标题】:how to extract intercept and slope from a plot for large number of variables如何从大量变量的图中提取截距和斜率
【发布时间】:2015-05-09 20:39:22
【问题描述】:

我正在尝试从 qplot 中提取 500 个变量的截距和斜率。我正在使用的代码:

qplot(gd, nd, data = test, colour = factor(ENT)) + 
  geom_smooth(method = "lm", se = FALSE)

有人可以帮我提取每条回归线(500 条线/变量)的截距和斜率,如图所示。

【问题讨论】:

标签: r ggplot2 coefficients


【解决方案1】:

ggplot 将绘制图形,但您从lm() 对象中提取系数(截距和斜率)。实现后者的一种方法是使用 dplyr 的 group_by()do() 函数。看?做

我在这里使用 mtcars 数据框。

library(ggplot2)
library(dplyr)

ggplot(mtcars, aes(mpg, disp, colour = factor(cyl))) +
   geom_point() +
   geom_smooth(method = "lm", se = FALSE)

mtcars %>% 
    group_by(cyl) %>% 
    do({
      mod = lm(disp ~ mpg, data = .)
      data.frame(Intercept = coef(mod)[1],
                 Slope = coef(mod)[2])
    })


Source: local data frame [3 x 3]
Groups: cyl

  cyl Intercept      Slope
1   4  233.0674  -4.797961
2   6  125.1225   2.947487
3   8  560.8703 -13.759624

【讨论】:

  • 大家好,我正在使用一些逻辑模型来提取斜率和拐点。但我想在逻辑拟合曲线上的某个位置提取 y 共振。以附图为例。
  • @Ravi,你应该问一个新问题。此外,数字不附加到 cmets。
  • 嗨桑迪..是的,我在这里单独问过这个问题:stackoverflow.com/questions/30676509/…
【解决方案2】:

如何使用lmList 函数,该函数旨在计算跨多个组的线性回归?

library("nlme")
coef(lmList(nd~gd|ENT , data = test))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2013-03-03
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多