我正在尝试从 qplot 中提取 500 个变量的截距和斜率。我正在使用的代码:
qplot(gd, nd, data = test, colour = factor(ENT)) +
geom_smooth(method = "lm", se = FALSE)
有人可以帮我提取每条回归线(500 条线/变量)的截距和斜率,如附图所示。
最佳答案
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
关于r - 如何从大量变量的图中提取截距和斜率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30144785/