Using polyfit and polyval functions with data

3 ビュー (過去 30 日間)
jacob Mitch
jacob Mitch 2019 年 11 月 17 日
コメント済み: jacob Mitch 2019 年 11 月 17 日
I've been using the polyfit and polyval functions recently and I would like to approximate data between 2 vectors but I'm wondering if Ive made a mistake somewhere.
If
a=[1;2;3;4;5;nan;6;nan;nan;10];%prices with data missing
b=[1;2;3;4;5;6;6;7;8;10]; %original prices
days=[1;2;3;4;5;6;7;8;9;10]%number of days
NotMissing=~isnan(a(:,1));
p=a(NotMissing,1); %prices with data not missing
days=a(NotMissing,1);
pf=polyfit(days,p,1);
pv=polyval(pf,days); %polyval approximation of length 7 compared to b which has length 10
%I've want to calculate the mean square error between the approximate prices pv and the original prices using
%E = sqrt( sum( (b-pv).^2) / numel(b) );
Have I made a mistake using the polyval function, in this case I cannot calcuate E because the vectors are different lengths so I'm wondering if Im using the polyfit and polyval functions incorrectly because my length is being reduced by the number of nan values. Or am I 'calculating the error ' between the 2 vectors incorrectly

採用された回答

Matt J
Matt J 2019 年 11 月 17 日
編集済み: Matt J 2019 年 11 月 17 日
To do what you were after in your post, you need to fit with the NotMissing data, but then apply the fit at all days,
a=[1;2;3;4;5;nan;6;nan;nan;10];%prices with data missing
b=[1;2;3;4;5;6;6;7;8;10]; %original prices
days=[1;2;3;4;5;6;7;8;9;10];%number of days
NotMissing=~isnan(a);
pf=polyfit(days(NotMissing),a(NotMissing),1);
pv=polyval(pf,days)
  1 件のコメント
jacob Mitch
jacob Mitch 2019 年 11 月 17 日
Hi thanksMatt, I think this is it, I was trying to do a linear approximation which you have done above the pv you achieved is the same vector length as b allowing for E to be calculated. I think I was fitting my data wrong as you suggested. If you'd be so kind I just wanted to check Am I on the right track for interpt1 with something like
a=[1;2;3;4;5;nan;6;nan;nan;10];%prices with data missing
days=[1;2;3;4;5;6;7;8;9;10];
b=[1;2;3;4;5;6;6;7;8;10]; %number of days
NotMissing=~isnan(a);
p=a(NotMissing,1);
days2=days(NotMissing,1);
x=[days2(1):1:days2(end)]
y=interp1(days2,p,x,'spline');

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2019 年 11 月 17 日
編集済み: Matt J 2019 年 11 月 17 日
I wonder if what you are really trying to do is a fillmissing operation, instead of a linear fit to the data,
a=[1;2;3;4;5;nan;6;nan;nan;10];%prices with data missing
days=[1;2;3;4;5;6;7;8;9;10]%number of days
pv=fillmissing(a,'linear','SamplePoints',days)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by