【发布时间】:2014-02-15 14:54:55
【问题描述】:
我有一个数据框x1,它是用下面这段代码生成的,
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 轴截距和线性回归拟合的斜率,
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
我用下面的代码融合并绘制了三列数据,
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")
现在我需要的是分别对所有数据进行线性最小二乘拟合,并将生成的截距和斜率存储在新的数据框中。
我使用plt + geom_abline(),但没有得到想要的结果。有人可以告诉我如何解决这个问题。
【问题讨论】:
-
那个情节在我看来不是很线性。你想拟合多项式吗?
-
@Roland 这只是一个例子,不过我需要做一个线性拟合
标签: r ggplot2 statistics regression linear-regression