我有一个符合非线性回归的图表。我想计算拟合曲线中的肘部。大多数第二种微分方法都无法准确地捕捉到这一点,目视检查似乎是唯一的追求方法(对自动化无用)。与自动“可视”方法最接近的是使用矢量投影来计算连接数据集的第一个和最后一个点的线的最远数据点(参见下面的问号)。可以使用R?
计算与连接第一个和最后一个点的直线垂直的这条直线我的非线性函数是: 结果< -lm(y~x + I(x ^ 2)+ I(x ^ 3)+ I(x ^ 4)+ I(x ^ 5),data = myData)
答案 0 :(得分:4)
试一试。看看它是否适用于您的真实数据。
library(MASS)
# fake data
x <- 5:300
y <- (x - 0.03*x^2 + 0.02*x^3 + rnorm(length(x), sd=5000))/1000
myData <- data.frame(x, y)
# fitted curve (I used a simpler example)
result <- lm(y ~ x + I(x^2) + I(x^3), data=myData)
p <- fitted(result)
# line connecting endpoints of fitted curve
i1 <- which.min(x)
i2 <- which.max(x)
slope <- (p[i2] - p[i1]) / (x[i2] - x[i1])
int <- p[i1] - slope*x[i1]
# for every point on the predicted curve (xi, pi), the perpendicular line that goes through that point has
perpslope <- -1/slope
perpint <- p - perpslope*x
# the intersection of the perp line(s) with the connecting line is
xcross <- (int - perpint) / (perpslope - slope)
ycross <- slope*xcross + int
# the distance between the intersection and the point(s) is
dists <- sqrt((x - xcross)^2 + (y - ycross)^2)
# the index of the farthest point
elbowi <- which.max(dists)
# plot the data
eqscplot(x, y)
lines(x[c(i1, i2)], p[c(i1, i2)])
points(x[elbowi], p[elbowi], pch=16, col="red")
lines(x[order(x)], p[order(x)], col="blue")
lines(c(x[elbowi], xcross[elbowi]), c(p[elbowi], ycross[elbowi]), col="red")