\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\selectlanguage{english}

\usepackage[colorlinks=true, citecolor=blue, 
  linkcolor=blue, urlcolor=blue,
  bookmarksopen=false, pdfpagemode=UseOutlines]{hyperref}

\usepackage{Sweave}
\DefineVerbatimEnvironment{Sinput}{Verbatim}{
  baselinestretch=1.2, fontshape = sl, fontsize=\small, xleftmargin = 0.7cm}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{
  baselinestretch=1.1, fontshape = sl, fontsize=\small, xleftmargin = 0.7cm}

\renewcommand{\baselinestretch}{1.3}

\topmargin -1.3cm
\textheight 24cm
\textwidth 15.5cm
\oddsidemargin -0.5cm
\evensidemargin 0.0cm

\title{\textsf{R} Package \textsf{tsoutliers}}
\author{Javier L\'opez-de-Lacalle}
\date{February 2019}

\begin{document}

%\VignetteIndexEntry{tsoutliers-intro}
%\VignetteKeyword{outliers}
%\VignetteKeyword{time series}
%\VignetteDepends{tsoutliers}

\maketitle

\abstract{
This is a minimal introduction to package \textsf{tsoutliers}. 
Further information is available in the references given below.
}

\section{Introduction}

Details about the methodology and algorithms implemented in the package are 
given in \href{https://www.jalobe.com/doc/tsoutliers.pdf}{this document}.
%
%https://stats.stackexchange.com/questions/104882/detecting-outliers-in-time-series-ls-ao-tc-using-tsoutliers-package-in-r-how
%https://stats.stackexchange.com/questions/108374/arima-intervention-transfer-function-how-to-visualize-the-effect
%
As a preliminary introduction and discussion see these posts: \\
\href{https://www.jalobe.com/blog/tsoutliers/}{%
https://www.jalobe.com/blog/tsoutliers/} and \\
\href{https://stats.stackexchange.com/questions/104882/}{%
https://stats.stackexchange.com/questions/104882/}.

\paragraph{Examples}

Fit a local level model to the Nile time series 
and check for the presence of possible outliers (additive outliers, 
level shifts or transitory changes):

As of version 0.6-7 the experimental version for structural time series model 
is not available. Check previous versions of the package or contact the maintainer for details.
For illustration, these are the results that were obtained in previous versions for the local level model.
% <<>>=
% require("tsoutliers")
% resNile1 <- tso(y = Nile, types = c("AO", "LS", "TC"),
%   tsmethod = "stsm", args.tsmodel = list(model = "local-level"))
% resNile1$fit$call$xreg <- NULL
% resNile1
% @
\begin{Sinput}
> resNile1 <- tso(y = Nile, types = c("AO", "LS", "TC"), 
+   tsmethod = "stsm", args.tsmodel = list(model = "local-level"))
> resNile1$fit$call$xreg<-NULL
> resNile1
\end{Sinput}

\begin{Soutput}
Call:
structure(list(method = "L-BFGS-B"), .Names = "method")

Parameter estimates:
               LS29   var1  var2
Estimate    -247.78  16136     0
Std. error    11.71   1163   NaN

Log-likelihood: -633.0286 
Convergence: 0 
Number of iterations: 46 46 
Variance-covariance matrix: optimHessian 

Outliers:
  type ind time coefhat  tstat
1   LS  29 1899  -247.8 -21.16
Warning messages:
1: In sqrt(diag(solve(res$hessian))) : NaNs produced
2: In sqrt(diag(solve(res$hessian))) : NaNs produced
3: In sqrt(diag(solve(res$hessian))) : NaNs produced
4: In sqrt(diag(solve(res$hessian))) : NaNs produced
5: In sqrt(diag(solve(res$hessian))) : NaNs produced
\end{Soutput}

Choose and fit an ARIMA model for the Nile time series 
checking for the presence detect possible outliers 
(additive outliers, level shifts or transitory changes):

<<>>=
require("tsoutliers")
resNile2 <- tso(y = Nile, types = c("AO", "LS", "TC"),
  discard.method = "bottom-up", tsmethod = "auto.arima",
  args.tsmethod = list(allowdrift = FALSE, ic = "bic"))
resNile2
@

\end{document}
