繁体   English   中英

矢量投影以确定拟合曲线中的弯头

[英]Vector projection to determine elbow in fitted curve

我有一个已拟合非线性回归的图。 我想计算拟合曲线中的弯头。 大多数第二种微分方法无法准确地捕捉到这一点,并且目视检查似乎是唯一的手段(对自动化没有用)。 与自动“可视”方法最接近的是使用矢量投影来计算从连接数据集的第一个点和最后一个点的直线上最远的数据点(请参见下面的问号)。 可以使用R计算垂直于连接第一个点和最后一个点的线的那条线吗?

我的非线性函数是:结果<-lm(y〜x + I(x ^ 2)+ I(x ^ 3)+ I(x ^ 4)+ I(x ^ 5),data = myData)

在此处输入图片说明

试试看。 查看它是否适用于您的真实数据。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM