This repository contains an R package called trendr
, which fits a locally-linear state-space model (LLM-SSM) to time series data. trendr
has been used on a number of research projects at the Data Science Campus, Office for National Statistics including pygrams.
The easiest method is to install direct from this GitHub repository using:
library(devtools)
install_github("datasciencecampus/trendr/trendr")
Otherwise, you can pull this repository and install locally using:
install("path/to/trendr/dir")
If neither method above works, or if you wish to make changes to the package, then you will need to build it. Building trendr
requires devtools
and roxygen2
:
# R
install.packages("devtools")
install.packages("roxygen2")
Then:
build("path/to/trendr/dir")
install("path/to/trendr/dir")
Once you have installed trendr
using RStudio you can now start using it in RStudio.
All time series data must be loaded into R as a data frame comprising of at least two columns:
- The first column is an indexing variable for the values in the second column. The values can be, for example, a numeric sequence (e.g. 1, 2, 3...) or sequential datetime column ("2020/01/01", "2020/01/02", "2020/01/03" ...).
- The second column contains the values of the process you want to extract the trend of. It is numeric (e.g. 43, 55, 76...)
The data must be equally spaced (e.g. daily, quarterly, yearly data) with no missing values. The CSV file must contain headers (column names). The header names can be specified in trendr()
using the value.colname
and time.colname
parameters.
As with any R package, it can be loaded in an R session using:
#R
library(trendr)
Then you can use the trendr
function:
trendr(
df,
value.colname,
time.colname,
output.save
output.dir,
output.file,
output.plot
)
We have also included two lighweight functions trendr_mu
and trendr_beta
to calculate the trend and first derivative, respectively.
trendr_mu(seq)
trendr_beta(seq)
In this package we have included a number of sample data sets. Please see the vignettes folder for analysis of them.