フィルターのクリア

fitline not displaying correct x values datetime

6 ビュー (過去 30 日間)
NOtway
NOtway 2023 年 5 月 26 日
コメント済み: Peter Perkins 2023 年 6 月 5 日
I an attempting to plot a linear OLS regression line from a timetable variable that I have, however somewhere along the way the x values are getting distorted so when I plot the graph they arent at the correct date/datenum (extremely far off).
Here is what I have so far:
dates = datenum(Timetable.Time);
myfitD = polyfit(dates, Timetable.Var1, 1);
fitline = polyval(myfitD, dates);
plot(fitline);
I need to be able to add this line to an existing plot where the X axis is in datetime format.
"Timetable variable" has dates ranging from 01-Jan-2015 to 01-Jan-2099, but based on the graph its currently producing it looks to me as if the fitline is showing the correct y values at an x range of [15 90] which I dont know how to convert to be able to add to this other graph.
I've used this same method before with a similar dataset and it worked just fine, so I'm not sure what the issue is.
For additional context, the graph I am attempting to add it to is plotted as follows:
Years = {'01-jan-2030'; '01-jan-2050'; '01-jan-2070'; '01-jan-2090'};
Xaxis = datetime(Years);
scatter(Xaxis, Yvar);
x1 = datetime('01-Jan-2020');
x2 = datetime('01-Jan-2099');
xlim ([x1, x2]);

採用された回答

Star Strider
Star Strider 2023 年 5 月 26 日
You probably need to do centreing and scaling with your data, then use datetick to display the dates correctrly —
dates = datenum(Timetable.Time);
[myfitD,S,mu] = polyfit(dates, Timetable.Var1, 1);
fitline = polyval(myfitD, dates, S, mu);
figure
plot(dates, fitline)
datetick('x', 'keepticks', 'keeplimits')
See the dateFormat documentation section for those details.
.
  1 件のコメント
Peter Perkins
Peter Perkins 2023 年 6 月 5 日
datenums and datetick are not the best thing to use, but there is a version of the same idea that would use datetime: get dates as something like
dates = Timetable.Time - mean(Timetable.Time);
and then you should just be able to add Timetable.Time,fitline to the scatter plot that used a datetime axis.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by