0

我一直在尝试使用拉格朗日插值来解决问题,该插值在 R 语言的poly.calc方法(包)中实现。polynom

基本上,我的问题是使用拉格朗日插值法预测某个国家的人口。我有过去几年(1961 - 2014)的人口。csv文件在这里

w1 = read.csv(file="country.csv", sep=",", head=TRUE)
array_x = w1$x
array_y = w1$y

#calls Lagrange Method
p = poly.calc(array_x, array_y)

#create a function to evaluate the polynom
prf <- as.function(p)
#create some points to plot
myx = seq(1961, 2020, 0.5)
#y's to plot
myy = prf(myx)
#plot
plot(myx, myy,col='blue')

之后,绘制的曲线正在下降,y 轴为(非常大)负(134 的幂)。它没有任何意义。但是,如果我使用五点,那是正确的。

4

1 回答 1

0

This is not really an SO question but rather a numerical analysis question.

R is doing everything you want it to, it's not a programming error. It's just that what you want it to do is notoriously bad. Lagrange polynomials are notorious for being incredibly unstable, especially when a large number of points are fit.

A much more stable alternative is the use of splines, such as B-splines. They can be fit very easily with R's default spline library into any regression model, i.e. you could fit a least squares model with

library(splines)
x <- sort(runif(500, -3,3) ) #sorting makes for easier plotting ahead
y <- sin(x)
splineFit <- lm(y ~ bs(x, df = 5) )
est_y <- predict(splineFit)
plot(x, y, type = 'l')
lines(x, est_y, col = 'blue')

You can see from the above model that the splines can do a good job of fitting non-linear relations.

于 2015-11-02T05:44:40.510 回答