Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the y-axis intercept and slope from a linear regression of multiple data and passing the intercept and slope values to a data frame

I have a data frame x1, which was generated with the following piece of code,

x <- c(1:10)
y <- x^3
z <- y-20
s <- z/3
t <- s*6
q <- s*y
x1 <- cbind(x,y,z,s,t,q)
x1 <- data.frame(x1)

I would like to extract the y-axis intercept and the slope of the linear regression fit for the data,

    x    y   z          s    t             q
1   1    1 -19  -6.333333  -38     -6.333333
2   2    8 -12  -4.000000  -24    -32.000000
3   3   27   7   2.333333   14     63.000000
4   4   64  44  14.666667   88    938.666667
5   5  125 105  35.000000  210   4375.000000
6   6  216 196  65.333333  392  14112.000000
7   7  343 323 107.666667  646  36929.666667
8   8  512 492 164.000000  984  83968.000000
9   9  729 709 236.333333 1418 172287.000000
10 10 1000 980 326.666667 1960 326666.666667 

I use the following codes to melt and plot three columns of data,

xm <- melt(x1, id=names(x1)[1], measure=names(x1)[c(2, 4, 5)], variable = "cols")
plt <- ggplot(xm) +
    geom_point(aes(x=x,y= value, color=cols), size=3) +
    labs(x = "x", y = "y") 

enter image description here

Now what I require is to get a linear least squares fit for all the data separately and store the resulting intercept and slope in a new data frame.

I use plt + geom_abline() but I don't get the desired result. Could someone let me know how to resolve this.

like image 574
Amm Avatar asked Jan 24 '14 14:01

Amm


Video Answer


1 Answers

I suppose you're looking for geom_smooth. If you call this function with the argument method = "lm", it will calculate a linear fit for all groups:

ggplot(xm, aes(x = x, y = value, color = cols)) +
  geom_point(size = 3) +
  labs(x = "x", y = "y") + 
  geom_smooth(method = "lm", se = FALSE)

enter image description here

You can also specify a quadratic fit with the poly function and the formula argument:

ggplot(xm, aes(x = x, y = value, color=cols)) +
  geom_point(size = 3) +
  labs(x = "x", y = "y") + 
  geom_smooth(method = "lm", se = FALSE, formula = y ~ poly(x, 2))

enter image description here


To extract the corresponding regression coefficients, you can use this approach:

# create a list of coefficients
fits <- by(xm[-2], xm$cols, function(i) coef(lm(value ~ x, i)))

# create a data frame
data.frame(cols = names(fits), do.call(rbind, fits))

#   cols X.Intercept.         x
# y    y   -277.20000 105.40000
# s    s    -99.06667  35.13333
# t    t   -594.40000 210.80000

If you want a quadratic fit, just replace value ~ x with value ~ poly(x, 2).

like image 141
Sven Hohenstein Avatar answered Sep 22 '22 16:09

Sven Hohenstein