Title: | Time Frame Coding Kernel Extensions |
---|---|
Description: | Extensions and additional 'tframe' utilities. |
Authors: | Paul Gilbert <[email protected]> |
Maintainer: | Paul Gilbert <[email protected]> |
License: | GPL-2 |
Version: | 2024.2-1 |
Built: | 2024-11-14 03:28:21 UTC |
Source: | https://github.com/r-forge/tsanalysis |
Convert series to quarterly or annual.
as.quarterly(x, FUN=sum, na.rm=FALSE, ...) as.annually(x, FUN=sum, na.rm=FALSE, ...) as.weekly(x, FUN=sum, na.rm=FALSE, foldFrom=end(x), periodicity = 7)
as.quarterly(x, FUN=sum, na.rm=FALSE, ...) as.annually(x, FUN=sum, na.rm=FALSE, ...) as.weekly(x, FUN=sum, na.rm=FALSE, foldFrom=end(x), periodicity = 7)
x |
a tframed object. (Only montly is currently working) |
FUN |
the function to use for aggregating. |
na.rm |
Logical indicating if |
foldFrom |
a date which is used to determine the end of weeks. |
periodicity |
the number of periods in a week. |
... |
additional arguments passed to |
Functions as.quarterly
and as.annually
uses aggregate, but
shifts the data to match usual economic and
financial quarters or years (whereas aggregate simply groups together a number
of periods corresponding to the new frequency starting with the first
observation). These functions should give the same result as
aggregate if the data starts and ends on quarter (annual) boundaries.
If the data does not start and end on quarter (annual) boundaries
then NA
will be put in where
data is incomplete, and the quarter (year) removed if na.rm=TRUE
.
Monthly to annual gives the aggregate by converting first to quarterly, which is not exactly correct.
To weekly by periodicity
groupings from foldFrom
.
Partial weeks in the beginning or end are padded with NA
and
dropped if na.rm=FALSE
.
(as.weekly has been tested only with daily 7 day weeks.)
time series
z <- ts(1:10, start = c(1999,2), frequency=4) as.annually(z) as.annually(z, na.rm=TRUE) z <- ts(1:30, start = c(1999,2), frequency=12) as.annually(z) as.annually(z, na.rm=TRUE) as.quarterly(z) as.quarterly(z, na.rm=TRUE)
z <- ts(1:10, start = c(1999,2), frequency=4) as.annually(z) as.annually(z, na.rm=TRUE) z <- ts(1:30, start = c(1999,2), frequency=12) as.annually(z) as.annually(z, na.rm=TRUE) as.quarterly(z) as.quarterly(z, na.rm=TRUE)
Change the class of a (Multivariate) Time Series
changeTSrepresentation (x, newRepresentation)
changeTSrepresentation (x, newRepresentation)
x |
a multivariate time series object. |
newRepresentation |
function or character string indicating the function to use to change the time series class. |
This function attempts to convert the time series to the new class using
newRepresentation
.
If the representation is a character string then it is applied using do.call(TSrepresentation, list(x, dates))
where x
is the time series (matrix) to be return and dates are
determined by as.Date(time(x))
where x
is the default representation of the data. If representation is not a character string
then it should be
a function and is applied using TSrepresentation(x, dates)
a multivariate time series object.
Aggregate time series using a rolling window.
rollAggregate(x, FUN=sum, na.rm=FALSE, aggPeriods=4, ...)
rollAggregate(x, FUN=sum, na.rm=FALSE, aggPeriods=4, ...)
x |
a time series object. |
FUN |
the function to use for aggregating. |
na.rm |
Logical indicating if |
aggPeriods |
the number of observations to use in the rolling window. |
... |
additional arguments passed to |
Functions rollAggregate
uses apply with FUN
on a rolling
window set by aggPeriods
.
Partial windows may result in NA
,
depending on FUN
. NA
in the beginning or end are
dropped if na.rm=TRUE
.
The frequency of the resulting series remains the same as the original, but
the result will be aggPeriods
shorter (i.e. have NAs).
The default will be a four period moving sum (which roughly converts weekly
data to something approximating monthly, but at a weekly frequency).
time series
as.annually
,
as.quarterly
,
as.weekly
,
apply
rollAggregate(ts(1:20, start = c(1999,1), frequency=1)) rollAggregate(ts(1:20, start = c(1999,1), frequency=12), aggPeriods=3) if(require("zoo")) { z <- zoo(rnorm(100), order.by = Sys.Date() + 1:100) rollAggregate(as.weekly(z), aggPeriods=4, FUN=mean) require("tfplot") tfplot(as.weekly(z),rollAggregate(as.weekly(z), aggPeriods=4, FUN=mean, na.rm=TRUE)) tfplot(z,rollAggregate(z, aggPeriods=28, FUN=mean)) }
rollAggregate(ts(1:20, start = c(1999,1), frequency=1)) rollAggregate(ts(1:20, start = c(1999,1), frequency=12), aggPeriods=3) if(require("zoo")) { z <- zoo(rnorm(100), order.by = Sys.Date() + 1:100) rollAggregate(as.weekly(z), aggPeriods=4, FUN=mean) require("tfplot") tfplot(as.weekly(z),rollAggregate(as.weekly(z), aggPeriods=4, FUN=mean, na.rm=TRUE)) tfplot(z,rollAggregate(z, aggPeriods=28, FUN=mean)) }
Integrate multivariate time series using cumsum
tfI (x, I=rep(TRUE, NCOL(x)), t0=rep(0, NCOL(x)))
tfI (x, I=rep(TRUE, NCOL(x)), t0=rep(0, NCOL(x)))
x |
a multivariate time series object. |
I |
a logical of length equal the number of series in |
t0 |
a vector of length equal the number of series in |
This function first prepends t0
to x
and then applies
cumsum
to each series indicated by I
. The first value is then
dropped from the result.
a multivariate time series object of the same dimension as x
.
tfL
tfwindow
aggregate
cumsum
as.quarterly
Plot series with persp
tfpersp (x, tf=tfspan(x), start=tfstart(tf), end=tfend(tf), theta = -30, phi = 15, scale = FALSE, xlab = "Time", ylab = "", zlab = "", aspect= c(0.5, 0.5), #y/time, z/time, ticktype="detailed",ltheta = -120, lphi = 15, ...)
tfpersp (x, tf=tfspan(x), start=tfstart(tf), end=tfend(tf), theta = -30, phi = 15, scale = FALSE, xlab = "Time", ylab = "", zlab = "", aspect= c(0.5, 0.5), #y/time, z/time, ticktype="detailed",ltheta = -120, lphi = 15, ...)
x |
multivariate time series (a tframed object). |
tf |
a tframe to set the start and end of the plot. |
start |
start period for the plot. |
end |
end period for the plot. |
theta |
argument passed to |
phi |
argument passed to |
scale |
argument passed to |
xlab |
argument passed to |
ylab |
argument passed to |
zlab |
argument passed to |
aspect |
argument passed to |
ticktype |
argument passed to |
ltheta |
argument passed to |
lphi |
argument passed to |
... |
additional arguments passed to |
This function uses passes a multivariate time series to persp.
Depends
Write one or more time series to a .xls spreadsheet file
TSwriteXLS(x, ..., FileName="R.xls", SheetNames=NULL, dateHeader="date", verbose = FALSE) TSwriteCSV(x, FileName="R.csv", dateFormat=1, dateHeader="date")
TSwriteXLS(x, ..., FileName="R.xls", SheetNames=NULL, dateHeader="date", verbose = FALSE) TSwriteCSV(x, FileName="R.csv", dateFormat=1, dateHeader="date")
x |
a time series or matrix of time series.) |
... |
additional time series objects (like x). |
FileName |
a string to use for the file name. |
SheetNames |
Logical indicating if NA should be removed from the beginning and end of a series. |
dateFormat |
an integer indicating the format to use for dates. (See details.) |
dateHeader |
string or vector of strings to use as header on date column. |
verbose |
logical passed to WriteXLS. |
This functions uses WriteXLS
to write time series data to an .xls spreadsheet
file. If the object x
has mulitple series then they will be put on
the same sheet. Additional objects in ... will be put on additional pages.
The first line of each sheet will have column headers using seriesNames
from the series in the column. The first column will have dates and the
heading for that column will be determined by dateHeader
. This can be a
vector of length equal the number of sheets, of a single character string, in
which case it is replicated for each sheet. The dateHeader
is a
might be useful for indication information like the date of the last
observation in a known place on the sheet (A1) for indicating on a graph.
For convenience, the year, period, and a character
representation of the period are indicated in columns 2 to 4. This is followed
by columns of the data.
If SheetNames
is NULL
(the default) then the sheet names will
be generated from the first series name in each object. If SheetNames
is supplied then it should have an element for x
and for each object
in ....
If WriteXLS
does not find a version of perl with appropriate modules
then a work around is attempted using save
.
WriteCSV
handles only a single object x
which can have mulitple
series. These are wtten as columns in the comma separated value file.
dateFormat
can be 0 indicating no date, 1 indicating a format like
"Jan 1969", 2 indicating a format like 1969,1, or 3 indicating a
format like 1969,"Jan",. Only 0 or 1 are supported for data other than monthly
and quarterly frequencies.
logical
tofile <- tempfile() z <- ts(1:10, start=c(1999,2), freq=12) seriesNames(z) <- "ser 1" TSwriteXLS(z, FileName=tofile) zz <- tbind(z, diff(z)) seriesNames(zz) <- c("ser 1", "diff") TSwriteXLS(zz, FileName=tofile, SheetNames="2 series") zz <- ts(1:10, start=c(1999,1), freq=1) seriesNames(zz) <- "annual ser" TSwriteXLS(z, zz, FileName=tofile, SheetNames=c("monthly", "annual")) unlink(tofile)
tofile <- tempfile() z <- ts(1:10, start=c(1999,2), freq=12) seriesNames(z) <- "ser 1" TSwriteXLS(z, FileName=tofile) zz <- tbind(z, diff(z)) seriesNames(zz) <- c("ser 1", "diff") TSwriteXLS(zz, FileName=tofile, SheetNames="2 series") zz <- ts(1:10, start=c(1999,1), freq=1) seriesNames(zz) <- "annual ser" TSwriteXLS(z, zz, FileName=tofile, SheetNames=c("monthly", "annual")) unlink(tofile)