从多个数据的线性回归中获取 y 轴截距和斜率,并将截距和斜率值传递给数据框 [英] 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

查看:18
本文介绍了从多个数据的线性回归中获取 y 轴截距和斜率,并将截距和斜率值传递给数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框x1,它是用下面一段代码生成的,

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)

我想提取数据的 y 轴截距和线性回归拟合的斜率,

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

现在我需要的是分别对所有数据进行线性最小二乘拟合,并将得到的截距和斜率存储在新的数据框中.

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.

我使用 plt + geom_abline() 但我没有得到想要的结果.有人可以告诉我如何解决这个问题.

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

推荐答案

我想您正在寻找 geom_smooth.如果您使用参数 method = "lm" 调用此函数,它将计算所有组的线性拟合:

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)

您还可以使用 poly 函数和 formula 参数指定二次拟合:

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

要提取相应的回归系数,可以使用这种方法:

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

如果您想要二次拟合,只需将 value ~ x 替换为 value ~ poly(x, 2).

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

这篇关于从多个数据的线性回归中获取 y 轴截距和斜率,并将截距和斜率值传递给数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆