[R] ts instead of xts object

Pascal Oettli kridox at ymail.com
Tue Mar 11 13:02:45 CET 2014


Hello,

On Tue, Mar 11, 2014 at 8:45 PM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
> On Tue, Mar 11, 2014 at 12:14 AM, Bill <william108 at gmail.com> wrote:
>>
>> Hello. I have a dataframe that has a date column. The intervals between
>> dates vary. I want to convert this to a ts object. I was able to convert it
>> to an xts object but the package I want to analyse this data with (called
>> 'changepoint') does not seem to want to deal with xts. In the example they
>> give they use the following:
>>
>> data(discoveries)
>> dis.pelt=cpt.meanvar(discoveries,test.stat='Poisson',method='PELT')
>> plot(dis.pelt,cpt.width=3)
>> cpts.ts(dis.pelt)
>>
>> and if I check:
>> str(discoveries)
>>  Time-Series [1:100] from 1860 to 1959: 5 3 0 2 0 3 2 3 6 1 ...
>>
>> If I try with my data
>> str(testTSRad)
>> An 'xts' object on 2011-07-16 07:08:02/2013-09-20 01:25:48 containing:
>>   Data: num [1:501, 1] 76 77 79 86 79 79 85 86 89 88 ...
>>   Indexed by objects of class: [POSIXct,POSIXt] TZ:
>>   xts Attributes:
>>  NULL
>>
>> where I used this:
>>
>> testTSRad=xts(radSampPerRegion[[2]][
>> ,2],order.by=as.POSIXct(radSampPerRegion[[2]][
>> ,1]))
>>
>> I get this:
>>
>> testt=cpt.mean(testTSRad)
>> Error in single.mean.norm(data, penalty, pen.value, class, param.estimates)
>> :
>>   Data must have atleast 2 observations to fit a changepoint model.
>>
> This is because of what ?cpt.mean says about the "data" argument:
> data: A vector, ts object or matrix containing the data within
>       which you wish to find a changepoint.  If data is a matrix,
>       each row is considered a separate dataset.
>
> An xts object is a matrix (with an index attribute), so each row is
> considered a separate data set.  Your object only has one column,
> hence only one observation per data set.  Things will work if you drop
> the dimensions of your single-column xts object:
> testt <- cpt.mean(drop(testTSRad))
>
>> My data is below. Is there a way to convert it to ts?
>>
> Yes, as is generally the case, use the "as" method:
> as.ts(testTSRad)
>

But in this case, the time serie will have a frequency of 1, which is
inconsistent with irregular sampling. This probably will lead to
inaccurate results

> Best,
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com
>
> ______________________________________________
> 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.

Regards,
Pascal

-- 
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan




More information about the R-help mailing list