Code Monkey home page Code Monkey logo

smac-group / simts Goto Github PK

View Code? Open in Web Editor NEW
15.0 10.0 7.0 146.91 MB

:hourglass_flowing_sand: Time Series Tools R package provides a series of tools to simulate, plot, estimate, select and forecast different time series models.

Home Page: https://smac-group.github.io/simts/index.html

License: GNU Affero General Public License v3.0

R 4.79% C++ 42.57% C 0.20% HTML 52.07% CSS 0.24% TeX 0.10% Makefile 0.01% Shell 0.01% M 0.01% MATLAB 0.01% JavaScript 0.01%
timeseries time-series simulation timeseries-data r rcpp rcpparmadillo

simts's Introduction

R-CMD-check Licence minimal R version CRAN CRAN RStudio mirror downloads CRAN RStudio mirror downloads

simts Overview

The Time Series Tools (simts) R package provides a series of tools to simulate, plot, estimate, select and forecast different time series models. Its original purpose was to be a support to the online textbook “Applied Time Series Analysis with R” but can obviously be used for time series analysis in general. More specifically, the package provides tools with the following features:

  • Simulation of time series from SARIMA models to various state-space models that can be expressed as latent time series processes.
  • Visualization of time series data with user specifications.
  • Specific simulation and visualization tools for latent time series models.
  • Easy-to-use functions to estimate and infer on the parameters of time series models through different methods (standard and robust).
  • Diagnostic and statistical tools to assess goodness of fit and select the best model for the data.
  • Estimating and plotting tools to deliver point forecasts and confidence intervals.

To understand the usage of the simts package, please refer to the “Vignettes” tab above.

Install Instructions

Installation

The simts package is available on both CRAN and GitHub. The CRAN version is considered stable while the GitHub version is subject to modifications/updates which may lead to installation problems or broken functions. You can install the stable version of the simts package with:

install.packages("simts")

For users who are interested in having the latest developments, the GitHub version is ideal although more dependencies are required to run a stable version of the package. Most importantly, users must have a (C++) compiler installed on their machine that is compatible with R (e.g. Clang).

# Install dependencies
install.packages(c("RcppArmadillo","devtools","knitr","rmarkdown"))

# Install the package from GitHub without Vignettes/User Guides
devtools::install_github("SMAC-Group/simts")

# Install the package with Vignettes/User Guides 
devtools::install_github("SMAC-Group/simts", build_vignettes = TRUE)

The setup to obtain the development version of simts is platform dependent.

License

The license this source code is released under is the GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) v3.0. Please see the LICENSE file for full text. Otherwise, please consult TLDR Legal or GNU which will provide a synopsis of the restrictions placed upon the code.

simts's People

Contributors

davideacucci avatar lionelvoirol avatar munsheet avatar nathanael-claussen avatar rfaris253 avatar robertomolinari avatar samorso avatar stephaneguerrier avatar theace0296 avatar yunxiang-z avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simts's Issues

Select function

Adapt the select function to the following models:

  • MA
  • ARMA
  • ARIMA
  • SAR
  • SMA
  • SARMA
  • SARIMA

Fix units in plot.gts/ACF/PACF

There is a bug with the lags and xlab when freq != 1 for the functions mentioned in the title. Here is an example:

# Load savingrt dataset
data("savingrt")

# Simulate based on data
savingrt = gts(as.vector(savingrt), start = 1959, freq = 12, unit_ts = "%", 
            name_ts = "Saving Rates", data_name = "US Personal Saving Rates",
            unit_time = "month")

# Plot savingrt simulation
plot(savingrt)

# Compute ACF and plot result
savingrt_acf = ACF(savingrt)
plot(savingrt_acf)

TO DO - Version 0.3.0

  • Prediction and residuals for state space models (@lionelvoirol )
  • Structure output of kalman_filter function in a list of matrix w/ forecast, filter and smooth (@lionelvoirol )
  • Discuss the functions AIC.fitsimts and evaluate in vignette(s) and textbook.
  • Add possibility to change title in Portmanteau Tests plot (see function diag_ljungbox and Figure 4.15).
  • Add summary.fitsimts (connect to gmwm).
  • Add possibility to compute CI by parametric bootstrap, block bootstrap and seeder.
  • Add news.md file
  • Add confidence intervals for parameters estimated with gmwm and rgmwm.
  • Add compatibility with RW, WN, QN and DR processes (as well as sum of latent processes) for the function estimate used with option gmwm and rgmwm.
  • Add possibility of simulating non-Gaussian time series (i.e. extent ts objects to contain some info on the distribution of the residuals, for example AR(1) would be Gaussian AR(1) while AR(1, df = 5) would an AR(1) with t-distributed residuals with 5 df.
  • Add an example of an unevenly spaced time series in the README and/or vignette. The code may need to be adapted.
  • The select function should be made compatible with the following models:
    • SAR and SMA
    • SARMA
    • SARIMA
  • Improve the function RW3dimensions. In particular, these parameters should be added as inputs: couleur = "blue4", xlab = "X-position", ylab = "Y-position", main = NULL, pt_col = NULL, pt_pch = 16, pt.cex = 2, leg_pos = NULL.
  • Add MAPE model selection features (See exts for details. Here is an example based on sunspot dataset, slide 38 - ARMAmodels.pdf)

screen shot 2018-10-28 at 5 50 53 pm

  • Add a check that supplied object is of class gts or fitsimts (e.g. predict() function in the stat environment takes non-compatible objects and can be used instead of the predict() function in simts)
  • Correct kernel density of the residual hist can go outside of the graph

screen shot 2018-10-29 at 12 25 47 am

  • Remove negative values (y axis) of Ljung-Box diagnostic plot see e.g.
n = 1000
model = AR(phi = c(0.9, -0.5), sigma2 = 1)
Xt = gen_gts(n, model, freq = 4)
plot(Xt)

model1 = estimate(AR(1), Xt)
predict(model1, n.ahead = 10)

check(model1)
  • Add diagnostic function based on wv. Suggested usage (probably after the release of wv package:
plot(wvar(res), main = "Haar WVar Representation", legend_position = NA)
  
  sigma2 = rep(var(res), length(wvar(res)$scales))
  points(wvar(res)$scales, sigma2/as.numeric(wvar(res)$scales), col = "orange", pch=0, cex=2)
  lines(wvar(res)$scales, sigma2/as.numeric(wvar(res)$scales), col = "orange", lty = 1)
  
  # add legend
  if (wvar(res)$robust == TRUE){
    wv_title_part1 = "Empirical Robust WV "
  }else{
    wv_title_part1 = "Empirical WV "
  }
  
  CI_conf = 1 - wvar(res)$alpha
  
  legend("bottomleft", 
         legend = c(as.expression(bquote(paste(.(wv_title_part1), hat(nu)^2))), 
                    as.expression(bquote(paste("CI(",hat(nu)^2,", ",.(CI_conf),")"))),
                    "WV implied by WN"),
         pch = c(16, 15, 0), lty = c(1, NA, 1), 
         col = c("darkblue", hcl(h = 210, l = 65, c = 100, alpha = 0.2), "orange"), 
         cex = 1, pt.cex = c(1.25, 3, 1.25), bty = "n")

Formatting `corr_analysis`

The formatting of corr_analysis is a bit off. In particular the spacing between graph is too big. This should be controlled by some input to the function. Could you please try to improve this? Below is an example:

screen shot 2017-09-08 at 4 43 42 pm

Compatibility of legends in make_frame

Hi,

I think make_frame works very well when we add plots on it. The only issue I find out for this moment is that, make_frame seems to have some issue with the location of legend of the original plot.

For example, please try the below code:
Xt = gen_gts(300, AR(phi = c(0, 0, 0.8), sigma2 = 1))
model = arima(Xt, order = c(3,0,0), include.mean = T)
resid_plot(Xt, model, std = FALSE, type = "hist")

As you can see the plot will have legend in the very topright, which should be included inside the white space.

@munsheet Do you think we can modify the make_frame function a bit to have it compatible with legends? Please let me know if I can help in any way! Thanks a lot! :)

Add parameters to plot.ACF/PACF

Add parameters to the functions mentioned in the title. I propose to add the following:

  • xlab
  • ylab
  • col_ci
  • alpha
  • Title of the graph

All of these parameters should have the default value leading to the current behavior of the function. Make sure to update help. Thanks!

grid issue in plot.select_arima

xt = gen_ma1(100, 0.3, 1) x = select_ma(xt, q.min=2L, q.max=5L) plot(x)

In this example, the grid lines also exist at x = 2.5, 3.5, 4.5. But we would only want grid lines at x = 2,3,4,5. How should we modify the grid? Thanks a lot!

screen shot 2018-09-02 at 11 48 02 pm

Change description

We should change description of the package from "Simulation tools" to something like "Simulation tools and functions need for the ts textbook". Rob could you please take of it? Thanks!

Add diagnostic function based on wv

Add this function once wv is available:

#' @description This function will plot eight diagnostic plots to assess how well the model fits  
#' the data. These include: (1) residuals plot, (2) residuals vs fitted values, 
#' (3) histogram of standardized residuals, (4) normal Q-Q plot of 
#' residuals, (5) ACF plot, (6) PACF plot, (7) Haar Wavelet variance representation,
#' (8) Ljung-Box test results.
#' @author Yuming Zhang, James Balamuta
#' @param Xt The original time series data.
#' @param model The \code{arima} model fit to the data.
#' @param std A \code{boolean} indicating whether we use standardized residuals for 
#' the (1) residuals plot and the (8) Ljung-Box test results.
#' @export
#' @importFrom graphics points
#' @importFrom stats qqnorm
#' @importFrom stats qqline
#' @importFrom wv wvar
#' @importFrom stats var
#' @importFrom stats resid
#' @examples 
#' Xt = gen_gts(300, AR(phi = c(0, 0, 0.8), sigma2 = 1))
#' model = arima(Xt, order = c(3,0,0), include.mean = TRUE)
#' diag_plot(Xt, model)
diag_plot = function(Xt, model, std = FALSE){
  par(mfrow = c(2,4))
  
  # extract residuals 
  res = resid(model)
  
  
  # ----- plot 1
  resid_plot(Xt, model, std = std, type = "resid")
  
  # ----- plot 2
  fitted = as.numeric(Xt - res)
  # make frame
  x_range = c(min(fitted), max(fitted)) * 1.05
  y_range = c(min(res), max(res)) * 1.05
  make_frame(x_range, y_range, 
             xlab = "Fitted Values", ylab = "Residuals",
             main = "Residuals vs Fitted Values")
  # add points
  points(fitted, res, pch=16, col = "black")
  
  # ----- plot 3
  resid_plot(Xt, model, std = TRUE, type = "hist")
  
  # ----- plot 4
  my_qqnorm = qqnorm(res, plot.it = FALSE)
  # make frame
  x_range = c(min(my_qqnorm$x), max(my_qqnorm$x))*1.05
  y_range = c(min(my_qqnorm$y), max(my_qqnorm$y))*1.05
  make_frame(x_range, y_range, 
             xlab = "Theoretical Quantiles", 
             ylab = "Sample Quantiles",
             main = "Normal Q-Q Plot")
  
  # add qq plots
  points(my_qqnorm$x, my_qqnorm$y)
  qqline(res, col = "blue",lwd = 2)
  
  # ----- plot 5
  plot(ACF(Xt), col_ci = "#00BBDC33")
  
  # ----- plot 6
  plot(PACF(Xt), col_ci = "#00BBDC33")
  
  # ----- plot 7
  plot(wvar(res), main = "Haar WVar Representation", legend_position = NA)
  
  sigma2 = rep(var(res), length(wvar(res)$scales))
  points(wvar(res)$scales, sigma2/as.numeric(wvar(res)$scales), col = "orange", pch=0, cex=2)
  lines(wvar(res)$scales, sigma2/as.numeric(wvar(res)$scales), col = "orange", lty = 1)
  
  # add legend
  if (wvar(res)$robust == TRUE){
    wv_title_part1 = "Empirical Robust WV "
  }else{
    wv_title_part1 = "Empirical WV "
  }
  
  CI_conf = 1 - wvar(res)$alpha
  
  legend("bottomleft", 
         legend = c(as.expression(bquote(paste(.(wv_title_part1), hat(nu)^2))), 
                    as.expression(bquote(paste("CI(",hat(nu)^2,", ",.(CI_conf),")"))),
                    "WV implied by WN"),
         pch = c(16, 15, 0), lty = c(1, NA, 1), 
         col = c("darkblue", hcl(h = 210, l = 65, c = 100, alpha = 0.2), "orange"), 
         cex = 1, pt.cex = c(1.25, 3, 1.25), bty = "n")
  
  
  # ----- plot 8
  object = diag_ljungbox(model, stop_lag = 20, stdres = std)
  
  # make frame
  x_range = c(min(object$lag), max(object$lag))*1.05
  y_range = c(0, max(object$pvalue))*1.05
  make_frame(x_range, y_range, 
             xlab = "Lag", ylab = "P-value", main = "Ljung-Box Results")
  
  # add plotting
  points(object$lag, object$pvalue, pch = 16)
  lines(object$lag, object$pvalue, lty = 3)
  abline(h = 0.05, col = "blue", lty = 2)
  
  par(mfrow = c(1,1))

}

Check dimnames(acfe) when x is a matrix

Problems with lines 145 and 384 in ACF.r which, in the first case, gives the following error:

x = matrix(rnorm(1000), 100, 10)
test = ACF(x)
Error in dimnames(acfe) <- list(seq_len(nrow(acfe)) - 1, "ACF", varName) :
length of 'dimnames' [2] not equal to array extent

Release to CRAN

  • Update estimate, select, check, predict to work with SARIMA
  • Add any other functions needed for STAT 463
  • Address relevant issues (at lease #23 #24 #21 #9)
  • Fix errors (i.e. check package)
  • Check if anythings else is needed to release the package

Improve function RW3dimensions

Improve the help of the function RW3dimensions. Add these parameters has inputs of the function:

  couleur = "blue4"
  xlab = "X-position"
  ylab = "Y-position"
  main = NULL
  pt_col = NULL
  pt_pch = 16
  pt.cex = 2
  leg_pos = NULL

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.