平滑多项式曲线

时间:2017-03-24 16:47:44

标签: r curve-fitting polynomials non-linear-regression loess

给出如下数据:

x<-c(0.287,0.361,0.348,0.430,0.294)
y<-c(105,230,249,758,379)

我正在尝试为这些数据安装几种不同的方法。对于这个问题,我正在研究二阶多项式拟合与黄土拟合。为了获得更平滑的曲线,我想扩展x数据以给我更多的预测点。因此,对于我的黄土曲线,我这样做:

Loess_Fit<-loess(y ~ x)
MakeSmooth<-seq(min(x), max(x), (max(x)-min(x))/1000)

plot(x,y)
#WithoutSmoothing
lines(x=sort(x), y=predict(Loess_Fit)[order(x)], col="red", type="l")
#With Smoothing
lines(x=sort(MakeSmooth), y=predict(Loess_Fit,MakeSmooth)[order(MakeSmooth)], col="blue", type="l")

当我尝试用二阶多项式拟合做同样的事情时 - 我得到一个错误

Poly2<-lm(y ~ poly(x,2,raw=TRUE))
plot(x,y)
#WithoutSmoothing
lines(x=sort(x), y=predict(Poly2)[order(x)], col="red", type="l")
#With Smoothing
lines(x=sort(MakeSmooth), y=predict(Poly2,MakeSmooth)[order(MakeSmooth)], col="blue", type="l")

显然Poly2和Loess_Fit之间存在一些差异,但我不知道有什么区别。有没有办法像我使用Loess_Fit那样平滑Poly2拟合?

1 个答案:

答案 0 :(得分:1)

对于lm,新数据需要是一个数据框:

lines(x=sort(MakeSmooth), y=predict(Poly2,data.frame(x=MakeSmooth))[order(MakeSmooth)], col="blue", type="l")