How to extract intercept and slope from a graph for a large number of variables

I am trying to extract hooks and slopes for 500 variables from qplot. The code I'm using:

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

      

Can anyone help me to extract the intercept and slope for each regression line (500 lines / variables) as shown in the attached figure.

+3


source to share


2 answers


How do I use a function lmList

that is designed to compute linear regressions for multiple groups?



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

      

+3


source


ggplot

will draw the graph, but you are extracting coefficients (interceptions and slopes) from objects lm()

. One way to do this is to use the dplyr group_by()

and do()

. See? Do

I am using the mtcars dataframe here.

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

      

+1


source







All Articles