How to turn (interpolate) this irregularly spaced time series into a regularly spaced one in R or Matlab? -
i have following data:
lines = "20/03/2014,9996792524 21/04/2014,8479115468 21/09/2014,11394750532 16/10/2014,9594869828 18/11/2014,10850291677 08/12/2014,10475635302 22/01/2015,10116010939 26/02/2015,11206949341 20/03/2015,11975140317 09/04/2015,11526960332 29/04/2015,9986194500 16/09/2015,11501088256 13/10/2015,11833183163 10/11/2015,13246940910 16/12/2015,13255698568 27/01/2016,13775653990 23/02/2016,13567323648 22/03/2016,14607415705 11/04/2016,13835444224 04/04/2016,14118970743"
i read r:
z <- read.zoo(text = lines, sep = ",", header = true, index = 1:1, tz = "", format = "%d/%m/%y")
i wish interpolate data such can convert irregularly spaced time series regular one. time interval not matter long it's regular monthly, weekly, or bi-weekly interval do.
how do in r
or matlab
?
note: realize interpolated values might not accurate , might misrepresent information, need learn how , i'm alright loosing accuracy.
ok, first of all, word of warning: if you're going interpolate , perform tests or generic statistical estimation, results (badly) biased, unless have reasons (domain knowledge?) assume interpolation method generate points coming same distribution of original points. , no, "the plot looks good" not criterion assess :) having being said, let's have @ data:
# lines contains data library(zoo) fmt <- "%d/%m/%y" z <- read.zoo(text = lines, sep = ",", header = true, index = 1:1, tz = "", format = fmt) t <- time(z) plot(z,type="p",xaxt="n",pch=19,col="cyan",cex=1.5) labs <- format(t,fmt) axis(side = 1, @ = t, labels = labs,cex.axis = 0.7)
it looks of missing data pertain summer 2014 , summer 2015. i'm curious know these data are...anyway, looks of data spaced @ least 2 weeks:
diff(t) # time differences in days # [1] 153 25 33 20 45 35 22 20 20 140 27 28 36 42 27 28 13 7
thus let's interpolate biweekly series creating first dummy zoo
object:
t.biweekly <- seq(from = min(t), to=max(t),by="2 weeks") dummy <- zoo(,t.biweekly)
merge dummy series yours:
z.interpolated <- merge(z,dummy,all=true)
if @ new series, you'll see there na values @ times of dummy
don't have corresponding time in z
. let's fill points linearly interpolated values , @ result:
z.interpolated <- na.approx(z.interpolated) plot(z.interpolated, type = "b") points(z,pch=19,col="cyan",cex=1.5)
voilĂ ! remember building models inference out of thing bad idea...
Comments
Post a Comment