MATLAB Answers

Optimize b-spline fitting while working with functional data analysis (fda) and slm-Toolbox

12 views (last 30 days)
rowJoe
rowJoe on 1 Jul 2016
Edited: rowJoe on 6 Jul 2016
I would like to work with b-splines in functional data analysis context. Therefore the goodness of the fit deviation from fitting to "real" data - is mandatory.
There is a dataset with let's say two variables (each in a separate plot) and their function values which I try to fit within a specific range - the left and right bound - with a b-spline basis expansion system. Thus I create a fit with the SLM Toolbox from John d'Errico on my "real" data (shown as blue circles). The result of the fit is shown as red line with 20 knots (dashed green vertical lines).
%%Create testdata and settings
tData = [155.414600000000 26.0051000000000 0.148509736735732;...
155.456400000000 26.0799000000000 0.137307258776272;...
155.509200000000 26.1448000000000 0.128057451105677;...
155.577400000000 26.2042000000000 0.113255101482559;...
155.666500000000 26.2658000000000 0.103700114826941;...
155.785300000000 26.3395000000000 0.0916774693051268;...
155.945900000000 26.4484000000000 0.0836495658335745;...
156.162600000000 26.6189000000000 0.0740131685766838;...
156.431000000000 26.8455000000000 0.0689090379013958;...
156.741200000000 27.1073000000000 0.0617360299138331;...
157.091000000000 27.3966000000000 0.0605832310638276;...
157.479300000000 27.7079000000000 0.0568441942031618;...
157.913800000000 28.0489000000000 0.0556327128149601;...
158.393400000000 28.4130000000000 0.0492277564656799;...
158.924700000000 28.8024000000000 0.0506784291846306;...
159.519800000000 29.2259000000000 0.0491172265542336;...
160.189700000000 29.6824000000000 0.0477431809016943;...
160.974000000000 30.2016000000000 0.0505541635063485;...
161.932100000000 30.8209000000000 0.0544171113558086;...
163.281800000000 31.7020000000000 0.0658285642365827];
knots = linspace(157.302,162.423,20)';
% Settings for usage of slm
settings1 = slmset('knots',5,...
'increasing','on',...
'plot','on',...
'result','pp');
settings2 = slmset('knots',15,...
'concaveup','on',...
'jerk','negative',...
'plot','on',...
'result','pp');
%%Curve-Fit with SLM
% ... for parameter 1
cfit1 = slmengine(tData(:,1),...
tData(:,2),...
settings1);
hold on
vline(nanmin(knots),'k','left bound');
hold on
vline(nanmax(knots),'k','right bound');
hold on
plot(knots,fnval(cfit1,knots),'^k')
% ... for parameter 2
cfit2 = slmengine(tData(:,1),...
tData(:,3),...
settings2);
hold on
vline(nanmin(knots),'k','left bound');
hold on
vline(nanmax(knots),'k','right bound');
hold on
plot(knots,fnval(cfit2,knots),'^k')
Fig 1.: Output for parameter 1 created with SLM
Fig 2.: Output for parameter 2 created with SLM
Q1: Is there a way to "cut" the b-spline created with slm-toolbox and convert these spline to the fda-classes? The FDA-Toolboy is written by Ramsay/Silvermann ( see link )
While I have no solution for this, I create equally spaced knots in the bounds (black triangles), calculate the function values out of the slm-fit and thy fo fit the b-spline with fda-toolbox.
%%Curve-Fit with FDA
% B-Spline basis for parameter 1
data1 = fnval(cfit1,knots);
rangeval = [min(knots(:,1)), max(knots(:,1))];
norder = 6;
nbasis = norder + length(knots)-2; % Order + internal knots
fdBasisParam1 = create_bspline_basis(rangeval,nbasis,norder,knots);
plot(fdBasisParam1)
% Set smoothing parameters for param 1
Lfdobj = int2Lfd(1);
lambda = 1;
fdParam1 = fdPar(fdBasisParam1,Lfdobj,lambda);
% Smooth & plot
fdObjects1 = smooth_basis(knots, data1, fdParam1);
plot(fdObjects1)
hold on
plot(knots,fnval(cfit1,knots),'^k')
% B-Spline basis for parameter 2
data2 = fnval(cfit1,knots);
rangeval = [min(knots(:,1)), max(knots(:,1))];
norder = 6;
nbasis = norder + length(knots)-2; % Order + internal knots
fdBasisParam2 = create_bspline_basis(rangeval,nbasis,norder,knots);
plot(fdBasisParam2)
% Set smoothing parameters for param 2
Lfdobj = int2Lfd(1);
lambda = 1;
fdParam2 = fdPar(fdBasisParam1,Lfdobj,lambda);
% Smooth & plot
fdObjects2 = smooth_basis(knots, data2, fdParam2);
plot(fdObjects2)
hold on
plot(knots,fnval(cfit2,knots),'^k')
Fig 3.: Output for parameter 1 created with FDA as example
Q2: Is there a possibility to increace the quality of the fitting? In slm there is the opportunity to set parameters like "increasing" and " concaceup". How can I realize these parameters with fda-package and achive an comparable quality for the fits?
It would be fine if someone could help me with these issues.
Hope to hear from you :)

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by