[转载]robustfith函数-最小二乘估计-M估计-Robust regression

转引自http://blog.csdn.net/meng4411yu/article/details/8851187

robustfit

Robust regression(稳健回归)

语法

b=robustfit(X,y)

b=robustfit(X,y,wfun,tune)

b=robustfit(X,y,wfun,tune,const)

[b,stats]=robustfit(…)

描述

b=robustfit(X,y)通过执行稳健回归来估计线性模型y=Xb,并返回一个由回归系数组成的向量b。X是一个np预测变量矩阵,y是一个n1观测向量。计算使用的方法是加上bisquare加权函数的迭代重加权最小二乘法。默认的情况下,robustfit函数把全1的一个列向量添加进X中,此列向量与b中第一个元素的常数项对应。注意不能直接对X添加一个全1的列向量。可以在下面的第三个描述中通过改变变量“const”来更改robustfit函数的操作。robustfit函数把X或y中的NaNs作为一个缺省值,接着把它删除。

b=robustfit(X,y,wfun,tune)增加了一个加权函数“wfun”和常数“tune”。“tune”是一个调节常数,其在计算权重之前被分成残差向量,如果“wfun”被指定为一个函数,那么“tune”是必不可少的。权重函数“wfun”可以为下表中的任何一个权重函数:

《[转载]robustfith函数-最小二乘估计-M估计-Robust regression》 图片.png

如果“tune”未被指定,那么其默认值为表中对应值。“wfun”也可以是一个把残差向量作为输入,并产生一个权重向量作为输出的函数。通过标准误差估计和调节参数来调整残差。“wfun”可以用@(@wyfun)。

b=robustfit(X,y,wfun,tune,const)增加一个“const”控制模式内是否包含一个常数项,默认为包含(on)。

[b,stats]=robustfit(…)返回一个包含一下域的STATS结构。

‘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.

matlab例子:

x = (1:10)’;
y = 10 – 2*x + randn(10,1); y(10) = 0;

使用原始最小二乘估计和稳健回归估计结果如下:

bls = regress(y,[ones(10,1) x])
bls =
7.2481
-1.3208

brob = robustfit(x,y)
brob =
9.1063
-1.8231

显示结果如下:

scatter(x,y,’filled’); grid on; hold on
plot(x,bls(1)+bls(2)x,’r’,’LineWidth’,2);
plot(x,brob(1)+brob(2)
x,’g’,’LineWidth’,2)
legend(‘Data’,’Ordinary Least Squares’,’Robust Regression’)

《[转载]robustfith函数-最小二乘估计-M估计-Robust regression》 image

一个来自网的例子的matlab实现:

估计:
《[转载]robustfith函数-最小二乘估计-M估计-Robust regression》 image

(K>0)

matlab实现代码:

[plain] view plaincopy

<embed id=”ZeroClipboardMovie_1″ src=”http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf” loop=”false” menu=”false” quality=”best” bgcolor=”#ffffff” name=”ZeroClipboardMovie_1″ allowscriptaccess=”always” allowfullscreen=”false” type=”application/x-shockwave-flash” pluginspage=”http://www.macromedia.com/go/getflashplayer” flashvars=”id=1&width=16&height=16″ wmode=”transparent” style=”box-sizing: border-box; outline: 0px;” width=”16″ height=”16″ align=”middle”>

  1. <span style=”font-size:12px;”>function wf=robust(x,y,k)

  2. % find starting values using Ordinary Least Squares

  3. w = x\y;

  4. r = y – x*w;

  5. scale=1;

  6. %optional I can compute the scale using MED

  7. % scale = median(abs(r – median(r)))/0.6745;

  8. cvg = 1;%convergence

  9. while (cvg > 1e-5)

  10. r = r/scale;

  11. wf = w; %save w

  12. WH=wfun(r,k);%diff(rho(xc)/x)

  13. % do weighted least-squares

  14. yst = y.*sqrt(WH);

  15. xst = matmul(x,sqrt(WH));

  16. w = xst\yst;

  17. %the new residual

  18. r = y – x*w;

  19. % compute the convergence

  20. cvg = max(abs(w-wf)./abs(wf));

  21. end;

  22. function W=wfun(r,k)

  23. W=zeros(length(r),1);

  24. for i=1:length(r)

  25. if (r(i)>=-(k-1)) && (r(i)<=k)

  26. W(i)=1;

  27. elseif r(i)<-(k-1)

  28. W(i)=(k-1)4/(r(i)4);

  29. else

  30. W(i)=k4/(r(i)4);

  31. end

  32. end</span>

“wfun”函数为
《[转载]robustfith函数-最小二乘估计-M估计-Robust regression》 image

,其中
《[转载]robustfith函数-最小二乘估计-M估计-Robust regression》 image

。并且
《[转载]robustfith函数-最小二乘估计-M估计-Robust regression》 image

另外,http://blog.csdn.net/abcjennifer/article/details/7449435#M-estimator M估计法 用于几何模型建立

博客中对M估计法有蛮好的解释。

    原文作者:天之道天知道
    原文地址: https://www.jianshu.com/p/cec1ea35de48
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞