Rav*_*avi 5 r ggplot2 coefficients
我试图从qplot中提取500个变量的截距和斜率.我正在使用的代码:
qplot(gd, nd, data = test, colour = factor(ENT)) +
geom_smooth(method = "lm", se = FALSE)
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我提取每个回归线(500行/变量)的截距和斜率,如附图所示.

ggplot将绘制图形,但您从lm()对象中提取系数(截距和斜率)。实现后者的一种方法是使用 dplyrgroup_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])
})
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3581 次 |
| 最近记录: |