From: Nathan on
I am trying to fit a line to my data points, and while polyfit and regstats will easily fit a line, it may not be physically relevant. How do I edit these functions so they will fit a regression line with a positive slope.

If you care, here is some sample data.
x=[282.2540 285.8649 253.2350 271.8654 293.8727 293.8727 106.1968 226.1100];
y=[104.8101 116.0248 112.0172 106.1792 117.0507 64.0306 115.3988 102.3172]

And I know the line should be positive, but polyfit generates a line with negative slope.
From: Matt J on
"Nathan " <ndn3(a)georgetown.edu.remove.this> wrote in message <hs9pck$618$1(a)fred.mathworks.com>...
> I am trying to fit a line to my data points, and while polyfit and regstats will easily fit a line, it may not be physically relevant. How do I edit these functions so they will fit a regression line with a positive slope.
>
> If you care, here is some sample data.
> x=[282.2540 285.8649 253.2350 271.8654 293.8727 293.8727 106.1968 226.1100];
> y=[104.8101 116.0248 112.0172 106.1792 117.0507 64.0306 115.3988 102.3172]
>
> And I know the line should be positive, but polyfit generates a line with negative slope.
===================


Fit using the following parametrized line

y(x)=m^2*x+b

and the objective function

f(m,b)=sum (m^2*x(i)+b-y(i) )^2

Sett the gradient of f to zero and solve for m and b.
From: Nathan on
I should have been more specific. While m and b are important, I'd also like to get the MSE, p, and r values for the fit. Ideally, I was looking for a model option for regstats that let me add constraints to the coefficients. Something like regstats(X,Y,[>0]).
From: Roger Stafford on
"Nathan " <ndn3(a)georgetown.edu.remove.this> wrote in message <hs9pck$618$1(a)fred.mathworks.com>...
> I am trying to fit a line to my data points, and while polyfit and regstats will easily fit a line, it may not be physically relevant. How do I edit these functions so they will fit a regression line with a positive slope.
>
> If you care, here is some sample data.
> x=[282.2540 285.8649 253.2350 271.8654 293.8727 293.8727 106.1968 226.1100];
> y=[104.8101 116.0248 112.0172 106.1792 117.0507 64.0306 115.3988 102.3172]
>
> And I know the line should be positive, but polyfit generates a line with negative slope.
- - - - - - - -
Nathan, if you are using least sum of squares as a criterion for best fit, the data you have given is best fit using a line with negative slope. It is a fact that is very easily demonstrated mathematically. If you wish to constrain the slope to non-negative values, then the best slope in that least squares sense would be a slope of zero. That is the value you would get if you were to minimize the objective function Matt described while restricting slope m to real values.

If these results are not in accordance with your needs, it would be necessary to define a different criterion for best fit.

Roger Stafford
From: Matt J on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hsa4uj$6mv$1(a)fred.mathworks.com>...
If you wish to constrain the slope to non-negative values, then the best slope in that least squares sense would be a slope of zero. That is the value you would get if you were to minimize the objective function Matt described while restricting slope m to real values.
====================

That is indeed the result I got when I performed the function minimization for the given data. It would not be the case for all data, however.