[R] POSIXct to ts

Pascal Oettli kridox at ymail.com
Fri Aug 10 10:37:58 CEST 2012


Hello,

decompose() works with moving averages to define the components of the 
time series.

stl() uses loess (Local Polynomial Regression Fitting) to define the 
components.

Regards,
Pascal



Le 10/08/2012 13:02, Mary Ann Middleton a écrit :
>
> Hi Michael,
>
> Thank you so much for your email.  That really helped, and with a frequency of 24, I finally have some figures I can work with!!
>
> I have a follow up question .  Do you have any resources you would recommend that would explain the difference between stl() and decompose()?  I have been reading the help files, and googling, but I am not finding the right resources.
>
> There are some notable difference between the "random" and the "remainder" in each and I am unsure which  is correct for my purposes.
>
> At this time, I can use the "random" calculation from decompose() to generate an acf of the data after the seasonal patterns and trends are stripped
> ( ac f ( n a.omit(x.ts.decomp$random) ) ,
> which is ultimately what I need, however, that isn't solid justification for choosing that calculation.
>
> Any pointers appreciated.
>
> Cheers,
> Mary Ann
>
> ----- Original Message -----
>
> From: "R. Michael Weylandt" <michael.weylandt at gmail.com>
> To: "Mary Ann Middleton" <maberg at sfu.ca>
> Cc: r-help at r-project.org
> Sent: Thursday, August 9, 2012 2:51:16 PM
> Subject: Re: [R] POSIXct to ts
>
> On Thu, Aug 9, 2012 at 3:30 PM, Mary Ann Middleton <maberg at sfu.ca> wrote:
>>
>> Hi,
>>
>> I have a dataframe (try.1) with  date/time and temperature columns, and the date/time is in POSIXct fomat. Sample included below.
>>
>> I would like to to try decompose () or stl() to look at the trends and seasonality in my data, eventually so that  I can  look at autocorrelation.  The series is 3 years of water temperature with clearly visible seasonal periods.
>>
>> Right now, if I try decompose, I get the following error, w hich I beleive is because I haven't correctly defined a time series.
>> "Error in decompose(try.1) : time series has no or less than 2 periods"
>>
>> I am stuck trying to go from POSIXct to as.ts
>> Any suggestions on how to tackle this would be greatly appreciated.
>>
>> Sincerely,
>> Mary Ann
>>
>> A sample of the data looks like this:
>> 'data.frame':   26925 obs. of  2 variables:
>>   $ date      : POSIXct, format: "2008-07-11 21:00:00" "2008-07-11 22:00:00" ...
>>   $ DL_1297699: num  15.3 15.1 14.9 14.6 14.1 ...   date DL_1297699 1    2008-07-11 21:00:00     15.318
>> 2       2008-07-11 22:00:00     15.127
>> 3       2008-07-11 23:00:00     14.888
>> 4       2008-07-12 00:00:00     14.553
>> 5       2008-07-12 01:00:00     14.146
>> 6       2008-07-12 02:00:00     13.738
>> 7       2008-07-12 03:00:00     13.401
>> 8       2008-07-12 04:00:00     13.088
>> 9       2008-07-12 05:00:00     12.823
>> 10      2008-07-12 06:00:00     12.630 and the dput(head(x,50) gives this output: structure(list(date = structure(c(1215810000, 1215813600, 1215817200,
>> 1215820800, 1215824400, 1215828000, 1215831600, 1215835200, 1215838800,
>> 1215842400, 1215846000, 1215849600, 1215853200, 1215856800, 1215860400,
>> 1215864000, 1215867600, 1215871200, 1215874800, 1215878400, 1215882000,
>> 1215885600, 1215889200, 1215892800, 1215896400, 1215900000, 1215903600,
>> 1215907200, 1215910800, 1215914400, 1215918000, 1215921600, 1215925200,
>> 1215928800, 1215932400, 1215936000, 1215939600, 1215943200, 1215946800,
>> 1215950400, 1215954000, 1215957600, 1215961200, 1215964800, 1215968400,
>> 1215972000, 1215975600, 1215979200, 1215982800, 1215986400), class = c("POSIXt",
>> "POSIXct"), tzone = "UTC"), DL_1297699 = c(15.318, 15.127, 14.888,
>> 14.553, 14.146, 13.738, 13.401, 13.088, 12.823, 12.63, 12.461,
>> 12.413, 12.461, 12.703, 13.04, 13.497, 14.026, 14.553, 15.031,
>> 15.366, 15.7, 15.819, 15.819, 15.7, 15.605, 15.461, 15.247, 14.984,
>> 14.673, 14.337, 14.002, 13.666, 13.377, 13.137, 12.944, 12.823,
>> 12.847, 13.016, 13.329, 13.762, 14.242, 14.697, 15.175, 15.581,
>> 15.891, 16.034, 16.034, 15.939, 15.772, 15.581)), .Names = c("date",
>> "DL_1297699"), row.names = c(NA, 50L), class = "data.frame")
>>
>
> Thank you for the dput()-ed data!
>
> The "time series" object that stl() and decompose() expect doesn't
> have time stamps -- rather it has a "start" and "end" marker as well
> as a frequency. [For more details, see ?tsp]
>
> With your described data, I'd imagine you'd have start = 2008 and
> frequency = 365*24 (if you have hourly data and an underlying yearly
> periodicity) but to work with the data you gave, lets suppose 12 hours
> is a cycle. Note you don't have to give end because that's figured out
> automatically from frequency and start.
>
> x.ts <- ts(x[,2], start = 1, frequency = 12)
>
> then I can
>
> stl(x, "per")
> decompose(x)
>
> as desired.
>
> Hope that helps,
> Michael
>
>
> 	[[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