1

I've fit a 4th order polynomial curve to my data like so:

y<-c(-13,16,35,40,28,36,43,33,40,33,22,-5,-27,-31,-29,-25,-26,-31,-26,-24,-25,-29,-23,4)
x<-1:24

#4th order polynomial fit
fit<-lm(y~poly(x,4,raw=TRUE))

plot(x,y,ylim=c(min(y)-10,max(y)+10))
lines(x,predict(fit,data.frame(x=x)),col="red")
abline(h=0,lty=2)

My final goal would be to calculate the 3 points of this curve where it meets the zero line.

So first, I need to extend the end of the curve fit so it passes beyond the zero line for a third time. Once I have done this, I would want to calculate the 3 points where this equation passes through the zero line.

4

1 回答 1

5

您可以使用该predict函数从拟合模型中获取值。例如

pred <- function(x) predict(fit, newdata=data.frame(x=x))

然后,如果你想要多个根,你可以使用uniroot.allrootSolve中的函数

rootSolve::uniroot.all(pred, c(0,30))
# 1.428859 11.990087 24.420745

这将从您的模型中找到 0 到 30 之间的根。uniroot您也可以多次调用基本函数。

于 2016-02-22T21:50:13.010 回答