[R] Missing data?

Kevin Burton rkevinburton at charter.net
Tue Nov 22 23:41:47 CET 2011


Also with to.weekly there seems to be a problem with when the week starts.
For example:

>xts.ts <- xts(1:4, c(as.Date("2011-01-01"), as.Date("2011-01-10"),
as.Date("2011-10-09"), as.Date("2011-10-10")), frequency=52)
> to.weekly(xts.ts)
           xts.ts.Open xts.ts.High xts.ts.Low xts.ts.Close
2011-01-01           1           1          1            1
2011-01-10           2           2          2            2
2011-10-09           3           3          3            3
2011-10-10           4           4          4            4

> xts.ts <- xts(1:4, c(as.Date("2011-01-01"), as.Date("2011-01-02"),
as.Date("2011-10-09"), as.Date("2011-10-10")), frequency=52)
> to.weekly(xts.ts)
           xts.ts.Open xts.ts.High xts.ts.Low xts.ts.Close
2011-01-02           1           2          1            2
2011-10-09           3           3          3            3
2011-10-10           4           4          4            4

So in the first case the week ends on January 1st. But the second indicates
that the end of the week is the 2nd but it includes the data from the first.
I would expect that the first column should be consistent.
Notice that 10-09 and 10-10 are properly considered different weeks because
the 9th is a Sunday and the 10th is a Monday (the beginning of the week).

-----Original Message-----
From: R. Michael Weylandt <michael.weylandt at gmail.com>
[mailto:michael.weylandt at gmail.com] 
Sent: Tuesday, November 22, 2011 3:10 PM
To: Kevin Burton
Cc: <r-help at r-project.org>
Subject: Re: [R] Missing data?

Couldn't you use seq.Date() to set up the time index and then just fill as
appropriate?

Alternatively, to.weekly if you are starting with a daily series. 

Michael

On Nov 22, 2011, at 4:00 PM, "Kevin Burton" <rkevinburton at charter.net>
wrote:

> I was wondering what the best approach is for missing data in a time
series.
> I give an example using xts but I would like to know what seems to be 
> the "best" method. Say I have
> 
> 
> 
> library(xts)
> 
> xts.ts <- xts(1:4,as.Date(c("1970-01-01", "1970-1-3", "1980-10-10", 
> "2007-8-19")), frequency=52)
> 
> 
> 
> I would like to turn this into a time series (still could be xts, or 
> converted to ts) that has values for every week starting with the week 
> that includes the start date and ending with the week that includes the
end date.
> If there is data for the week then use it otherwise set it to NA or 0.
> Remember some years have 52, 53, or rarely 54 full or partial weeks. 
> What to do with the partials at the beginning and ending of the year? 
> This seems to be a fairly common problem and doing it myself is very 
> cumbersome. Does a solution to this kind of problem exist? Once the 
> approach to a weekly period is found I am sure that adjustment to 
> daily, monthly, or quarterly would be relatively straightforward.
> 
> 
> 
> Thank you.
> 
> 
> 
> Kevin
> 
> 
> 
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list