0

给定如下所示的数据:

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 贴合度?

4

1 回答 1

1

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

lines(x=sort(MakeSmooth), y=predict(Poly2,data.frame(x=MakeSmooth))[order(MakeSmooth)], col="blue", type="l")
于 2017-03-24T17:05:08.383 回答