Why is this xts frequency always 1?
Consecutive Date class dates always have a frequency of 1 since consecutive dates are 1 apart. Use ts or zooreg to get a frequency of 7:
tt <- ts(values, frequency = 7)
library(zoo)
zr <- as.zooreg(tt)
# or
zr <- zooreg(values, frequency = 7)
These will create a series whose times are 1, 1+1/7, 1+2/7, ...
If we have some index values of zr
zrdates <- index(zr)[5:12]
we can recover the dates from zrdates
like this:
days[match(zrdates, index(zr))]
As pointed out in the comments xts does not support this type of series.
Understand frequency parameter while converting xts to ts object in R
The frequency
is equivalent to 1/deltat
, where deltat
is the fraction of the sampling period between successive observations. ?frequency
gives the example that deltat
would be "1/12 for monthly data".
In the case of hourly data, deltat
is 3600, since there are 3600 seconds in an hour. Since frequency = 1 / deltat
, that means frequency = 1 / 3600
, or 0.0002777778
.
Get frequency for TS from and XTS for X12
This is untested for this specific case, but try using xts::periodicity
for the frequency:
freq <- switch(periodicity(lax)$scale,
daily=365,
weekly=52,
monthly=12,
quarterly=4,
yearly=1)
And use the year
and mon
elements of POSIXlt
objects to calculate the start year and month.
pltStart <- as.POSIXlt(start(lax))
Start <- c(pltStart$year+1900,pltStart$mon+1)
laxts <- ts(lax$WILACR3URN, start=Start, frequency=freq)
plot.ts(laxts)
decomposition of xts weekly time series in R
Hm maybe try a different value for frequency. From the ts
help page:
"The value of argument frequency is used when the series is sampled an integral number of times in each unit time interval. For example, one could use a value of 7 for frequency when the data are sampled daily, and the natural time period is a week, or 12 when the data are sampled monthly and the natural time period is a year. Values of 4 and 12 are assumed in (e.g.) print methods to imply a quarterly and monthly series respectively."
So for your data the following might work
decompose(ts(x,frequency = 4))
Or
decompose(ts(x,frequency = 52))
as weekly
How to transform xts to lower frequency in a cumulative way
Split on "weeks", apply a custom function to each week's data, then rbind the results
to.weekly.cumulative <- function(xts.obj, name="") {
out <- do.call(rbind,
lapply(split(xts.obj, 'weeks'),
function(x) cbind(rep(first(x[,1]), NROW(x[,1])),
cummax(x[,2]), cummin(x[,3]), x[,4])))
colnames(out) <- paste(name, c("Open", "High", "Low", "Close"), sep=".")
out
}
> library(quantmod)
> data(sample_matrix)
> myxts <- as.xts(sample_matrix)
> head(to.weekly.cumulative(myxts), 15)
.Open .High .Low .Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.03978 50.42188 49.95041 50.39767
2007-01-04 50.03978 50.42188 49.95041 50.33236
2007-01-05 50.03978 50.42188 49.95041 50.33459
2007-01-06 50.03978 50.42188 49.95041 50.18112
2007-01-07 50.03978 50.42188 49.95041 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806
2007-01-09 50.03555 50.10363 49.80454 49.91333
2007-01-10 50.03555 50.13053 49.80454 49.97246
2007-01-11 50.03555 50.23910 49.80454 50.23910
2007-01-12 50.03555 50.35980 49.80454 50.28519
2007-01-13 50.03555 50.48000 49.80454 50.41286
2007-01-14 50.03555 50.62395 49.80454 50.60145
2007-01-15 50.61724 50.68583 50.47359 50.48912
2007-01-16 50.61724 50.73731 50.47359 50.67835
Coerce xts to ts in R
library(xts)
library(ggplot2)
library(reshape2)
set.seed(42)
timevalues = "20150101 0000/20150110 2359"
timesequence <- timeBasedSeq(timevalues)
min_data <- xts(rnorm(14400),timesequence)
ts_data <- ts(as.numeric(min_data), frequency = 1440)
out <- stl(ts_data, s.window = "per")
time.series <- as.data.frame(cbind(ts_data, out$time.series))
colnames(time.series) <- c("Data", "Seasonal", "Trend", "Remainder")
time.series$Date <- timesequence
time.series <- melt(time.series, 'Date')
ggplot(time.series, aes(x=Date, y=value)) +
geom_line() +
facet_free(variable~.)
Related Topics
What Is the "Embracing Operator" '{{ }}'
How to Run a Function Every Second
Pivot_Longer Multiple Variables of Different Kinds
How to Get Covariance Matrix for Random Effects (Blups/Conditional Modes) from Lme4
Change Background Colour of Knitr::Kable Headers
Labelling the Plots with Images on Graph in Ggplot2
How to Show a Loading Screen When the Output Is Being Calculated in a Background Process
Calculating Prediction Accuracy of a Tree Using Rpart's Predict Method
R: Clustering Results Are Different Everytime I Run
How to Flatten R Data Frame That Contains Lists
As.Posixct Gives an Unexpected Timezone
Unzip Password Protected Zip Files in R
Rmarkdown Removes Citation Hyperlink
Rstudio Shiny Not Able to Use Ggvis
Exporting Multiple Panels of Plots and Data to *.Png (In the Style Layout() Works Within R)