-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrobustfit.m
More file actions
103 lines (97 loc) · 4.5 KB
/
robustfit.m
File metadata and controls
103 lines (97 loc) · 4.5 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function varargout = robustfit(X,y,wfun,tune,const)
%ROBUSTFIT Robust linear regression
% B = ROBUSTFIT(X,Y) returns the vector B of regression coefficients,
% obtained by performing robust regression to estimate the linear model
% Y = Xb. X is an n-by-p matrix of predictor variables, and Y is an
% n-by-1 vector of observations. The algorithm uses iteratively
% reweighted least squares with the bisquare weighting function. By
% default, ROBUSTFIT adds a column of ones to X, corresponding to a
% constant term in the first element of B. Do not enter a column of ones
% directly into the X matrix.
%
% B = ROBUSTFIT(X,Y,'WFUN',TUNE) uses the weighting function 'WFUN' and
% tuning constant TUNE. 'WFUN' can be any of 'andrews', 'bisquare',
% 'cauchy', 'fair', 'huber', 'logistic', 'talwar', or 'welsch'.
% Alternatively 'WFUN' can be a function that takes a residual vector as
% input and produces a weight vector as output. The residuals are scaled
% by the tuning constant and by an estimate of the error standard
% deviation before the weight function is called. 'WFUN' can be
% specified using @ (as in @myfun). TUNE is a tuning constant that is
% divided into the residual vector before computing the weights, and it
% is required if 'WFUN' is specified as a function.
%
% B = ROBUSTFIT(X,Y,'WFUN',TUNE,'CONST') controls whether or not the
% model will include a constant term. 'CONST' is 'on' (the default) to
% include the constant term, or 'off' to omit it.
%
% [B,STATS] = ROBUSTFIT(...) also returns a STATS structure
% containing the following fields:
% 'ols_s' sigma estimate (rmse) from least squares fit
% 'robust_s' robust estimate of sigma
% 'mad_s' MAD estimate of sigma; used for scaling
% residuals during the iterative fitting
% 's' final estimate of sigma, the larger of robust_s
% and a weighted average of ols_s and robust_s
% 'se' standard error of coefficient estimates
% 't' ratio of b to stats.se
% 'p' p-values for stats.t
% 'covb' estimated covariance matrix for coefficient estimates
% 'coeffcorr' estimated correlation of coefficient estimates
% 'w' vector of weights for robust fit
% 'h' vector of leverage values for least squares fit
% 'dfe' degrees of freedom for error
% 'R' R factor in QR decomposition of X matrix
%
% The ROBUSTFIT function estimates the variance-covariance matrix of the
% coefficient estimates as V=inv(X'*X)*STATS.S^2. The standard errors
% and correlations are derived from V.
%
% ROBUSTFIT treats NaNs in X or Y as missing values, and removes them.
%
% Example:
% x = (1:10)';
% y = 10 - 2*x + randn(10,1); y(10) = 0;
% bls = regress(y,[ones(10,1) x])
% brob = robustfit(x,y)
% scatter(x,y)
% hold on
% plot(x,brob(1)+brob(2)*x,'r-', x,bls(1)+bls(2)*x,'m:')
%
% See also REGRESS, ROBUSTDEMO.
% References:
% DuMouchel, W.H., and F.L. O'Brien (1989), "Integrating a robust
% option into a multiple regression computing environment,"
% Computer Science and Statistics: Proceedings of the 21st
% Symposium on the Interface, American Statistical Association.
% Holland, P.W., and R.E. Welsch (1977), "Robust regression using
% iteratively reweighted least-squares," Communications in
% Statistics - Theory and Methods, v. A6, pp. 813-827.
% Huber, P.J. (1981), Robust Statistics, New York: Wiley.
% Street, J.O., R.J. Carroll, and D. Ruppert (1988), "A note on
% computing robust regression estimates via iteratively
% reweighted least squares," The American Statistician, v. 42,
% pp. 152-154.
% Copyright 1993-2006 The MathWorks, Inc.
% $Revision: 1.1.6.2 $ $Date: 2010/10/08 17:26:28 $
if nargin < 2
error(message('stats:robustfit:TooFewInputs'));
end
if (nargin<3 || isempty(wfun)), wfun = 'bisquare'; end
if nargin<4, tune = []; end
[eid,emsg,wfun,tune] = statrobustwfun(wfun,tune);
if ~isempty(eid)
error(sprintf('stats:robustfit:%s',eid), emsg);
end
if (nargin<5), const='on'; end
switch(const)
case {'on' 1}, doconst = 1;
case {'off' 0}, doconst = 0;
otherwise, error(message('stats:robustfit:BadConst'));
end
% Remove missing values and check dimensions
[anybad wasnan y X] = statremovenan(y,X);
if (anybad==2)
error(message('stats:robustfit:InputSizeMismatch'));
end
varargout=cell(1,max(1,nargout));
[varargout{:}] = statrobustfit(X,y,wfun,tune,wasnan,doconst);