Title: | Generalized Linear Autoregressive Moving Average Models |
---|---|
Description: | Functions are provided for estimation, testing, diagnostic checking and forecasting of generalized linear autoregressive moving average (GLARMA) models for discrete valued time series with regression variables. These are a class of observation driven non-linear non-Gaussian state space models. The state vector consists of a linear regression component plus an observation driven component consisting of an autoregressive-moving average (ARMA) filter of past predictive residuals. Currently three distributions (Poisson, negative binomial and binomial) can be used for the response series. Three options (Pearson, score-type and unscaled) for the residuals in the observation driven component are available. Estimation is via maximum likelihood (conditional on initializing values for the ARMA process) optimized using Fisher scoring or Newton Raphson iterative methods. Likelihood ratio and Wald tests for the observation driven component allow testing for serial dependence in generalized linear model settings. Graphical diagnostics including model fits, autocorrelation functions and probability integral transform residuals are included in the package. Several standard data sets are included in the package. |
Authors: | William T.M. Dunsmuir <[email protected]>, Cenanning Li <[email protected]>, and David J. Scott <[email protected]> |
Maintainer: | "William T.M. Dunsmuir" <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.6-0 |
Built: | 2024-12-11 07:19:41 UTC |
Source: | CRAN |
The data arose from a single hospital (at Campbelltown) as part of a larger (ongoing) study into the relationship between atmospheric pollution and the number of asthma cases presenting themselves to various emergency departments in local hospitals in the South West region of Sydney, Australia.
data(Asthma)
data(Asthma)
A data frame containing the following columns:
[, 1] | Count | Daily counts of asthma at Campbelltown Hospital. |
[, 2] | Intercept | A vector of ones, providing the intercept in the model. |
[, 3] | Sunday | Takes value one for Sundays, otherwise zero. |
[, 4] | Monday | Takes value one for Mondays, otherwise zero. |
[, 5] | CosAnnual | cos((2*pi*t)/365), annual cosine term. |
[, 6] | SinAnnual | sin((2*pi*t)/365), annual sine term. |
[, 7] | H7 | Scaled lagged and smoothed humidity variable. |
[, 8] | NO2max | Maximum daily nitrogen dioxide. |
[, 9:16] | T1.1990 - T2.1993 | Smooth shapes to capture school terms in each year. |
Davis, Richard A and Dunsmuir, William TM and Streett, Sarah B (2003) Observation-driven models for Poisson counts. Biometrika, 90, 777–790.
### Example with asthma data data(Asthma) y <- Asthma[,1] X <- as.matrix(Asthma[,2:16]) ## Model in Davis, Dunsmuir and Streett (2003) ## MA(7) specification - see Davis, Dunsmuir and Streett (2003) ## Pearson Residuals, Fisher Scoring glarmamod <- glarma(y, X, thetaLags = 7, type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) likTests(glarmamod) plot.glarma(glarmamod) ## Not run: ## Example is specified as \dontrun because it takes too long ## for package inclusion on CRAN ## Pearson Residuals, Newton Raphson, Negative Binomial ## Initial value of the shape parameter take to be zero glarmamod <- glarma(y, X, thetaLags = 7, type = "NegBin", method = "NR", residuals = "Pearson", alphaInit = 0, maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) likTests(glarmamod) plot.glarma(glarmamod) ## End(Not run)
### Example with asthma data data(Asthma) y <- Asthma[,1] X <- as.matrix(Asthma[,2:16]) ## Model in Davis, Dunsmuir and Streett (2003) ## MA(7) specification - see Davis, Dunsmuir and Streett (2003) ## Pearson Residuals, Fisher Scoring glarmamod <- glarma(y, X, thetaLags = 7, type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) likTests(glarmamod) plot.glarma(glarmamod) ## Not run: ## Example is specified as \dontrun because it takes too long ## for package inclusion on CRAN ## Pearson Residuals, Newton Raphson, Negative Binomial ## Initial value of the shape parameter take to be zero glarmamod <- glarma(y, X, thetaLags = 7, type = "NegBin", method = "NR", residuals = "Pearson", alphaInit = 0, maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) likTests(glarmamod) plot.glarma(glarmamod) ## End(Not run)
coef
is a generic function which extracts GLARMA model coefficients
from objects returned by modeling functions. coefficients
is an alias
for it.
## S3 method for class 'glarma' coef(object, types = "all", ...)
## S3 method for class 'glarma' coef(object, types = "all", ...)
object |
An object of class |
types |
Character; which coefficients to extract, either
|
... |
Further arguments passed to or from other methods. |
This is an S3 generic function. coef
or coefficients
return the requested coefficients from the object of class
"glarma"
. By changing the argument type
, either the ARMA
coefficients (ARMA
), regression coefficients (beta
) or
all coefficients are returned. In the case of negative binomial
counts, the negative binomial coefficient is also
returned if type is
all
, or if type is NB
. The default
is all
.
ARMA
coefficients, beta
coefficients, NB
coefficients or all of these three types of coefficients are extracted
from the glarma
model object object
.
A named numeric vector or list of named numeric vectors is returned.
fitted.glarma
and residuals.glarma
for
related methods;
data(Polio) Y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(Y, X, thetaLags = c(1, 2, 5), type = "Poi", method = "FS", residuals= "Pearson", maxit = 100, grad = 1e-6) coef(glarmamod, type = "ARMA") coef(glarmamod, type = "beta") coef(glarmamod, type = "all")
data(Polio) Y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(Y, X, thetaLags = c(1, 2, 5), type = "Poi", method = "FS", residuals= "Pearson", maxit = 100, grad = 1e-6) coef(glarmamod, type = "ARMA") coef(glarmamod, type = "beta") coef(glarmamod, type = "all")
This data set gives the number of single vehicle nighttime driver deaths in the state of Utah by month over the period August 1980 to July 1986, along with observations on a number of possible predictors. The aim of the study from which it was taken was to investigate the effect of the lowering of the legal blood alcohol concentration (BAC) while driving, from 0.1 to 0.08 units, and the simultaneous introduction of administrative license revocation. The time period for the observations is centred on the month of the intervention, August 1983.
data(DriverDeaths)
data(DriverDeaths)
A data frame containing the following columns:
[, 1] | Deaths | Number of single vehicle nighttime driver deaths monthly. |
[, 2] | Intercept | A vector of ones, providing the intercept in the model. |
[, 3] | ReducedBAC | Indicator of before or after lowering of legal blood alcohol level.0 for months prior to August 1983, 1 for months on or after August 1983. |
[, 4] | FriSat | Number of Friday and Saturday nights in the month. |
[, 5] | lnOMVDRate | Log of the number of other motor vehicle deaths per 100,000 of population. |
[, 6] | Population | Adult population of the State of Utah. |
Debra H. Bernat, William T.M. Dunsmuir, and Alexander C. Wagenaar (2004) Effects of lowering the legal BAC to 0.08 on single-vehicle-nighttime fatal traffic crashes in 19 jurisdictions. Accident Analysis & Prevention, 36, 1089–1097.
### Model number of deaths data(DriverDeaths) y <- DriverDeaths[, "Deaths"] X <- as.matrix(DriverDeaths[, 2:5]) Population <- DriverDeaths[, "Population"] ### Offset included glarmamodOffset <- glarma(y, X, offset = log(Population/100000), phiLags = c(12), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) print(summary(glarmamodOffset)) par(mfrow =c(3,2)) plot(glarmamodOffset) ### No offset included glarmamodNoOffset <- glarma(y, X, phiLags = c(12), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) print(summary(glarmamodNoOffset)) par(mfrow=c(3,2)) plot(glarmamodNoOffset)
### Model number of deaths data(DriverDeaths) y <- DriverDeaths[, "Deaths"] X <- as.matrix(DriverDeaths[, 2:5]) Population <- DriverDeaths[, "Population"] ### Offset included glarmamodOffset <- glarma(y, X, offset = log(Population/100000), phiLags = c(12), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) print(summary(glarmamodOffset)) par(mfrow =c(3,2)) plot(glarmamodOffset) ### No offset included glarmamodNoOffset <- glarma(y, X, phiLags = c(12), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) print(summary(glarmamodNoOffset)) par(mfrow=c(3,2)) plot(glarmamodNoOffset)
extractAIC
method for class "glarma"
. Used to
extract AIC from a glarma
object.
## S3 method for class 'glarma' extractAIC(fit, ...)
## S3 method for class 'glarma' extractAIC(fit, ...)
fit |
An object of class |
... |
Further arguments passed to or from other methods. |
AIC extracted from object
coef.glarma
, residuals.glarma
,
glarma
.
fitted
method for class "glarma"
. fitted.values
is an alias for fitted
.
## S3 method for class 'glarma' fitted(object, ...)
## S3 method for class 'glarma' fitted(object, ...)
object |
An object of class |
... |
Further arguments passed to or from other methods. |
This is an S3 generic function. fitted
or fitted.values
return the required fitted values from an object of class
"glarma"
.
Fitted values mu
extracted from the object object
.
coef.glarma
, residuals.glarma
,
glarma
.
forecast
is a generic function for forecasting time series or
time series models. The function invokes particular methods which
depend on the class of the first argument.
Currently the only method provided by the package is for objects of
class "glarma"
.
forecast(object, ...) ## S3 method for class 'glarma' forecast(object, n.ahead = 1, newdata = 0, newoffset = 0, newm = 1, ...)
forecast(object, ...) ## S3 method for class 'glarma' forecast(object, n.ahead = 1, newdata = 0, newoffset = 0, newm = 1, ...)
object |
An object of class "glarma" obtained from a call to
|
n.ahead |
The number of periods ahead to be forecast. |
newdata |
The model matrix |
newoffset |
A vector containing the values of the offset for the times for which the series is to be predicted. |
newm |
A vector containing the number of trials when forecasting binomial or binary time series. Defaults to the binary case. |
... |
Further arguments for the call, currently unused. |
Only one forecasting method is currently provided, for objects of class "glarma". This produces an object of class "glarmaForecast".
When forecasting one step ahead, the values in the matrix
newdata
(and offset
if there is an offset) in the GLARMA
model are used along with the regression coefficients in the model to
obtain the predicted value of , the regression
component of the state variable
. The predicted value of the
ARMA component of the state variable is then added to this value to
give the predicted value of
.
When further predictions are required, since no data is available to
calculate the predicted value of the state variable, an observation is
generated from the predicted distribution and the methodology for one
step ahead is then used on this generated data. This process is
repeated until predictions are obtained for the required number of
time periods (specified by n.ahead
). Note that the value of
n.ahead
must equal the row dimension of newdata
and if
they are specified, of newoffset
and newm
.
For completeness a randomly generated value of the time series is produced even for one step-ahead prediction.
Note that the forecasted time series returned as the component
fitted
is then a randomly generated sample path for the
predicted time series. If a sample of such paths is produced by
repeated calls to forecast
then sample predicted distributions
can be obtained for the forecast series.
In the case of binary or binomial time series in addition to values of
the predictors in the regression component of the state variable and
the values of any offset, the numbers of trials for the binomially
distributed future observations are required. This information should
be provided in the argument newm
. If not, the number of trials
defaults to 1, which is the case of binary responses.
forecast
currently has no default method.
When object
is of class "glarma"
, forecast
returns an object of class "glarmaForecast"
with components:
eta |
the forecast values of the regression component of the state variable |
W |
the forecast values of the state variable |
mu |
the conditional mean |
Y |
the simulated series based on the fitted model |
n.ahead |
the number of steps ahead for which the forecasts were
requested in the call to |
newdata |
the model matrix |
newoffset |
the vector containing the values of the offset for the times for which the series is to be predicted |
newm |
the vector giving the number of trials when forecasting binomial or binary time series |
model |
the |
"William T.M. Dunsmuir" <[email protected]> and "David J Scott" <[email protected]>
require(zoo) ### Model number of deaths data(DriverDeaths) y <- DriverDeaths[, "Deaths"] X <- as.matrix(DriverDeaths[, 2:5]) Population <- DriverDeaths[, "Population"] ### Offset included glarmamod <- glarma(y, X, offset = log(Population/100000), phiLags = c(12), thetaLags = c(1), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) print(summary(glarmamod)) XT1 <- matrix(X[72,], nrow = 1) offsetT1 <- log(Population/100000)[72] mu <- forecast(glarmamod, 1, XT1, offsetT1)$mu print(mu) ### Save some values allX <- X allFits <- fitted(glarmamod) ally <- y ### Look at a succession of forecasts ### Using actual values in forecasts forecasts <- numeric(72) for (i in (62:71)){ y <- DriverDeaths[1:i, "Deaths"] X <- as.matrix(DriverDeaths[1:i, 2:5]) Population <- DriverDeaths[1:i, "Population"] ## Offset included glarmamod <- glarma(y, X, offset = log(Population/100000), phiLags = c(12), thetaLags = c(1), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) XT1 <- matrix(allX[i + 1, ], nrow = 1) offsetT1 <- log(DriverDeaths$Population[i + 1]/100000) mu <- forecast(glarmamod, 1, XT1, offsetT1)$mu if (i == 62){ forecasts[1:62] <- fitted(glarmamod) } forecasts[i+1] <- mu } par(mfrow = c(1,1)) forecasts <- ts(forecasts[63:72], start = c(1985, 10), deltat = 1/12) fitted <- ts(allFits, start = c(1980, 8), deltat = 1/12) obs <- ts(DriverDeaths$Deaths, start = c(1980, 8), deltat = 1/12) plot(obs, ylab = "Driver Deaths", lty = 2, main = "Single Vehicle Nighttime Driver Deaths in Utah") points(obs) lines(fitted, lwd = 2) lines(forecasts, col = "red") par(xpd = NA) graph.param <- legend("top", legend = c("observations",expression(estimated~mu[t]), expression(predicted~mu[t])), ncol = 3, cex = 0.7, bty = "n", plot = FALSE) legend(graph.param$rect$left, graph.param$rect$top + graph.param$rect$h, legend = c("observations", expression(estimated~mu[t]), expression(predicted~mu[t])), col = c("black","black","red"), lwd = c(1,2,1), lty = c(2,1,1), pch = c(1, NA_integer_, NA_integer_), ncol = 3, cex = 0.7, bty = "n", text.font = 4) par(xpd = FALSE) ### Generate a sample of Y values 2 steps ahead and examine the distribution data(DriverDeaths) y <- DriverDeaths[, "Deaths"] X <- as.matrix(DriverDeaths[, 2:5]) Population <- DriverDeaths[, "Population"] ### Fit the glarma model to the first 70 observations glarmamod <- glarma(y[1:70], X[1:70, ], offset = log(Population/100000)[1:70], phiLags = c(12), thetaLags = c(1), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) nObs <- NROW(X) n.ahead <- 2 ### Specify the X matrix and offset for the times where predictions ### are required XT1 <- as.matrix(X[(nObs - n.ahead + 1):nObs, ]) offsetT1 <- log(Population/100000)[(nObs - n.ahead + 1):nObs] nSims <- 500 forecastY <- matrix(ncol = n.ahead, nrow = nSims) forecastMu <- matrix(ncol = n.ahead, nrow = nSims) ### Generate sample predicted values for(i in 1:nSims){ temp <- forecast(glarmamod, n.ahead, XT1, offsetT1) forecastY[i, ] <- temp$Y forecastMu[i, ] <- temp$mu } ### Examine distribution of sample of Y values n.ahead table(forecastY[, 2]) par(mfrow = c(2,1)) barplot(table(forecastY[, 2]), main = "Barplot of Sample Y Values 2 Steps Ahead") hist(forecastY[, 2], xlab = "Sample Y values", breaks=seq(0,max(forecastY[, 2])), main = "Histogram of Sample Y Values 2 Steps Ahead\nwith 0.025 and 0.975 Quantiles") abline(v = quantile(forecastY[, 2], c(0.025, 0.975)), col = "red")
require(zoo) ### Model number of deaths data(DriverDeaths) y <- DriverDeaths[, "Deaths"] X <- as.matrix(DriverDeaths[, 2:5]) Population <- DriverDeaths[, "Population"] ### Offset included glarmamod <- glarma(y, X, offset = log(Population/100000), phiLags = c(12), thetaLags = c(1), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) print(summary(glarmamod)) XT1 <- matrix(X[72,], nrow = 1) offsetT1 <- log(Population/100000)[72] mu <- forecast(glarmamod, 1, XT1, offsetT1)$mu print(mu) ### Save some values allX <- X allFits <- fitted(glarmamod) ally <- y ### Look at a succession of forecasts ### Using actual values in forecasts forecasts <- numeric(72) for (i in (62:71)){ y <- DriverDeaths[1:i, "Deaths"] X <- as.matrix(DriverDeaths[1:i, 2:5]) Population <- DriverDeaths[1:i, "Population"] ## Offset included glarmamod <- glarma(y, X, offset = log(Population/100000), phiLags = c(12), thetaLags = c(1), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) XT1 <- matrix(allX[i + 1, ], nrow = 1) offsetT1 <- log(DriverDeaths$Population[i + 1]/100000) mu <- forecast(glarmamod, 1, XT1, offsetT1)$mu if (i == 62){ forecasts[1:62] <- fitted(glarmamod) } forecasts[i+1] <- mu } par(mfrow = c(1,1)) forecasts <- ts(forecasts[63:72], start = c(1985, 10), deltat = 1/12) fitted <- ts(allFits, start = c(1980, 8), deltat = 1/12) obs <- ts(DriverDeaths$Deaths, start = c(1980, 8), deltat = 1/12) plot(obs, ylab = "Driver Deaths", lty = 2, main = "Single Vehicle Nighttime Driver Deaths in Utah") points(obs) lines(fitted, lwd = 2) lines(forecasts, col = "red") par(xpd = NA) graph.param <- legend("top", legend = c("observations",expression(estimated~mu[t]), expression(predicted~mu[t])), ncol = 3, cex = 0.7, bty = "n", plot = FALSE) legend(graph.param$rect$left, graph.param$rect$top + graph.param$rect$h, legend = c("observations", expression(estimated~mu[t]), expression(predicted~mu[t])), col = c("black","black","red"), lwd = c(1,2,1), lty = c(2,1,1), pch = c(1, NA_integer_, NA_integer_), ncol = 3, cex = 0.7, bty = "n", text.font = 4) par(xpd = FALSE) ### Generate a sample of Y values 2 steps ahead and examine the distribution data(DriverDeaths) y <- DriverDeaths[, "Deaths"] X <- as.matrix(DriverDeaths[, 2:5]) Population <- DriverDeaths[, "Population"] ### Fit the glarma model to the first 70 observations glarmamod <- glarma(y[1:70], X[1:70, ], offset = log(Population/100000)[1:70], phiLags = c(12), thetaLags = c(1), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) nObs <- NROW(X) n.ahead <- 2 ### Specify the X matrix and offset for the times where predictions ### are required XT1 <- as.matrix(X[(nObs - n.ahead + 1):nObs, ]) offsetT1 <- log(Population/100000)[(nObs - n.ahead + 1):nObs] nSims <- 500 forecastY <- matrix(ncol = n.ahead, nrow = nSims) forecastMu <- matrix(ncol = n.ahead, nrow = nSims) ### Generate sample predicted values for(i in 1:nSims){ temp <- forecast(glarmamod, n.ahead, XT1, offsetT1) forecastY[i, ] <- temp$Y forecastMu[i, ] <- temp$mu } ### Examine distribution of sample of Y values n.ahead table(forecastY[, 2]) par(mfrow = c(2,1)) barplot(table(forecastY[, 2]), main = "Barplot of Sample Y Values 2 Steps Ahead") hist(forecastY[, 2], xlab = "Sample Y values", breaks=seq(0,max(forecastY[, 2])), main = "Histogram of Sample Y Values 2 Steps Ahead\nwith 0.025 and 0.975 Quantiles") abline(v = quantile(forecastY[, 2], c(0.025, 0.975)), col = "red")
The function glarma
is used to fit generalized linear
autoregressive moving average models with various distributions
(Poisson, binomial, negative binomial) using either Pearson residuals
or score residuals, and for the binomial distribution, identity
residuals. It also estimates the parameters of the GLARMA model with
various distributions by using Fisher scoring or Newton-Raphson
iteration.
For Poisson and negative binomial response distributions the log link is currently used. For binomial responses the logit link is currently used.
glarma(y, X, offset = NULL, type = "Poi", method = "FS", residuals = "Pearson", phiLags, thetaLags, phiInit, thetaInit, beta, alphaInit, alpha = 1, maxit = 30, grad = 2.22e-16) glarmaPoissonPearson(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaPoissonScore(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaBinomialIdentity(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaBinomialPearson(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaBinomialScore(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaNegBinPearson(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaNegBinScore(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS")
glarma(y, X, offset = NULL, type = "Poi", method = "FS", residuals = "Pearson", phiLags, thetaLags, phiInit, thetaInit, beta, alphaInit, alpha = 1, maxit = 30, grad = 2.22e-16) glarmaPoissonPearson(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaPoissonScore(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaBinomialIdentity(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaBinomialPearson(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaBinomialScore(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaNegBinPearson(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS") glarmaNegBinScore(y, X, offset = NULL, delta, phiLags, thetaLags, method = "FS")
y |
Numeric vector; the response variable. If the response variable is for the model with the binomial distribution, it should be a n by 2 matrix, one column is the number of successes and another is the number of failures. |
X |
Matrix; the explanatory variables. A vector of ones should be
added to the data matrix as the first column for the |
offset |
Either |
beta |
Numeric vector; initial values of the regression coefficients. |
phiLags |
Numeric vector; AR orders. |
phiInit |
Numeric vector; initial values for the corresponding AR orders. |
thetaLags |
Numeric vector; MA orders. |
thetaInit |
Numeric vector; initial values for the corresponding MA orders. |
delta |
Numeric vector; initial values of the parameters for the
GLARMA estimation procedure. It is a combination of the parameters of
|
alpha |
Numeric; an optional initial shape parameter for
|
alphaInit |
Numeric; an initial shape parameter for
|
type |
Character; the count distribution. Possible values are
|
method |
Character; method of iteration to be used. Possible
values are |
residuals |
Character; the type of residuals to be used. Possible
values are |
maxit |
Numeric; the maximum number of iterations allowed. |
grad |
Numeric; the tolerance for recognizing numbers, which are smaller than the specified tolerance, as zero. |
Models for glarma
are specified symbolically. A typical model
has the form y
(response), X
(terms) where y
is
the count or factor reponse vector, X
is a series of terms
which specifies a linear predictor for the response. It should be noted
that the first column of X
should be a vector of 1s as the
intercept in the model. Four initial parameters that need to be
estimated are combined into , where
is an optional parameter to accomodate the negative binomial
model. Note that in the function
glm.nb
from the
package MASS, this parameter is called theta
.
For Poisson and negative binomial response distributions the log link is currently used. For binomial responses the logit link is currently used.
The generalized linear autoregressive moving average models are computed as follows.
The linear predictor for the response is
The infinite moving average from the linear predictor is
This infinite moving average, is computed using the autoregressive moving average recursions
where and
are the orders of
and
respectively and the non-zero lags of the vectors
phi
and theta
may be specified by the user via the
arguments phiLag
and thetaLag
.
There are two types of residuals which may be used in each
recursion, Pearson residuals or score residuals, and in addition,
for the binomial distribution, identity residuals may be used. The
infinite moving average, , depends on the type of
residuals used, as do the final parameters obtained from the
filter. Standardisation of past observed counts is necessary to
avoid instability, therefore the user should choose the appropriate
type of residuals depending on the situation.
The method of estimation for parameters implemented in the function
aims to maximise the log likelihood by an iterative method commencing
from suitably chosen initial values for the parameters. Starting from
initial values for the vector of
parameters updates are obtained using the iterations
where is some
suitably chosen matrix.
Iterations continue for until convergence is
reached or the number of iterations
reaches a user specified
upper limit on maximum iterations in which case they will stop. The
convergence criterion used in our implementation is that based on
, the maximum of absolute values of the first
derivatives.
When is less than a user specified value
grad
the iterations stop. There are two methods of optimization of the
likelihood, Newton-Raphson and Fisher scoring. The method used is
specified by the argument method
. It should be noticed that if
the initial value for parameters are not chosen well, the
optimization of the likelihood might fail to converge. Care is needed
when fitting mixed ARMA specifications because there is potential for
the AR and MA parameters to be non-identifiable if the orders and
are too large. Lack of identifiability manifests itself in the
algorithm to optimize the likelihood failing to converge and/or the
hessian being singular—check the warning messages and convergence
error codes.
The function summary
(i.e., summary.glarma
)
can be used to obtain or print a summary of the results.
The generic accessor functions coef
(i.e.,
coef.glarma
), logLik
(i.e.,
logLik.glarma
), fitted
(i.e.,
fitted.glarma
), residuals
(i.e.,
residuals.glarma
), nobs
(i.e.,
nobs.glarma
), model.frame
(i.e.,
model.frame.glarma
) and extractAIC
(i.e.,
extractAIC.glarma
) can be used to extract various useful
features of the value returned by glarma
.
glarma
returns an object of class "glarma" with components:
delta |
a vector of coefficients for |
logLik |
the loglikelihood of the specific distribution. |
logLikDeriv |
the derivative of the loglikelhood of the specified distribution. |
logLikDeriv2 |
the second derivative of the loglikelihood of the specified distribution. |
eta |
the estimated linear predictor. |
mu |
the GLARMA estimated mean. |
fitted.values |
the GLARMA fitted values. |
residuals |
the residuals of the type specified. |
cov |
the estimated covariance matrix of the maximum likelihood estimators. |
phiLags |
vector of AR orders. |
thetaLags |
vector of MA orders. |
r |
the number of columns in the model matrix. |
pq |
the number of |
null.deviance |
the deviance from the initial GLM fit. |
df.null |
the degrees of freedom from the initial GLM fit. |
y |
the |
X |
the model matrix. |
offset |
the offset, |
type |
the distribution of the counts. |
method |
the method of iteration used. |
residType |
the type of the residuals returned. |
call |
the matched call. |
iter |
the number of iterations. |
errCode |
the error code; 0 indicating successful convergence of the iteration method, 1 indicating failure. |
WError |
error code for finiteness of |
min |
the minimum of the absolute value of the gradient. |
aic |
A version of Akaike's An Information Criterion, minus twice the maximized log-likelihood plus twice the number of parameters, computed by the aic component of the family. For binomial and Poisson families the dispersion is fixed at one and the number of parameters is the number of coefficients. |
The original GLARMA routine for Poisson responses was developed in collaboration with Richard A. Davis and Ying Wang. The binomial response version was developed with the assistance of Haolan Lu. The extension to negative binomial response was carried out by Bo Wang. Daniel Drescher contributed to the initial structure of the software used as the basis of the package.
The main author of the package is "William T.M. Dunsmuir" <[email protected]>. Package development was carried out by Cenanning Li supervised by David J. Scott.
Dunsmuir, William T. M. and Scott, David J. (2015) The glarma Package for Observation-Driven Time Series Regression of Counts. Journal of Statistical Software, 67(7), 1–36. http://dx.doi.org/10.18637/jss.v067.i07
Additional examples may be found in Asthma
,
OxBoatRace
, RobberyConvict
, and
DriverDeaths
.
### Example from Davis, Dunsmuir Wang (1999) ## MA(1,2,5), Pearson Residuals, Fisher Scoring data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## Score Type (GAS) Residuals, Fisher Scoring glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Score", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## Score Type (GAS) Residuals, Newton Raphson ## Note: Newton Raphson fails to converge from GLM initial estimates. ## Setting up the initial estimates by ourselves init.delta <- glarmamod$delta beta <- init.delta[1:6] thetaInit <- init.delta[7:9] glarmamod <- glarma(y, X, beta = beta, thetaLags = c(1, 2, 5), thetaInit = thetaInit, type ="Poi", method = "NR", residuals = "Score", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## AR(1,5), Pearson Residuals, Fisher Scoring glarmamod <- glarma(y, X, phiLags = c(1, 5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod)
### Example from Davis, Dunsmuir Wang (1999) ## MA(1,2,5), Pearson Residuals, Fisher Scoring data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## Score Type (GAS) Residuals, Fisher Scoring glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Score", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## Score Type (GAS) Residuals, Newton Raphson ## Note: Newton Raphson fails to converge from GLM initial estimates. ## Setting up the initial estimates by ourselves init.delta <- glarmamod$delta beta <- init.delta[1:6] thetaInit <- init.delta[7:9] glarmamod <- glarma(y, X, beta = beta, thetaLags = c(1, 2, 5), thetaInit = thetaInit, type ="Poi", method = "NR", residuals = "Score", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## AR(1,5), Pearson Residuals, Fisher Scoring glarmamod <- glarma(y, X, phiLags = c(1, 5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod)
Function used to generate initial values of parameters for the
GLARMA model from glm
or glm.nb
.
initial(y, X, offset = NULL, type = "Poi", alpha = 1)
initial(y, X, offset = NULL, type = "Poi", alpha = 1)
y |
Numeric vector; response variable. |
X |
Matrix; explanatory variables. A vector of ones should be
added to the data matrix as the first column for the
|
offset |
Either |
type |
Character; the distribution of the counts. |
alpha |
Numeric; an optional initial value for the
|
Generates and returns the initial parameters for the GLARMA model under the specified distribution by fitting a generalized linear model.
beta |
A named numeric vector of initial coefficients. |
y |
If requested, the |
X |
If requested, the model matrix. |
alpha |
The |
type |
The distribution of the counts in the GLARMA model. |
null.deviance |
Null deviance of the GLM with the same regression structure as the GLARMA model. |
df.null |
Null degrees of freedom of the GLM with the same regression structure as the GLARMA model. |
"William T.M. Dunsmuir" <[email protected]>
### Using the polio data data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glmMod <- initial(y, X, type = "Poi", alpha=1) str(glmMod) head(glmMod)
### Using the polio data data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glmMod <- initial(y, X, type = "Poi", alpha=1) str(glmMod) head(glmMod)
GLARMA
Fit
Function to carry out the likelihood ratio and Wald tests of serial
dependence when the alternative is a GLARMA process. This function
takes a "glarma"
object and uses its attributes to set up a GLM
fit that matches the GLARMA model regression structure. This is done
to ensure that the GLM object is the null hypothesis for testing
against the "glarma"
object.
likTests(object) likeTests(object) ## S3 method for class 'likTests' print(x, ...)
likTests(object) likeTests(object) ## S3 method for class 'likTests' print(x, ...)
object |
An object of class |
x |
An object of class |
... |
Further arguments passed to or from other methods. |
This function carries out the likelihood ratio and Wald tests for comparing the null model and the alternative model.
likeTests
is an alias for likTests
.
likTests
returns an object of class "likTests"
. A matrix is
shown with the statistics and p-value for each test. The significance
stars alongside help to identify any probabilities less than 0.05 or
0.01.
"William T.M. Dunsmuir" <[email protected]>
data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 2.22e-16) likTests(glarmamod) likeTests(glarmamod)
data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 2.22e-16) likTests(glarmamod) likeTests(glarmamod)
logLik
is a generic function which extracts the GLARMA model
log-likelihood from objects returned by modeling functions.
## S3 method for class 'glarma' logLik(object, deriv, ...)
## S3 method for class 'glarma' logLik(object, deriv, ...)
object |
An object of class |
deriv |
Numeric; either "0", "1"
or "2". It is used to choose and extract the log-likehood, its
derivative or its second derivative respectively from the
|
... |
Further arguments passed to or from other methods. |
This is an S3 generic function. logLik
returns the
log-likelihood, its derivative, or its second derivative from the
object of class glarma
based on the value of the argument
deriv
. "0" is for the log-likelihood, "1" is for the first
derivative of log-likelihood and "2" is for the second derivative of
the log-likelihood.
The log-likelihood, the derivative of the log-likelihood or the second
derivative of the log-likelihood extracted from the GLARMA model
object object
.
coef.glarma
, residuals.glarma
,
fitted.glarma
, glarma
.
data(Polio) Y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(Y, X, thetaLags = c(1, 2, 5), type = "Poi", method ="FS", residuals = "Pearson", maxit = 100 , grad = 1e-6) logLik(glarmamod, deriv = 0) logLik(glarmamod, deriv = 1) logLik(glarmamod, deriv = 2)
data(Polio) Y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(Y, X, thetaLags = c(1, 2, 5), type = "Poi", method ="FS", residuals = "Pearson", maxit = 100 , grad = 1e-6) logLik(glarmamod, deriv = 0) logLik(glarmamod, deriv = 1) logLik(glarmamod, deriv = 2)
model.frame
(a generic function) and its methods return a
data frame with the variables that are used in the
glarma
model.
## S3 method for class 'glarma' model.frame(formula, ...)
## S3 method for class 'glarma' model.frame(formula, ...)
formula |
An object of class |
... |
Further arguments passed to or from other methods. |
This is an S3 generic function. It extracts the response variable
vector and the matrix of the explanatory variables from the object of
class "glarma"
, and combines them as a data frame.
A data frame with the variables used in the fitted
glarma
model.
Cenanning Li <[email protected]>
coef.glarma
, residuals.glarma
,
fitted.glarma
, glarma
.
data(Polio) print(y <- Polio[, 2]) X <- as.matrix(Polio[, 3:8]) str(X) head(X) glarmamod <- glarma(y, X, thetaLags = c(1, 2, 5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) str(model.frame(glarmamod)) head(model.frame(glarmamod))
data(Polio) print(y <- Polio[, 2]) X <- as.matrix(Polio[, 3:8]) str(X) head(X) glarmamod <- glarma(y, X, thetaLags = c(1, 2, 5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) str(model.frame(glarmamod)) head(model.frame(glarmamod))
Inverts the second derivative matrix of the log-likelihood to obtain the estimated covariance matrix of the parameters.
mySolve(A)
mySolve(A)
A |
Matrix; the negative second derivative of the log-likelihood |
mySolve
attempts to invert its matrix argument. If the matrix
supplied is not invertible, ErrCode
is set to 1.
Ainv |
inverse of the negative second derivative of the loglikelihood. If the inverse is unable to be obtained, returns the original negative second derivative of the log-likelihood. |
ErrCode |
Numeric; 0 if the inverse can be found, 1 if not. |
"William T.M. Dunsmuir" <[email protected]>
### Using the polio data data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) ## Construct the vectors of phi lags and theta lags theta.lags <- c(1, 2, 5) phi.lags <- rep(0, 0) ## Construct the initial delta vector delta <- c("Intcpt" = 0.2069383, "Trend" = -4.7986615 , "CosAnnual" = -0.1487333, "SinAnnual" = -0.5318768, "CosSemiAnnual" = 0.1690998, "SinSemiAnnual" = -0.4321435, "theta_1" = 0, "theta_2"= 0, "theta_5"= 0 ) ## Calculate the second derivative of the loglikelihood glarmamod <- glarmaPoissonPearson(y, X, delta = delta, phiLags = phi.lags, thetaLags = theta.lags, method = "FS") ## estimate the covariance matrix of the estimators from the second ## derivative of the loglikelihood mySolve(-glarmamod$ll.dd)
### Using the polio data data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) ## Construct the vectors of phi lags and theta lags theta.lags <- c(1, 2, 5) phi.lags <- rep(0, 0) ## Construct the initial delta vector delta <- c("Intcpt" = 0.2069383, "Trend" = -4.7986615 , "CosAnnual" = -0.1487333, "SinAnnual" = -0.5318768, "CosSemiAnnual" = 0.1690998, "SinSemiAnnual" = -0.4321435, "theta_1" = 0, "theta_2"= 0, "theta_5"= 0 ) ## Calculate the second derivative of the loglikelihood glarmamod <- glarmaPoissonPearson(y, X, delta = delta, phiLags = phi.lags, thetaLags = theta.lags, method = "FS") ## estimate the covariance matrix of the estimators from the second ## derivative of the loglikelihood mySolve(-glarmamod$ll.dd)
An accessor function used to extract the number of observations from a
"glarma"
object.
## S3 method for class 'glarma' nobs(object, ...)
## S3 method for class 'glarma' nobs(object, ...)
object |
An object of class |
... |
Further arguments passed to or from other methods. |
The number of observations extracted from the object object
.
"Cenanning Li" <[email protected]>
coef.glarma
, residuals.glarma
,
fitted.glarma
, glarma
.
### Example from Davis, Dunsmuir Wang (1999) ## MA(1,2,5), Pearson Residuals, Fisher Scoring data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 2.22e-16) nobs(glarmamod)
### Example from Davis, Dunsmuir Wang (1999) ## MA(1,2,5), Pearson Residuals, Fisher Scoring data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 2.22e-16) nobs(glarmamod)
Function to create the normalized conditional (randomized) quantile residuals.
normRandPIT(object)
normRandPIT(object)
object |
an object of class "glarma" |
The function glarmaPredProb
produces the non-randomized
probability integral transformation (PIT). It returns estimates of the
cumulative predictive probabilities as upper and lower bounds of a
collection of intervals. If the model is correct, a histogram drawn
using these estimated probabilities should resemble a histogram
obtained from a sample from the uniform distribution. This function
aims to produce observations which instead resemble a sample from a
normal distribution. Such a sample can then be examined by the usual
tools for checking normality, such as histograms, Q-Q normal plots and
for checking independence, autocorrelation and partial autocorrelation
plots, and associated portmanteau statistics.
For each of the intervals produced by glarmaPredProb
, a
random uniform observation is generated, which is then converted to a
normal observation by applying the inverse standard normal
distribution function (that is qnorm
). The vector of
these values is returned by the function in the list element
rt
. In addition non-random observations which should appear
similar to a sample from a normal distribution are obtained by
applying qnorm
to the mid-points of the predictive distribution
intervals. The vector of these values is returned by the function in
the list element rtMid
.
A list consisting of two elements:
rt |
the normalized conditional (randomized) quantile residuals |
rtMid |
the midpoints of the predictive probability intervals |
"William T.M. Dunsmuir" <[email protected]> and "David J Scott" <[email protected]>
Berkowitz, J. (2001) Testing density forecasts, with applications to risk management. Journal of Business \& Economic Statistics, 19, 465–474.
Dunn, Peter K. and Smyth, Gordon K. (1996) Randomized quantile residuals. Journal of Computational and Graphical Statistics, 5, 236–244.
See also as glarmaPredProb
.
data(DriverDeaths) y <- DriverDeaths[, "Deaths"] X <- as.matrix(DriverDeaths[, 2:5]) Population <- DriverDeaths[, "Population"] ### Offset included glarmamodOffset <- glarma(y, X, offset = log(Population/100000), phiLags = c(12), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) rt <- normRandPIT(glarmamodOffset)$rt par(mfrow = c(2,2)) hist(rt, main = "Histogram of Randomized Residuals", xlab = expression(r[t])) box() qqnorm(rt, main = "Q-Q Plot of Randomized Residuals" ) abline(0, 1, lty = 2) acf(rt, main = "ACF of Randomized Residuals") pacf(rt, main = "PACF of Randomized Residuals")
data(DriverDeaths) y <- DriverDeaths[, "Deaths"] X <- as.matrix(DriverDeaths[, 2:5]) Population <- DriverDeaths[, "Population"] ### Offset included glarmamodOffset <- glarma(y, X, offset = log(Population/100000), phiLags = c(12), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 1e-6) rt <- normRandPIT(glarmamodOffset)$rt par(mfrow = c(2,2)) hist(rt, main = "Histogram of Randomized Residuals", xlab = expression(r[t])) box() qqnorm(rt, main = "Q-Q Plot of Randomized Residuals" ) abline(0, 1, lty = 2) acf(rt, main = "ACF of Randomized Residuals") pacf(rt, main = "PACF of Randomized Residuals")
Results of the boat race between Oxford and Cambridge from 1829–2011.
data(OxBoatRace)
data(OxBoatRace)
A data frame containing the following columns:
[, 1] | Year | Year in which the race occurred. Some years are missing when the race was not run. |
[, 2] | Intercept | A vector of ones, providing the intercept in the model. |
[, 3] | Camwin | A binary response, zero for an Oxford win, one for a Cambridge win. |
[, 4] | WinnerWeight | Weight of winning team's crew. |
[, 5] | LoserWeight | Weight of losing team's crew. |
[, 6] | Diff | Difference between winning team's weight and losing team's weight. |
Klingenberg, Bernhard (2008) Regression models for binary time series with gaps. Computational Statistics & Data Analysis, 52, 4076–4090.
### Example with Oxford-Cambridge Boat Race data(OxBoatRace) y1 <- OxBoatRace$Camwin n1 <- rep(1, length(OxBoatRace$Year)) Y <- cbind(y1, n1 - y1) X <- cbind(OxBoatRace$Intercept, OxBoatRace$Diff) colnames(X) <- c("Intercept", "Weight Diff") oxcamglm <- glm(Y ~ Diff + I(Diff^2), data = OxBoatRace, family = binomial(link = "logit"), x = TRUE) summary(oxcamglm) X <- oxcamglm$x glarmamod <- glarma(Y, X, thetaLags = c(1, 2), type = "Bin", method = "NR", residuals = "Pearson", maxit = 100, grad = 1e-6) summary(glarmamod) likTests(glarmamod) ## Plot Probability of Cambridge win versus Cambridge Weight advantage: beta <- coef(glarmamod, "beta") par(mfrow = c(1, 1)) plot(OxBoatRace$Diff, 1 / (1 + exp(-(beta[1] + beta[2] * OxBoatRace$Diff + beta[3] * OxBoatRace$Diff^2))), ylab = "Prob", xlab = "Weight Diff") title("Probability of Cambridge win \n versus Cambridge weight advantage") ## Residuals and fit plots par(mfrow=c(3, 2)) plot.glarma(glarmamod)
### Example with Oxford-Cambridge Boat Race data(OxBoatRace) y1 <- OxBoatRace$Camwin n1 <- rep(1, length(OxBoatRace$Year)) Y <- cbind(y1, n1 - y1) X <- cbind(OxBoatRace$Intercept, OxBoatRace$Diff) colnames(X) <- c("Intercept", "Weight Diff") oxcamglm <- glm(Y ~ Diff + I(Diff^2), data = OxBoatRace, family = binomial(link = "logit"), x = TRUE) summary(oxcamglm) X <- oxcamglm$x glarmamod <- glarma(Y, X, thetaLags = c(1, 2), type = "Bin", method = "NR", residuals = "Pearson", maxit = 100, grad = 1e-6) summary(glarmamod) likTests(glarmamod) ## Plot Probability of Cambridge win versus Cambridge Weight advantage: beta <- coef(glarmamod, "beta") par(mfrow = c(1, 1)) plot(OxBoatRace$Diff, 1 / (1 + exp(-(beta[1] + beta[2] * OxBoatRace$Diff + beta[3] * OxBoatRace$Diff^2))), ylab = "Prob", xlab = "Weight Diff") title("Probability of Cambridge win \n versus Cambridge weight advantage") ## Residuals and fit plots par(mfrow=c(3, 2)) plot.glarma(glarmamod)
Functions which use the arguments of a glarma
call to
generate the initial delta
, theta
and phi
vectors.
deltaGen(y, X, offset = NULL, phiInit, thetaInit, type, alpha, beta, alphaInit) thetaGen(thetaLags, thetaInit) phiGen(phiLags, phiInit)
deltaGen(y, X, offset = NULL, phiInit, thetaInit, type, alpha, beta, alphaInit) thetaGen(thetaLags, thetaInit) phiGen(phiLags, phiInit)
y |
Numeric vector; response variable. |
X |
Matrix; the explanatory variables. A vector of ones should be
added to the data matrix as the first column for the |
offset |
Either |
phiInit |
Numeric vector; initial values for the corresponding AR orders. |
thetaInit |
Numeric vector; initial values for the corresponding orders. |
type |
Character; the count distribution. The default is the Poisson distribution. |
beta |
Numeric vector; initial values of the parameters of
variables. It is for the user to construct the specific |
alpha |
Numeric; an optional initial |
alphaInit |
Numeric; an initial |
thetaLags |
Numeric vector; MA orders |
phiLags |
Numeric vector; AR orders |
The thetaGen
and phiGen
functions take the arguments,
thetaLags
, phiLags
, thetaInit
and phiInit
,
in a glarma
call to generate and return the initial
theta
and phi
vectors with orders corresponding to their
names. Then the deltaGen
function uses the values returned by
thetaGen
, phiGen
and other arguments in the
glarma
call to generate and return the initial
delta
vector with correct names.
thetaGen
returns a list containing thetaLags
and
thetaInit
. thetaInit
is the initial theta
vector
with its corresponding MA orders as its names.
phiGen
returns a list containing phiLags
and
phiInit
. phiInit
is the initial phi
vector
with its corresponding MA orders as its names.
deltaGen
returns a named vector giving the values of
beta
, phiInit
, thetaInit
plus alpha
in the
negative binomial case.
"Cenanning Li" <[email protected]> and "William T.M. Dunsmuir" <[email protected]>
### Using the polio data data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) ## generate the theta vector theta.lags <- c(1, 2, 5) theta.init <- c(0.0, 0.0, 0.0) theta <- thetaGen(theta.lags, theta.init) print(thetaLags <- theta[[1]]) print(theta.init <- theta[[2]]) ## generate the vector of phi phi.lags <- rep(0, 0) phi.init <- rep(0, 0) phi <- phiGen(phi.lags, phi.init) print(phiLags <- phi[[1]]) print(phi.init <- phi[[2]]) ## generate the delta vector delta <- deltaGen(y = y, X = X, phiInit = phi.init, thetaInit = theta.init, type = "Poi", alpha = 1) delta
### Using the polio data data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) ## generate the theta vector theta.lags <- c(1, 2, 5) theta.init <- c(0.0, 0.0, 0.0) theta <- thetaGen(theta.lags, theta.init) print(thetaLags <- theta[[1]]) print(theta.init <- theta[[2]]) ## generate the vector of phi phi.lags <- rep(0, 0) phi.init <- rep(0, 0) phi <- phiGen(phi.lags, phi.init) print(phiLags <- phi[[1]]) print(phi.init <- phi[[2]]) ## generate the delta vector delta <- deltaGen(y = y, X = X, phiInit = phi.init, thetaInit = theta.init, type = "Poi", alpha = 1) delta
Functions to produce the non-randomized probability integral transform (PIT) to check the adequacy of the distributional assumption of the GLARMA model.
glarmaPredProb(object) glarmaPIT(object, bins = 10)
glarmaPredProb(object) glarmaPIT(object, bins = 10)
object |
An object of class |
bins |
Numeric; the number of bins used in the PIT. |
These functions are used for the assessment of predictive distributions in discrete data. They obtain the predictive probabilities and the probability integral transformation for a fitted GLARMA model.
glarmaPredProb
returns a list with values:
upper |
the predictive cumulative probabilities used as the upper bound for computing the non-randomized PIT. |
lower |
the predictive cumulative probabilities used as the lower bound for computing the non-randomized PIT. |
glarmaPIT
returns a list with values:
upper |
the predictive cumulative probabilities used as the upper bound for computing the non-randomized PIT. |
lower |
the predictive cumulative probabilities used as the lower bound for computing the non-randomized PIT. |
conditionalPIT |
the conditional probability integral transformation given the observed counts. |
PIT |
the probability integral transformation. |
"David J. Scott" <[email protected]> and "Cenanning Li" <[email protected]>
Czado, Claudia and Gneiting, Tilmann and Held, Leonhard (2009) Predictive model assessment for count data. Biometrics, 65, 1254–1261.
Jung, Robert.C and Tremayne, A.R (2011) Useful models for time series of counts or simply wrong ones? Advances in Statistical Analysis, 95, 59–91.
### Example from Davis, Dunsmuir Wang (1999) ## MA(1,2,5), Pearson Residuals, Fisher Scoring data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 2.22e-16) glarmaPredProb(glarmamod) glarmaPIT(glarmamod)
### Example from Davis, Dunsmuir Wang (1999) ## MA(1,2,5), Pearson Residuals, Fisher Scoring data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1,2,5), type = "Poi", method = "FS", residuals = "Pearson", maxit = 100, grad = 2.22e-16) glarmaPredProb(glarmamod) glarmaPIT(glarmamod)
glarma
ObjectTen plots (selectable by which
) are currently available: a
time series plot with observed values of the dependent variable, fixed
effects fit, and GLARMA fit; an ACF plot of residuals; a plot
of residuals against time; a normal Q-Q plot; the PIT histogram;
a uniform Q-Q plot for the PIT; a histogram of the normal randomized
residuals; a Q-Q plot of the normal randomized residuals; a plot of
the autocorrelation of the normal randomized residuals; and a plot of
the partial autocorrelation of the normal randomized residuals. By
default, six plots are provided, numbers 1, 3, 5, 7, 8 and 9 from this
list of plots.
## S3 method for class 'glarma' plot(x, which = c(1L,3L,5L,7L,8L,9L), fits = 1L:3L, ask = prod(par("mfcol")) < length(which) && dev.interactive(), lwdObs = 1, lwdFixed = 1, lwdGLARMA = 1, colObs = "black", colFixed = "blue", colGLARMA = "red", ltyObs = 2, ltyFixed = 1, ltyGLARMA = 1, pchObs = 1, legend = TRUE, residPlotType = "h", bins = 10, line = TRUE, colLine = "red", colHist = "royal blue", lwdLine = 2, colPIT1 = "red", colPIT2 = "black", ltyPIT1 = 1, ltyPIT2 = 2, typePIT = "l", ltyQQ = 2, colQQ = "black", titles, ...)
## S3 method for class 'glarma' plot(x, which = c(1L,3L,5L,7L,8L,9L), fits = 1L:3L, ask = prod(par("mfcol")) < length(which) && dev.interactive(), lwdObs = 1, lwdFixed = 1, lwdGLARMA = 1, colObs = "black", colFixed = "blue", colGLARMA = "red", ltyObs = 2, ltyFixed = 1, ltyGLARMA = 1, pchObs = 1, legend = TRUE, residPlotType = "h", bins = 10, line = TRUE, colLine = "red", colHist = "royal blue", lwdLine = 2, colPIT1 = "red", colPIT2 = "black", ltyPIT1 = 1, ltyPIT2 = 2, typePIT = "l", ltyQQ = 2, colQQ = "black", titles, ...)
x |
An object of class |
which |
Numeric; if a subset of the plots is required, specify a subset of the numbers 1:10. 1 is the time series plot with observed values of the dependent variable, fixed effects fit, and GLARMA fit. 2 is the ACF plot of residuals. 3 is a plot of residuals against time. 4 is the normal Q-Q plot. 5 is the PIT histogram. 6 is the uniform Q-Q plot for the PIT. 7 is the histogram of the normal randomized residuals. 8 is the Q-Q plot of the normal randomized residuals. 9 is the autocorrelation of the normal randomized residuals. 10 is the partial autocorrelation of the normal randomized residuals. By default, plots 1, 3, 5, 7, 8 and 9 are provided. |
fits |
Numeric; if a subset of fits on the time series plot is required, specify a subset of the numbers 1:3. 1 is the observed values of the dependent variable, 2 is the fixed effects fit, and 3 is GLARMA fit. By default, all fits are provided. |
ask |
Logical; if |
lwdObs |
Numeric; the line widths for lines of the observed values of the dependent variable appearing in the time series plot. |
lwdFixed |
Numeric; the line widths for lines of the fixed effects fit appearing in the time series plot. |
lwdGLARMA |
Numeric; the line widths for lines of GLARMA fit appearing in the time series plot. |
ltyObs |
An integer or character string; the line types for the
line of the observed data of the dependent variable appearing in the
time series plot, see |
ltyFixed |
An integer or character string; the line types for the
line of the fixed effects fit appearing in the time series plot, see
|
ltyGLARMA |
An integer or character string; the line types for the
line of GLARMA fit appearing in the time series plot, see
|
pchObs |
Numeric; the point type for the point of the observed data of the dependent variable appearing in the time series plot. |
colObs |
Numeric or character; the colour of lines or points of the observed data of the dependent variable appearing in the time series plot. |
colFixed |
Numeric or character; the colour of lines of the fixed effects fit appearing in the time series plot. |
colGLARMA |
Numeric or character; the colour of lines of GLARMA fit appearing in the time series plot. |
legend |
Logical; if |
residPlotType |
A 1-character string giving the type of plot
desired. The following values are possible, for details, see
|
bins |
Numeric; the number of bins shown in the PIT histogram and of the number of breaks in the histogram of the normal randomized residuals. By default, it is 10. |
line |
Logical; if |
colLine |
Numeric or character; the colour of the line for comparison in the PIT histogram. |
lwdLine |
Numeric; the line widths for the comparison line in the PIT histogram. |
colHist |
Numeric or character; the colour of the histogram for the PIT, and of the histogram of the normal randomized residuals. |
colPIT1 |
Numeric or character; the colour of the sample uniform Q-Q plot in the PIT. |
colPIT2 |
Numeric or character; the colour of the theoretical uniform Q-Q plot in the PIT. |
ltyPIT1 |
An integer or character string; the line types for the
sample uniform Q-Q plot in the PIT, see |
ltyPIT2 |
An integer or character string; the line types for the
theoretical uniform Q-Q plot in the PIT, see |
typePIT |
A 1-character string; the type of plot for the sample uniform Q-Q plot in the PIT. |
ltyQQ |
An integer or character string; the line type for the
normal Q-Q plot of the normal randomized residuals, see
|
colQQ |
Numeric or character; the colour of the line in the normal Q-Q plot of the normal randomized residuals. |
titles |
A list of the same length as |
... |
Further arguments passed to |
plot.glarma
is an S3 generic function for objects of class
glarma
.
The plots in this method display the fixed effects fit, GLARMA fit and
various types of residuals for the GLARMA fit under the Poisson
distribution, the binomial distribution or the negative binomial
distribution, plus a number of plots of the randomized residuals (see
normRandPIT
for details of the randomized residuals). In
all, ten plots can be produced. The observed values of the dependent
variable shown in the time series plot are mainly used to compare with
the two fits.
The fixed effects fit is calculated from , the
multiplication of the data matrix
X
and
coefficients in GLARMA model. In contrast, the GLARMA fit is
calculated from
, the product of the data matrix
X
and in the GLARMA model, which is the combination
of both the
and ARMA coefficients, and is also
called the state variable of the series.
There are some major differences for computing the fixed effects fit
from and the GLARMA fit from
under
different distributions.
Under the Poisson distribution and negative binomial distribution,
and
Under the binomial distribution,
and
The residuals are calculated from the observed data and GLARMA
fit. The exact computation for the residuals depends on the type
of residuals used. The details are given in
glarma
. The ACF plot, the residuals against time and
the normal Q-Q plot are all based on these residuals. Further details
about those three plots are passed to acf
and
qqnorm
.
There are four plots based on the randomized residuals calculated
using normRandPIT
. These are a histogram, a Q-Q plot,
an autocorrelation plot and a partial autocorrelation plot.
The number of plots to be shown in the window depends on the value of
the graphical parameter mfrow
(or mfcol
). If the
displayed window is set to be large enough to show all ten plots,
they will be shown at one time. Otherwise, the required number of
plots will appear each time in the displayed window, and the user
will need to enter return
to see subsequent plots. By default,
six plots are produced.
For the time series plot in the function, the fit displayed is
specified by the argument fits
. The legend will be shown if
legend
is TRUE
. It will appear under the title
of the time series plot. Also the legend and the title will alter
automatically according to the fits shown in the plot.
"Cenanning Li" <[email protected]>
plot.ts
, qqnorm
, acf
,
plot.default
, normRandPIT
.
### A example from Davis, Dunsmuir Wang (1999) ## MA(1,2,5), Pearson Residuals, Fisher Scoring data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1, 2, 5), type = "Poi",method = "FS", residuals = "Pearson", maxit = 100 , grad = 1e-6) ## The default plots are shown plot(glarmamod) ## The plots used only to compared GLARMA fit and the observed data plot(glarmamod, which = 1L, fits = c(1, 3))
### A example from Davis, Dunsmuir Wang (1999) ## MA(1,2,5), Pearson Residuals, Fisher Scoring data(Polio) y <- Polio[, 2] X <- as.matrix(Polio[, 3:8]) glarmamod <- glarma(y, X, thetaLags = c(1, 2, 5), type = "Poi",method = "FS", residuals = "Pearson", maxit = 100 , grad = 1e-6) ## The default plots are shown plot(glarmamod) ## The plots used only to compared GLARMA fit and the observed data plot(glarmamod, which = 1L, fits = c(1, 3))
glarma
Object
Two plots for the non-randomized PIT are currently available for checking the distributional assumption of the fitted GLARMA model: the PIT histogram, and the uniform Q-Q plot for PIT.
histPIT(object, bins = 10, line = TRUE, colLine = "red", colHist = "royal blue", lwdLine = 2, main = NULL, ...) qqPIT(object, bins = 10, col1 = "red", col2 = "black", lty1 = 1, lty2 = 2, type = "l", main = NULL, ...)
histPIT(object, bins = 10, line = TRUE, colLine = "red", colHist = "royal blue", lwdLine = 2, main = NULL, ...) qqPIT(object, bins = 10, col1 = "red", col2 = "black", lty1 = 1, lty2 = 2, type = "l", main = NULL, ...)
object |
An object of class |
bins |
Numeric; the number of bins shown in the PIT histogram or the PIT Q-Q plot. By default, it is 10. |
line |
Logical; if |
colLine |
Numeric or character; the colour of the line for comparison in PIT histogram. |
lwdLine |
Numeric; the line widths for the comparison line in PIT histogram. |
colHist |
Numeric or character; the colour of the histogram for PIT. |
col1 |
Numeric or character; the colour of the sample uniform Q-Q plot in PIT. |
col2 |
Numeric or character; the colour of the theoretical uniform Q-Q plot in PIT. |
lty1 |
An integer or character string; the line types for the
sample uniform Q-Q plot in PIT, see |
lty2 |
An integer or character string; the line types for the
theoretical uniform Q-Q plot in PIT, see |
type |
A 1-character string; the type of plot for the sample uniform Q-Q plot in PIT. |
main |
A character string giving a title. For each plot the default provides a useful title. |
... |
Further arguments passed to |
The histogram and the Q-Q plot are used to compare the fitted profile with U(0, 1). If they match relatively well, it means the distributional assumption is satisfied.
"David J. Scott" <[email protected]> and "Cenanning Li" <[email protected]>
Czado, Claudia and Gneiting, Tilmann and Held, Leonhard (2009) Predictive model assessment for count data. Biometrics, 65, 1254–1261.
Jung, Robert.C and Tremayne, A.R (2011) Useful models for time series of counts or simply wrong ones? AStA Advances in Statistical Analysis, 95, 59–91.
## For examples see example(plot.glarma)
## For examples see example(plot.glarma)
This data set gives the monthly number of cases of poliomyelitis in the U.S. for the years 1970–1983 as reported by the Center for Disease Control. The polio data frame has 168 rows and 8 columns.
data(Polio)
data(Polio)
A data frame containing the following columns:
[, 1] | Cases | monthly number of cases of poliomyelitis. |
[, 2] | Intcpt | a vector of ones, providing the intercept in the model. |
[, 3] | Trend | a linear trend. |
[, 4] | CosAnnual | cosine harmonics at periods of 12. |
[, 5] | SinAnnual | sine harmonics at periods of 12. |
[, 6] | CosSemiAnnual | cosine harmonics at periods of 6. |
[, 7] | SinSemiAnnual | sine harmonics at periods of 6. |
Zeger, S.L (1988) A regression model for time series of counts. Biometrika, 75, 621–629.
residuals
is a generic function which extracts model residuals
from objects returned by the modeling function
glarma
. resid
is an alias for residuals
.
## S3 method for class 'glarma' residuals(object, ...)
## S3 method for class 'glarma' residuals(object, ...)
object |
An object of class |
... |
Further arguments passed to or from other methods. |
Residuals extracted from the object object
.
"William T.M. Dunsmuir" <[email protected]> and "Cenanning Li" <[email protected]>
coefficients.glarma
, fitted.glarma
,
glarma
.
Monthly counts of charges laid and convictions made in Local Courts and Higher Court in armed robbery in New South Wales from 1995–2007.
data(RobberyConvict)
data(RobberyConvict)
A data frame containing the following columns:
[, 1] | Date | Date in month/year format. |
[, 2] | Incpt | A vector of ones, providing the intercept in the model. |
[, 3] | Trend | Scaled time trend. |
[, 4] | Step.2001 | Unit step change from 2001 onwards. |
[, 5] | Trend.2001 | Change in trend term from 2001 onwards. |
[, 6] | HC.N | Monthly number of cases for robbery (Higher Court). |
[, 7] | HC.Y | Monthly number of convictions for robbery (Higher court). |
[, 8] | HC.P | Proportion of convictions to charges for robbery (Higher court). |
[, 9] | LC.N | Monthly number of cases for robbery (Lower court). |
[, 10] | LC.Y | Monthly number of convictions for robbery (Lower court). |
[, 11] | LC.P | Proportion of convictions to charges for robbery (Lower court). |
Dunsmuir, William TM, Tran, Cuong, and Weatherburn, Don (2008) Assessing the Impact of Mandatory DNA Testing of Prison Inmates in NSW on Clearance, Charge and Conviction Rates for Selected Crime Categories.
### Example with Robbery Convictions data(RobberyConvict) datalen <- dim(RobberyConvict)[1] monthmat <- matrix(0, nrow = datalen, ncol = 12) dimnames(monthmat) <- list(NULL, c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) months <- unique(months(strptime(RobberyConvict$Date, format = "%m/%d/%Y"), abbreviate=TRUE)) for (j in 1:12) { monthmat[months(strptime(RobberyConvict$Date, "%m/%d/%Y"), abbreviate = TRUE) == months[j], j] <-1 } RobberyConvict <- cbind(rep(1, datalen), RobberyConvict, monthmat) rm(monthmat) ## LOWER COURT ROBBERY y1 <- RobberyConvict$LC.Y n1 <- RobberyConvict$LC.N Y <- cbind(y1, n1-y1) glm.LCRobbery <- glm(Y ~ Step.2001 + I(Feb + Mar + Apr + May + Jun + Jul) + I(Aug + Sep + Oct + Nov + Dec), data = RobberyConvict, family = binomial(link = logit), na.action = na.omit, x = TRUE) summary(glm.LCRobbery, corr = FALSE) X <- glm.LCRobbery$x ## Newton Raphson glarmamod <- glarma(Y, X, phiLags = c(1), type = "Bin", method = "NR", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## LRT, Wald tests. likTests(glarmamod) ## Residuals and Fit Plots plot.glarma(glarmamod) ## HIGHER COURT ROBBERY y1 <- RobberyConvict$HC.Y n1 <- RobberyConvict$HC.N Y <- cbind(y1, n1-y1) glm.HCRobbery <- glm(Y ~ Trend + Trend.2001 + I(Feb + Mar + Apr + May + Jun) + Dec, data = RobberyConvict, family = binomial(link = logit), na.action = na.omit, x = TRUE) summary(glm.HCRobbery,corr = FALSE) X <- glm.HCRobbery$x ## Newton Raphson glarmamod <- glarma(Y, X, phiLags = c(1, 2, 3), type = "Bin", method = "NR", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## LRT, Wald tests. likTests(glarmamod) ## Residuals and Fit Plots plot.glarma(glarmamod)
### Example with Robbery Convictions data(RobberyConvict) datalen <- dim(RobberyConvict)[1] monthmat <- matrix(0, nrow = datalen, ncol = 12) dimnames(monthmat) <- list(NULL, c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) months <- unique(months(strptime(RobberyConvict$Date, format = "%m/%d/%Y"), abbreviate=TRUE)) for (j in 1:12) { monthmat[months(strptime(RobberyConvict$Date, "%m/%d/%Y"), abbreviate = TRUE) == months[j], j] <-1 } RobberyConvict <- cbind(rep(1, datalen), RobberyConvict, monthmat) rm(monthmat) ## LOWER COURT ROBBERY y1 <- RobberyConvict$LC.Y n1 <- RobberyConvict$LC.N Y <- cbind(y1, n1-y1) glm.LCRobbery <- glm(Y ~ Step.2001 + I(Feb + Mar + Apr + May + Jun + Jul) + I(Aug + Sep + Oct + Nov + Dec), data = RobberyConvict, family = binomial(link = logit), na.action = na.omit, x = TRUE) summary(glm.LCRobbery, corr = FALSE) X <- glm.LCRobbery$x ## Newton Raphson glarmamod <- glarma(Y, X, phiLags = c(1), type = "Bin", method = "NR", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## LRT, Wald tests. likTests(glarmamod) ## Residuals and Fit Plots plot.glarma(glarmamod) ## HIGHER COURT ROBBERY y1 <- RobberyConvict$HC.Y n1 <- RobberyConvict$HC.N Y <- cbind(y1, n1-y1) glm.HCRobbery <- glm(Y ~ Trend + Trend.2001 + I(Feb + Mar + Apr + May + Jun) + Dec, data = RobberyConvict, family = binomial(link = logit), na.action = na.omit, x = TRUE) summary(glm.HCRobbery,corr = FALSE) X <- glm.HCRobbery$x ## Newton Raphson glarmamod <- glarma(Y, X, phiLags = c(1, 2, 3), type = "Bin", method = "NR", residuals = "Pearson", maxit = 100, grad = 1e-6) glarmamod summary(glarmamod) ## LRT, Wald tests. likTests(glarmamod) ## Residuals and Fit Plots plot.glarma(glarmamod)
summary
method for class glarma
and functions to
generate the estimates for this summary
method.
## S3 method for class 'glarma' summary(object, tests = TRUE, ...) ## S3 method for class 'summary.glarma' print(x, digits = max(3L, getOption("digits") - 3L), ...) glarmaModelEstimates(object)
## S3 method for class 'glarma' summary(object, tests = TRUE, ...) ## S3 method for class 'summary.glarma' print(x, digits = max(3L, getOption("digits") - 3L), ...) glarmaModelEstimates(object)
object |
An object of class |
x |
An object of class |
digits |
Numeric; minimum number of significant digits to be used for most numbers. |
tests |
Logical; if |
... |
Further arguments passed to or from other methods. |
summary.glarma
returns an object of class
"summary.glarma"
, a list with components
call |
the component from |
null.deviance |
null deviance of the GLM with the same regression structure as the GLARMA model. |
df.null |
null degrees of freedom of the GLM with the same regression structure as the GLARMA model. |
phi.lags |
the component from |
theta.lags |
the component from |
pq |
the component from |
iter |
the component from |
deviance |
the deviance of the fitted model. |
df.residual |
the degrees of freedom of the fitted model. |
deviance.resid |
the component from |
aic |
the component from |
methods |
vector specifying the count distribution of the GLARMA model, the iteration method and the type of residual used. |
tests |
whether tests were asked for. |
likTests |
if |
coefficients1 |
the matrix of beta coefficients, standard errors, z-ratio and p-values. |
coefficients2 |
the matrix of ARMA coefficients, standard errors, z-ratio and p-values. |
coefficients3 |
when the count distribution is negative binomial, a matrix with 1 row, giving the negative binomial parameter, its standard error, z-ratio and p-value. |
"William T.M. Dunsmuir" <[email protected]> and "Cenanning Li" <[email protected]>
## For examples see example(glarma)
## For examples see example(glarma)