Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fit to time series using Gnuplot

I am a big fan of Gnuplot and now I would like to use the fit-function for time series.

My data set is like:

1.000000 1.000000 0.999795 0.000000 0.000000 0.421927 0.654222 -25.127700 1.000000 1994-08-12
1.000000 2.000000 0.046723 -0.227587 -0.689491 0.328387 1.000000 0.000000 1.000000 1994-08-12 
2.000000 1.000000 0.945762 0.000000 0.000000 0.400038 0.582360 -8.624480 1.000000 1995-04-19 
2.000000 2.000000 0.060228 -0.056367 -0.680224 0.551019 1.000000 0.000000 1.000000 1995-04-19
3.000000 1.000000 1.016430 0.000000 0.000000 0.574478 0.489638 -3.286880 1.000000 1995-07-15

And my fitting script:

set timefmt "%Y-%m-%d"
set xdata time
set format x "%Y-%m-%d"
f(x)=a+b*x
fit f(x) "model_fit.dat" u 10:($2==2?$4:1/0) via a,b

So I make a conditional fitting to time data. My problem is, that the Gnuplot fit function doesn't work on time data. I found a similar question here: Linear regression for time series with Gnuplot but I don't want to use other software. And I also don't know how to change time values to numbers, and then back again....

Can anyone help me solving this with Gnuplot?

Thanks a lot!

like image 392
rstofi Avatar asked Mar 16 '23 13:03

rstofi


1 Answers

Indeed, gnuplot's fitting mechanism works fine for time-data. You must only pay attention to some details.

In general, a linear fit through two data points can be solved exactly. But gnuplot does generally a nonlinear fit, so the initial values are important.

The effective x-value used for the line is given in seconds. Using an initial value of b = 1e-8 works fine here:

set timefmt "%Y-%m-%d"
set xdata time
set format x "%Y-%m-%d"
f(x)=a+b*x
a = 1
b = 1e-8
fit f(x) "model_fit.dat" u 10:($2==2?$4:1/0) via a,b

plot "model_fit.dat" u 10:($2==2?$4:1/0) lt 1 pt 7 title 'data',\
     f(x) w l lt 1 title 'fit'

enter image description here

like image 140
Christoph Avatar answered Mar 30 '23 23:03

Christoph