forked from viggin/yan-prtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregress_simplefit_tr.m
34 lines (29 loc) · 1.11 KB
/
regress_simplefit_tr.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [model] = regress_simplefit_tr(X,Y,param)
%Wrapper to matlab's fitting functions
% Include least squares, robust fitting, quadratic terms, etc.
%
% X : each row is a sample.
% Y : a column vector.
% PARAM: struct of parameters. The beginning part of this code (before
% defParam) explains each parameter, and also sets the default parameters.
% You can change parameter p to x by setting PARAM.p = x. For parameters
% that are not set, default values will be used.
% Return:
% MODEL: a struct containing coefficients.
%
% Ke YAN, 2016, Tsinghua Univ. http://yanke23.com, [email protected]
method = 3;
isRobust = 0;
defParam
if method == 1 % robust fit, univariate or multivariate
model.coefs = robustfit(X,Y);
elseif method == 2 % univariate polynomial fit
if size(X,2) > 1, error('only allow univariate'); end
model.coefs = polyfit(X,Y,1);
elseif method == 3 % general method. See doc for LinearModel.
if isRobust, v = 'on'; else v = 'off'; end
model.coefs = LinearModel.fit(X,Y,'RobustOpts',v);
% model = LinearModel.fit(X,Y,'purequadratic'); % include sqr, lin and constant
end
model.method = method;
end