Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

r - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...