MATLAB中约束条件下线性曲线拟合的置信区间

时间:2016-06-08 19:24:44

标签: matlab constraints curve-fitting confidence-interval

我使用MATLAB中的函数lsqlin,在线条通过(x0,y0)的约束条件下,在68个样本的数据集上拟合了一条直线。我怎样才能找到这个的置信区间?

我的代码(Source):

我从mat文件导入包含x和y向量的数据集,该文件还包含约束x0和y0的值。

n = 1; % Degree of polynomial to fit
V(:,n+1) = ones(length(x),1,class(x)); %V=Vandermonde matrix for 'x'
for j = n:-1:1
     V(:,j) = x.*V(:,j+1);
end
d = y; % 'd' is the vector of target values, 'y'.
% There are no inequality constraints in this case, i.e., 
A = [];b = [];
% We use linear equality constraints to force the curve to hit the required point. In
% this case, 'Aeq' is the Vandermoonde matrix for 'x0'
Aeq = x0.^(n:-1:0);
% and 'beq' is the value the curve should take at that point
beq = y0;
%% 
[p, resnorm, residual, exitflag, output, lambda] = lsqlin(V, d, A, b, Aeq, beq);
%%
% We can then use POLYVAL to evaluate the fitted curve
yhat = polyval( p, x );

1 个答案:

答案 0 :(得分:0)

使用lsqlin时,函数bootci可用于查找置信区间。以下是它的使用方法:

ci=bootci(68,{@(x,y)func(x,y),x,y},'type','student');

第一个参数是数据点的数量,或矢量x的长度。

第二个参数中的函数基本上应该计算您需要找到置信区间的任何统计量。在这种情况下,此统计数据是我们拟合线的系数。因此,函数func(x,y)在这里应该返回lsqnonlin返回的回归系数。此函数的输入是数据集向量x和y。

第三个和第四个参数允许您指定数据集的分布。您可以通过绘制残差的直方图来了解这一点:

histogram(residuals);