今天简单展示下,一些数据平滑的实现方法。
局部加权回归散点平滑法(locally weighted scatterplot smoothing,LOWESS或LOESS)最简单实现方法就是通过sgplot的loess语句,其中的smooth选项可以指定平滑的程度,以SAS自带数据cars为例:
proc sgplot data=sashelp.cars;
loess x=horsepower y=mpg_highway /smooth=0.1;
xaxis grid;
yaxis grid;
run;
↑proc sgplotd 的 loess
除此之外,LOESS也有专门的过程Proc loess可以通过选项select=AICC来依据AICC值自动选择平滑程度。
proc loess data=sashelp.cars plots(only)=(FitPlot);
model mpg_highway=horsepower/select= AICC alpha=.05 all;
run;
↑proc loess 的 loess
样条函数(spline)中最容易实现的是penalized B-spline curve, 借助sgplot里的pbspline语句。
proc sgplot data=sashelp.cars;
pbspline x=horsepower y=mpg_highway /smooth=0.1;
xaxis grid;
yaxis grid;
run;
↑proc sgplot 的 pbspline
文章中经常碰到限制性立方样条函数(restricted cubic spline),在SAS里好像无法直接实现,需要借助宏程序%RCSPLINE Proc Reg Proc sgplot。
↑%RCSPLINE 的restricted cubic spline
说到这里,有位大牛写了一个超牛叉的SAS宏,总共4K多行,尼玛光是说明书就有52页,我也是醉了。不过说起功能呢,也是无敌,可以在unconditional logistic, pooled logistic, conditional logistic, 甚至是 proportional hazards regression models里实现限制性立方样条函数。这个宏就是传说中的%LGTPHCURV9。
↑%LGTPHCURV9
SAS里有一个过程,Proc transreg,通过对因变量和自变量的变形,可以实现更灵活的拟合。
比如直线拟合:
proc transreg data=a;
model identity(y)=identity(x);
run;
↑identity直线拟合
再比如,penalized B-spline:
proc transreg data=a;
model identity(y)=pbspline(x / nknots=9);
run;
↑pbspline拟合
最后,看了R包ggplot2里的geom_smooth函数里通过method=可以设置“lm”,'glm','gam'以及'rlm',再配合formula=感觉更灵活方便。
最最后:希望试试%RCSPLINE,请在StatsThinking微信后台直接回复「m1」;希望试试%LGTPHCURV9,请在StatsThinking微信后台直接回复「m2」
版权说明:
感谢支持鼓励,形式包括不限于转发,转载,推荐,打赏。
微信公众平台可直接转载,欲获取修改白名单权限,请后台回复「51BMD」。
其他平台转载,不得省略作者信息,包括公众号二维码。
联系客服