今天学习了三次样条插值的原理,以及stata实现,延伸阅读有matlab以及excel的例子,大家感兴趣可以阅读~
帖子也分为三部分:
一、三次样条插值cubic spline interpolation原理
二、三次样条插值的stata实现
1. mata方法
2. csipolate方法
三、延伸阅读
~~~~~~~~~~~~~~
一、三次样条插值cubic spline interpolation原理
简单来说,为了对缺失的数据点进行插值,同时保证我们插值之后的图形在原有的数据点上一阶导数和二阶导数都是相等的【也就是图形看起来是平滑、没有跳点的,我们就需要使用三次样条插值。这也是这种插值方法的优势所在。
原理步骤为,在两个真实数据点之间插值,采用三次多项式拟合的方法进行拟合;其次对于真实数据点在左右两侧拟合的三次多项式加以限制,即两侧一阶导数和二阶导数相等。
这样得到三次多项式拟合的参数。
二、三次样条插值的stata实现
1. mata方法:上传了一个附件do,也可以看下面的文本。
use "https://www.dropbox.com/s/3y3h39dwbgghusp/stata_spline.dta?dl=0"
mata // This line launches the mata system inside Stata
X = st_data((1,36),"x") // This pulls in the x quarterly markers data.
Y = st_data((1,36),"y") // This pulls in the quarterly y data we want to interpolate between.
XX = st_data(.,"xx") // This pulls in the xx monthly markers we want to interpolate at.
A = spline3(X,Y) // This generates the cubic spline coefficients matrix, and stores it in A.
B = spline3eval(A,XX) // This performs the interpolation, and store the values in B.
st_store(.,"yy",B) // This pushes the inpolated figures in B back into the yy variable in Stata.
end
2. csipolate方法:
ssc install csipolate
csipolate creates newvar by averaging non-missing values of yvar and using natural cubic spline interpolation of missing values of yvar, given xvar. That is, provided that xvar is not missing,
三、延伸阅读
https://columbiaeconomics.com/2012/06/06/stata-do-file-for-cubic-spline-interpolation/