如何从 fit() 结果 matlab 中提取 95% 置信区间

How to extract 95% confidence bounds from fit() result matlab

我将一行数据拟合如下:

[xData, yData] = prepareCurveData( lnN, lne );

ft = fittype( 'poly1' );

[fitresult, gof] = fit( xData, yData, ft );

其中 fitresult 变量是:

>> fitresult

fitresult = 

     Linear model Poly1:
     fitresult(x) = p1*x + p2
     Coefficients (with 95% confidence bounds):
       p1 =     -0.1331  (-0.1437, -0.1226)
       p2 =      -2.625  (-2.699, -2.552)

但是当我尝试获得 p1 的置信区间时,例如,它只打印平均值:

>> fitresult.p1

ans =

    -0.1331

如何提取这些界限?

我是这样解决的:

%create a string from the output of fit()
out = evalc('fitresult');

%crop out the relevant part of the string (by counting characters)
out = out(145:170);

%search between ( and , for the lower limit
p1_lower = str2num(out((strfind(out,'(')+1):(strfind(out,',')-1)));

%search between , and ) for the upper limit
p1_upper = str2num(out((strfind(out,',')+1):(strfind(out,')')-1)));

EDIT 根据@alexforrence 的建议,我检查了 fitresults 对象有哪些方法。因此,以下解决方案要简单得多:

[intervals] = confint(fitresult);

这将生成一个 2 x 2 矩阵,其中包含 p1p2 的上限和下限。