Shruti
Shruti

Reputation: 159

Confidence intervals for linear curve fit under constraints in MATLAB

I have fitted a straight line to a dataset with 68 samples, under the constraint that the line passes through (x0,y0) using the function lsqlin in MATLAB. How can I find the confidence intervals for this?

My code (Source):

I import the dataset containing x and y vectors from a mat file, which also contains the values of constraints x0 and 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 );

Upvotes: 2

Views: 581

Answers (1)

Shruti
Shruti

Reputation: 159

The function bootci can be used to find confidence intervals when using lsqlin. Here's how it can be used:

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

The first argument is the number of data points, or the length of the vector x.

The function in the second argument is basically supposed to compute any statistic for which you need to find the confidence intervals. In this case, this statistic is the coefficients of our fitted line. Hence, the function func(x,y) here should return the regression coefficients returned by lsqnonlin. The inputs to this function are the dataset vectors x and y.

The third and fourth argument lets you specify the distribution of your dataset. You can get an idea of this by plotting a histogram of the residuals like this:

histogram(residuals);

Upvotes: 0

Related Questions