more than just data ... it's a palindrome
... astsa
is the R package to accompany Time Series Analysis and Its Applications: With R Examples and Time Series A Data Analysis Approach using R.
The GitHub version is updated before the CRAN version. To update the package to the most recent working version, you just need the following two lines:
install.packages("remotes") # if you don't have the package already
remotes::install_github("nickpoison/astsa/astsa_build")
If
astsa
is loaded, then detach it first:detach(package:astsa)
. If you get a warning to install Rtools, ignore it. After successful installation, don't forget to reloadastsa
. If you get an error, just restart R and reloadastsa
.
💡 You can also install astsa
from the archive (which is always current): astsa_2.2.tar.gz - in progress. Download it (there's a download ⇩ button) and in R, use Install package(s) from local files... from the Packages tab.
🐤 You can find a short guide to astsa
scripts right here at FUN WITH ASTSA .
✨ Here is A Road Map if you want a broad view of what is available.
should be available on cRan by the time edition 5 is published (Jan-Feb 2025)
-
added
QQnorm
- a much nicer version ofqqnorm
from the stats package. -
tsplot
gets a multifigure scale factor so characters do not get too small - examples in the help file. -
lag1.plot
andlag2.plot
got little facelifts including increasing the character expansion (cex
) a bit to prevent characters from getting too small. -
in
tsplot
, ifspaghetti=TRUE
, added the ability to include a simple legend (so you don't have to uselegend
). An example is in the help file. -
changed
sarima
so it works for tiny sample sizes (thought about including a "tiny" warning, but didn't ... maybe some other time) -
added
pre.white
to do cross-correlation analysis with automatic prewhitening -
arma.spec
gets better default title andsarima.for
gets aylab
option. -
added
lap.xts
(an xts object), which contains the original daily observations from the LA Pollution-Mortality study. How to easily get weekly averages is given in the examples section of the man page. -
for
trend
, ifresults=TRUE
a summary of the regression (if used) results is printed -
the scripts and data sets that were marked 'x' have been removed to here ... they won't be included in the next version.
-
dna2vector
was updated (😡 due to change in a base R script) - previous versions might not work -
in
mvspec
setfxx
toNULL
in univariate case ... and in the call, ifdemean=TRUE
(default isFALSE
)detrend
is set toFALSE
; i.e.,demean=TRUE
setsdetrend=FALSE
. -
added
gtemp.month
(monthly global data 1975-2023) -- rows are month, columns are year to make it easy to plot as monthly functional data
-
added
SV.mle
to fit an SV model [with feedback (aka leverage) if desired] via quasi-MLE. Details are in the help file.SVfilter
is now part ofSV.mle
and the original script is now in the xBox asxSVfilter
. -
tsplot
becomes more kick-ass with full control of theGrid
-
sarima
- prettified the output -
added
MEI
(Multivariate ENSO Index) data set -
SV.mcmc
moved ESS display to the ACFs -
test.linear
gets a title (main
) control -
adjustment to
Kfilter(... , version=2)
- if the errors are Q½ wt and R½ vt then S = cov(wt, vt), the Q½ and R½ are already included in the script. Often in this case wt = vt and both are iid N(0, I) sequences, soS = diag(1, q)
, the q x q identity matrix. -
made
acf1
more flexible -
minor visual improvements to
SigExtract
-
increased the default max order and made detrend default on
spec.ic
-
improvements to
mvspec
like allow detrending via lowess and some other visual improvements like a gris-gris option, enhancing the voodoo grammar of astsa -
updated
cardox
to 2023 -
added
ar.boot
to get the bootstrapped distributions of the parameters of a specified (by the order) AR model. -
added option in
trend
to plot (or not) the CIs - default is to plot (ci=TRUE
). -
updated global temperature data sets and put all the old ones into the x box to be deleted (eventually) ...
gtemp_land
,gtemp_ocean
, andgtemp_both
are the updated/new sets -
updated
gnp
andgdp
in setsGNP
andGDP
. -
added US population data (
USpop
) ... we thought it was in already, now it is
Note There are a number of new scripts and some old ones are set to be retired eventually.
-
Added
autoSpec
for changepoint detection using local nonparametric spectra.-
Also added the data set
ENSO
which is the most recent update to SOI (an older version was used the paper that introduced autoSpec). -
The script uses the Bartlett kernel so it was made available for general use; see
bart
.
-
-
Also added
autoParm
for changepoint detection using local AR models.
✄ Some examples are in FUN WITH ASTSA - structural breaks.
🔸🔸🔸🔸🔸
-
Added
Kfilter
andKsmooth
which are faster than the olderKfilter0-1-2
andKsmooth0-1-2
, are easier to work with, and removes the need for 3 different scripts. -
Added
EM
which supersedesEM0
andEM1
and uses the quickerKfilter
andKsmooth
scripts. In addition, the script now accepts inputs in both the state and observations equations.
Warning the old script names
EM0-1
,Kfilter0-1-2
, andKsmooth0-1-2
have anx
in front of them now:xEM0-1
,xKfilter0-1-2
, andxKsmooth0-1-2
. The scripts haven't changed (old scripts will still work with thex
name change), but they will be phased out eventually. Converting code that used the old scripts to use the newer scripts should be easy with only a few minor changes in the call.
🔸🔸🔸🔸🔸
-
Updated (to run with the new
Kfilter
andKsmooth
scripts):- the Forward Filtering Backward Sampling script (
ffbs
) and - the simple univariate state space model (
ssm
) script
- the Forward Filtering Backward Sampling script (
-
Updated data files
gtemp_land
andgtemp_ocean
to 2021 -
Made
lag1.plot
andlag2.plot
look more purty.
-
Added
Months
to use withpch
for monthly data; see the help file?Months
. -
Tweaked
tsplot
by adding the ability to adjust thempg
graphics parameters settings (?par
); see the help file?tsplot
. -
A
tsplot
plot can now be stored by putting it in an object; e.g.,pl = tsplot(soi)
. Later, enteringpl
will restore the graph and it's possible to add to it (made possible byrecordPlot
). -
Added some Bayesian scripts (examples in FUN WITH ASTSA - see the new section 9)
-
Added
ar.mcmc
to fit AR models via Gibbs sampling -
Added
SV.mcmc
to fit stochastic volatility models -
... and some financial data sets
sp500.gr
(S&P 500 daily returns) andBCJ
(returns of 3 banks) -
Added
ffbs
(forward filter backward sample algorithm) for linear state space models -
Added
ESS
to estimate the effective sample size -
Added a line to
detrend
to make sure the input series is univariate (already there intrend
). Also, intrend
, forgot to add the span option for lowess (actuallystats::loess
with a robust option) - this has been corrected. -
Added the ability to change the legend text color in
lag1.plot
andlag2.plot
and set the default to black - it makes the values easier to see, especially if the background of the legend is transparent.
-
-
v1.15 is v1.14.3 plus the following 2 additions and (of course) minor changes to appease the CRAN warlords:
- Added two new scripts
detrend()
andtrend()
. The first one returns a detrended series using a polynomial regression (default is linear) or lowess (with the default span). The second script fits a trend (same options as detrend) and produces a graphic of the series with the trend and error bounds superimposed. The trend and error bounds are returned invisibly.
- Added two new scripts
-
v1.14.3 (Dec 2021)
-
Added sleep state and movement data (
sleep1
andsleep2
) - more details in the help files. -
Added option to specify a kernel in
specenv
and ifspans
andkernel
are bothNULL
, the spectral envelope will be based on the periodogram. Also changed the way it checks ifsection
is a proper sequence and added option to taper the data prior to estimating spectra. -
Some minor changes:
-
In
matrixpwr
changedisSymmetric(A)
toisSymmetric(unname(A))
because a symmetric matrix is not taken as such if the column and row names are not the same. -
In
arma.spec
if there is near parameter redundancy,ylim
is now adjusted so the figure will be close to the white noise (uniform) density.
-
-
-
v1.14 (Sept 2021) Just in time for a new skool year - v1.14 is on CRAN - it is v1.13.2 with minor changes to please the CRAN gods.
-
v1.13.2 (Aug 2021) Added
acfm
for multiple time series. Produces a matrix of plots of sample ACFs on the diagonal and sample CCFs on the off-diagonals. It's just a nicer version ofacf
for multiple time series.- Also, changed the LAG axis labels on
acf1
,acf2
, andccf2
to show the frequency of the series if it's bigger than one. For example,soi
has frequency 12 and the LAG axis ofacf1(soi)
will be ticked as 1, 2, 3, ... but the label now emphasizes that each tick is LAG divided by 12.
- Also, changed the LAG axis labels on
-
v1.13.1 (July 2021) Some minor improvements to
tsplot-spaghetti
,sarima.sim
,sarima
, andarma.spec
.-
For
sarima.sim
, I forgot to add theinnov
argument in the call (only a problem if you wanted to use your own innovations), but it seemed to work in the examples I tried (maybe it got passed in ...) ¿Quién sabe? Or maybe it was just voodoo. -
Updated some man page (adding sources and subtracting typos).
-
-
v1.13 is on CRAN. There are lots of additions to the package that are listed below.
1.12.9 (GitHub - May 2021)
-
Added
scatter.hist
to draw a scatterplot with marginal histograms - never really liked other versions. -
Minor updates to
tsplot
,SigExtract
andLagReg
to improve the displays. -
Added scripts for the analysis of DNA sequences and other categorical time series:
specenv
,dna2vector
, and the data setEBV
. Andspecenv
can also handle real-valued series.
-
dna2vector
is used to preprocess a categorical sequence. -
EBV
is the entire Epstein-Barr sequence as a long single string. It's not useful on its own, but thrudna2vector
, different regions can be explored viaspecenv
.
-
We needed powers of matrices enough where we thought we'd include it in
astsa
. The script is calledmatrixpwr
and includes%^%
as a more intuitive operator. For example,var(econ5)%^%-.5
to calculate an inverse square root matrix. -
Added
test.linear
, a script to test the null hypothesis that the data are generated from a linear process with iid innovations. -
Updated
Grid
,tsplot
,sarima.sim
, andmvspec
.-
Grid
andtsplot
will produce grid lines at the minor ticks. These can be shut off individually on either axis. -
For
sarima.sim
, now allow seasonal period without having to specify other seasonal parameters - doing so gives a message to make sure you're doing it on purpose, whereas it used to stop the execution. There's an example of the advantage of this in its man page. -
For
tsplot
andmvspec
, by default now, there is one minor tick on the x-axis and none on the y-axis. Also,mvspec
doesn't display the bandwidth on the axis - it's still there in the CI if the plot is on log-scale and it's still part of the "spec" object. -
Also, updated
tsplot
so multiple series can have different plot symbols (pch
), e.g.,tsplot(blood, type='o', col=2:4, pch=2:4, cex=2)
-
-
Prettified
arma.spec
,lag1.plot
,lag2.plot
,sarima
, andsarima.for
, using the awesome power oftsplot
, but no need to change existing code. -
Updated
tsplot
to allow for spaghetti plots:
x <- replicate(100, cumsum(rcauchy(1000))/1:1000)
tsplot(x, col=1:8, spaghetti=TRUE)
-
The package now has its own color palette that is attached when the package is attached. The palette is especially suited for plotting time series. It is a bit darker than the new default R4 palette. You can revert back using
palette("default")
.- In addition, added
astsa.col
script to easily adjust opacity of the astsa color palette - examples on its man page.
- In addition, added
-
Improvements to
acf1
,acf2
, andccf2
employing the power oftsplot
to give some kickass displays. For example,acf2(soi, col=4, lwd=3, gg=TRUE)
. -
sarima.sim
output used to start at time0
- now the start time is up to the user (with defaultt0=0
).
1.12 (CRAN - Dec 2020) The main change was to add a simulation script sarima.sim
for seasonal ARIMA models.
keep moving ... nothing to see here
1.10.6 (Nov 2020 - Github)
-
added
sarima.sim
to simulate data from (possibly seasonal) ARIMA models ... as usual, it has simplifiedastsa
syntax - the model is specified by the parameters, no lists are needed. The script usespolyMul
(also added) to obtain the appropriate autoregessive and moving average polynomials from the specified model. -
Changed any script that used colors as numbers to color names (due to change in R v4 palettes).
-
added PACF option to
acf1
so you can see the PACF alone -
added
plot
option insarima.for
. -
added
spec.ic
which is similar tospec.ar
but has option to base the AR spectral estimate on BIC and todetrend
before the fit. -
fixed
tsplot
- the minor ticks weren't changing on multiple plots
1.10 (May 2020 - CRAN)
Since Version 1.9, see the updates 1.9.1-4 below. The major change was to fix routines
that produced graphics but didn't work on Apple's OS. In addition, I made tsplot
even more useful:
-
For
tsplot
, I added an argument (byrow
) where you can plot row wise (TRUE
) or column wise (FALSE
) for multiple plots. -
Also, for
tsplot
, added an argumentgg
so if it's true the plot will have a g-gray interior ... e.g.,tsplot(climhyd, ncolm=2, gg=TRUE, col=rainbow(6,v=.8), lwd=2)
1.9.4
- Added the polio data (
polio
) set from the gamlss.data package. It is used in Chapter 6 problems. I added it because gamlss.data has data sets with names that are the same as those in astsa. I hate to see package fights.
1.9.3
-
Updated correlation scripts:
-
for
acf1
andacf2
,plot=TRUE
prints rounded values, but ifplot=FALSE
the returned values are not rounded by the scirpt. -
and
ccf2
, the values are now returned invisibly.
-
1.9.2
-
Updated
mvspec
due to possible plot error being caused by use ofpanel.first
(tries to drawgrid
beforeplot
is called on some machines or OS). Note to self: avoidpanel.first
dumb ass. -
Updated
arma.spec
to avoid same problem asmvspec
-
While I'm here, updated
tsplot
to avoid similar problems.
1.9.1
-
added the ability of
tsplot
to do multiple plots, for example,
tsplot(eqexp[,1:8], col=rainbow(8), ncolm=2, lwd=2, main='EQs')
- there's no change for univariate time series. -
added ellipses ( … ) to
lag1.plot
andlag2.plot
so you can change some of the graphical parameters; e.g.,lag1.plot(soi, max.lag=4, pch=20, cex=1.5, col=rgb(0,.5,1,.5))
-
fixed some man pages (after cRan submission)
1.9 (CRAN - May 2019)
-
This version is essentially version 1.8.8 but with changes made to pass the CRAN tests, which mainly deal with the man pages (help files).
-
For the Springer text, you can see the difference between v1.8 and v1.9 by looking at the changelog below v1.8.8. Some scripts have added capabilities, but it won't change any data analysis. The only real difference will be slight numerical differences in the reported ICs in
sarima
.
1.8.8
-
Fixed β reporting for
LagReg()
wheninverse=TRUE
... it is correct in the text example. -
Added ability to fix parameters in
sarima()
andsarima.for()
. Works just like it does in the stat packagearima()
buttransform.pars
is set toFALSE
automatically if parameters are fixed. -
Added a little explanation of how ICs are calculated in the
sarima()
man page.
1.8.7 Added Grid()
combining grid(lty=1, col=gray(.9))
and minor.ticks()
from Graphics and Hmisc packages. It's used in most scripts that used to call grid()
.
1.8.6 Added ssm()
for fitting a simple univariate state space model. This will be used in the forthcoming text.
1.8.5
lag1.plot
andlag2.plot
now have color option for the points with defaultgray(.1)
for a little nicer display- added
cardox
data set, an update toco2
in the datasets package, which stopped in 1997 (now to the end of 2018) - tweaked
sarima
residual analysis graphic so it has less white space - nothing else has changed
1.8.4 changed mvspec
and arma.spec
so the default is NOT to plot on a log scale and the graphics now have a grid ... also, for mvspec
, added a details
value, which is a matrix of frequency, period, spectral ordinate- e.g., mvspec(soi)$details[1:45,]
1.8.3 changed calculation of the ICs in sarima
... there will be a slight difference because I didn't remove the log(2π) part.
1.8.2 (basically adding some new data sets)
- added US GDP - quarterly adjusted to 1947-1 to 2018-3
- slight change to
acf1
andacf2
so user can changeylim
Hare
andLynx
the 90-year data sets of snowshoe hare and lynx pelts purchased by the Hudson's Bay Company of Canada - noteLynx
differs from the R data setlynx
.gtemp_land
land only - updated global temps to 2017gtemp_ocean
open ocean only to 2017- added data set
salmon
- added source of data to
gnp
man page - added
plot
option toacf1
with defaultTRUE
and a few additional minor changes
1.8.1 minor change to the way acf2
calls main
(cleaner) - this was done at the beginning of v1.8 - it just never made it to CRAN
Time Series Analysis and Its Applications With R Examples -- Edition 4 was written under this version.
-
Version 1.8 is on CRAN
-
Version 1.8.1 is here on GitHub.
-
These versions are essentially version 1.7.11 but with some minor changes to satisfy the CRANks. Also, the GitHub version is slightly improved, so call it v1.8.1. The changes are things no one would notice in places where no one looks.
★ Just for historical record, version 1.7 was when CRAN maintainers got CRANky and started asking for arbitrary changes that no one would notice. I'm talking about asking to change one word in the DESCRIPTION file kind of stupid stuff. This is when the GitHub versions started.
1.7.11.
-
added
ARMAtoAR
to give the pi-weights in the invertible representation of an ARMA model ... this is included mainly for pedagogical reasons -
changed the
max.lag
default inacf1
andacf2
so if the series is seasonal, you'll see at least 4 seasons by default ... I got tired of typingacf2(soi, 48)
in class ... nowacf2(soi)
is the same.
1.7.10. in sarima.for
, added the option to include regressors in the forecast
1.7.9. changed na.action
to na.pass
in acf1
, acf2
, and ccf2
... these used to be na.fail
which is the R stats package default
1.7.8. updated tsplot
so the time index can be changed
1.7.7. added tsplot
to give a nice plot of a univariate time series in one easy command ... works like plot
for a ts
object.
1.7.6. added ccf2
, which plots the sample CCF of two series... it operates like ccf
but the graphic is nicer
1.7.5. added acf1
giving the sample ACF of a series without the zero lag... it operates like acf2
but doesn't give the PACF
1.7.4. added data set hor
, quarterly Hawaiian Occupancy Rate (% of rooms)
1.7.3. some additons to acf2
allowing a plot title change, and
an option not to produce a graphic (if you only want to use or see the
values in a nice form)
1.7.2. added plot.all
option to sarima.for
so that if TRUE, all the data are plotted in the graphic; otherwise,
only the last 100 observations are plotted. The default is plot.all=FALSE
because it's easier to see
the forecasts if only 100 observations are plotted.
1.7.1. minor changes to sarima
- diagnostic QQplot used to
depend on
MASS
package until it gave warnings on some simple examples ... now it's done "inhouse" - changed degrees of freedom calculation (wasn't sure the commands I used to get it were correct... now I'm sure).
- made
details=FALSE
also shut off the diagnostic plot, so if you run
dog <- sarima(cmort, 1,1,1, details=FALSE)
then everything (except the graphic) is stored indog
and you won't see any output.
1.7 On CRAN Dec 2016
-
fixed
x0n
andP0n
inKsmooth0
andKsmooth1
(minor fix) -
add
box()
aftergrid()
on some graphics because the grid was overplotting the box =( -
added title option to
sarima()
diagnostic plots indicating model orders (there by default) ... now if you fit a few models, it's easy to see which diagnostic plot goes with which model... -
... also added pointwise 99.9% confidence bounds to the innovations diagnostic QQ-plot based on asymptotic distn of iid normal order statistics ... and some minor tweaks to the Q-stat plot
-
now show ±1 and ±2 root MSPE bounds to
sarima.for()
and used transparent ribbons to display all error bounds because you can't be too pretty -
added time series
cpg
(annual median cost per GB of storage) for an easy regression with autocorrelated errors exercise -
added time series
UnempRate
, which can be taken as an update tounemp
(still there) - the data are monthly US unemployment rate (% unemployed) from 1948 to Nov 2016.
-
added series
globtemp
andglobtempl
... they are updates to the global temperature deviation seriesgtemp
andgtemp2
, respectively.globtempℓ
is land only.gtemp
andgtemp2
are still in astsa so as not to cause problems - the temperature data sets were reformulated in 2011 so the series do not match up in the overlapping time frame - see the help file for more info -
added new series
chicken
... the price of chicken, which is a decent example of trend stationarity
-
needed a minor fix to
acf2
, so while I'm here: -
minor tweaks to
acf2
andsarima.for
displays -
added
xts
datasetdjia
(Dow Jones) so don't need internet connection to use it (or Yahoo now that Verizon owns it - who knows what will happen???) -
listed p-values in
sarima
ttable
because t-tables and p-values go together like horses and carriages, and were popular together around the same time.
-
sarima
has a t-table now (no p-values) -
data set
ar1boot
is gone (example uses simulated data) -
minor tweaks to
lag1.plot
andlag2.plot
displays -
added
sp500w
... anxts
data set, S&P500 weekly returns -
updated man pages for new edition
-
sarima
now allows inclusion ofxreg
to facilitate regression with autocorrelated errors -
fixed
acf2
so grid is under plot -
star
data added -
sunspotz
man page emphasizes data are 2 times/year -
fixed estimate of cov( vt ) = R in
EM0
andEM1
(t=1 part was missing) -
fixed typo in
EMx
missing code (0=observed, 1=missing) -
EM1
fixed so inputs are not allowed (and no longer included in the call):-
Inputs are not allowed. The script uses
Ksmooth1
, but everything related to inputs are set equal to zero. That was the original intent of this script. -
It would be relatively easy to include estimates of
Ups
andGam
because conditional on the states, these are just regression coefficients. If you decide to alterEM1
to include estimates of theUps
orGam
, feel free to notify me with a working example and I'll include it in the next update (assuming it's correct, of course). Instructors... this would bean awesome class project.
-
-
Fixed man page for the scripts
stoch.reg
and for theKfilter
s andKsmooth
s -
acf2
can take additionalacf
arguments likena.action=na.omit
... -
acf2
max vertical axis was always 1; now it depends on data -
acf2
now has grid lines -
FDR
man entry corrected -
Kfilter1
; changedUps
andGam == 0
case to matchKfilter2
s appropriate method -
astsadata()
is gone,LazyData: true
instead
-
Associated namespace with all but 'base' function calls
-
Added data set
blood
(based on code in Example 6.1) as an mts object of the Jones data set withNA
as missing data code. Example 6.9 still uses 0 for missing data. -
Added links to related data sets in some man pages (e.g.,
oil
<->gas
...) -
Added
astsadata.R
option to load all the data sets at once. -
Changed
mvspec.R
so it could be used in place ofspec.pgram
andspectrum
. The defaults are similar now tospec.prgram
, but the default is not to taper, forcing the user to think about it. It also returns the book's more useful definition of bandwidth.
astsa
built fromtsa3.rda
(which is gone now) with the following changes:
in astsa | was | in tsa3 |
---|---|---|
arma.spec() |
... | spec.arma() |
lag1.plot() |
... | lag.plot1() |
lag2.plot() |
... | lag.plot2() |
For the 3rd edition of the text, we included data and scripts as a compressed file
called tsa3.rda
and the basic version of ASTSA was abandoned. Two years later,
tsa3.rda
was abandoned.
The second edition of the text, which included the subtitle With R Examples was when we started giving R code in the text and writing R scripts to compensate for the fact that S and consequently R provided scripts for time series as an afterthought. Still, much of the analysis in the text was done using Matlab.
The first edition of Time Series Analysis and Its Applications used
an updated version of the basic ASTSA
and it was distributed on the website for that
version: Edition 1 site.
You had to extract the files to a floppy (3.5" by that time) and then install ASTSA
.
The first version of ASTSA
was developed by R.H. Shumway for the new text Applied Statistical Time Series Analysis published by Prentice Hall. The package was written in Microsoft basic and was distributed on a 5.25" floppy disk that was included with the text.
The instruction manual has been preserved for historical purposes: http://www.stat.pitt.edu/stoffer/astsaman.pdf