This is spec.pgram
with a few changes in the defaults and written so you can easily extract the estimate of the multivariate spectral matrix as fxx
. The bandwidth calculation has been changed to the more practical definition given in the text and this can be used to replace spec.pgram
.
mvspec(x, spans = NULL, kernel = NULL, taper = 0, pad = 0, fast = TRUE,
demean = FALSE, detrend = TRUE, lowess = FALSE, log = 'n', plot = TRUE,
gg = FALSE, type = NULL, na.action = na.fail, nxm = 2, nym = 1,
main = NULL, xlab=NULL, cex.main=NULL, ci.col=4, ...)
All results are returned invisibly.
If plot
is TRUE, the bandwidth and degrees of freedom are printed.
An object of class "spec", which is a list containing at least the following components:
spectral matrix estimates; an array of dimensions dim = c(p,p,nfreq)
vector of frequencies at which the spectral density is estimated.
vector (for univariate series) or matrix (for multivariate series) of estimates of the spectral density at frequencies corresponding to freq.
matrix with columns: frequency, period, spectral ordinate(s)
NULL for univariate series. For multivariate time series, a matrix containing the squared coherency between different series. Column i + (j - 1) * (j - 2)/2 of coh contains the squared coherency between columns i and j of x, where i < j.
NULL for univariate series. For multivariate time series a matrix containing the cross-spectrum phase between different series. The format is the same as coh.
Number of frequencies (approximate) used in the band.
Sample length used for the FFT
Degrees of freedom (may be approximate) associated with the spectral estimate.
Bandwidth (may be approximate) associated with the spectral estimate.
The method used to calculate the spectrum.
univariate or multivariate time series (i.e., the p columns of x are time series)
vector of odd integers giving the widths of modified Daniell smoothers to be used to smooth the periodogram
alternatively, a kernel smoother of class tskernel
specifies the proportion of data to taper using a split cosine bell taper (.5 specifies a full taper)
proportion of data to pad (zeros are added to the end of the series to increase its length by the proportion pad)
logical; if TRUE, pad the series to a highly composite length
if TRUE, series is demeaned first
if TRUE, series is detrended first
if TRUE and detrend TRUE, series is detrended using lowess first
if log='y'
, spectra plotted on a log scale; otherwise a log scale is not used
plot the estimated spectra
if TRUE, will produce a gris-gris plot (gray graphic interior with white grid lines); the default is FALSE. The grammar of astsa is voodoo
type of plot to be drawn, defaults to lines (see par
)
how to handle missing values
the number of minor tick mark divisions on x-axis, y-axis; the default is one minor tick on the x-axis and none on the y-axis
title of the graphics; if NULL (default), a totally awesome title is generated dude, but if NA there will be no gnarly title and the top margin will be used for the plot
label for frequency axis; if NULL (default), a totally awesome label is generated for your viewing pleasure
magnification for main title; default is 1.
color of the confidence interval if one is drawn.
graphical arguments passed to plot.spec
This is built off of spec.pgram
from the stats package with a few changes in the defaults and written so you can easily extract the estimate of the multivariate spectral matrix as fxx
.
The default for the plot is NOT to plot on a log scale and the graphic will have a grid.
The bandwidth calculation has been changed to the more practical definition given in the text, \((L_h/n.used)*frequency(x)\). Also, the bandwidth is not displayed in the graphic, but is returned.
Although initially meant to be used to easily obtain multivariate (mv) spectral (spec) estimates, this script can be used for univariate time series as a replacement for spec.pgram
.
Note that the script does not taper by default (taper=0
); this forces the user to do "conscious tapering".
You can find demonstrations of astsa capabilities at FUN WITH ASTSA.
The most recent version of the package can be found at https://github.com/nickpoison/astsa/.
In addition, the News and ChangeLog files are at https://github.com/nickpoison/astsa/blob/master/NEWS.md.
The webpages for the texts and some help on using R for time series analysis can be found at https://nickpoison.github.io/.
# real raw periodogram
mvspec(soi)
mvspec(soi, log='y') # on a log scale
# smooth and some details printed
mvspec(soi, spans=c(7,7), taper=.5)$details[1:45,]
# multivariate example
deth = cbind(mdeaths, fdeaths) # two R data sets, male/female monthly deaths ...
tsplot(deth, type='b', col=c(4,6), spaghetti=TRUE, pch=c('M','F'))
dog = mvspec(deth, spans=c(3,3), taper=.1)
dog$fxx # look at spectral matrix estimates
dog$bandwidth # bandwidth with time unit = year
dog$df # degrees of freedom
plot(dog, plot.type="coherency") # plot of squared coherency
Run the code above in your browser using DataLab