我有一个已拟合非线性回归的图。我想计算拟合曲线中的弯头。大多数第二种微分方法无法准确地捕捉到这一点,并且目视检查似乎是唯一的手段(对自动化没有用)。与自动“可视”方法最接近的事情是使用矢量投影从连接数据集的第一个点和最后一个点的线计算最远的数据点(请参见下面的问号)。可以使用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")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句