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

时间:2016-10-28 00:44:21

标签: matlab curve-fitting

我按如下方式对数据进行拟合:

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

ft = fittype( 'poly1' );

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

fitresult var是:

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

如何提取这些边界?

1 个答案:

答案 0 :(得分:0)

这就是我解决它的方法:

%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)));

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

[intervals] = confint(fitresult);

这导致2乘2矩阵,保持p1p2的上限和下限。