Description Usage Arguments Details See Also Examples
Interface to lm.wfit for fitting dynamic linear models
and time series regression relationships.
1 2 3 |
formula |
a |
data |
an optional |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
weights |
an optional vector of weights to be used
in the fitting process. If specified, weighted least squares is used
with weights |
na.action |
a function which indicates what should happen
when the data contain |
method |
the method to be used; for fitting, currently only
|
model, x, y, qr |
logicals. If |
singular.ok |
logical. If |
contrasts |
an optional list. See the |
offset |
this can be used to specify an a priori
known component to be included in the linear predictor
during fitting. An |
start |
start of the time period which should be used for fitting the model. |
end |
end of the time period which should be used for fitting the model. |
... |
additional arguments to be passed to the low level regression fitting functions. |
The interface and internals of dynlm are very similar to lm,
but currently dynlm offers three advantages over the direct use of
lm: 1. extended formula processing, 2. preservation of time series
attributes, 3. instrumental variables regression (via two-stage least squares).
For specifying the formula of the model to be fitted, there are
additional functions available which allow for convenient specification
of dynamics (via d() and L()) or linear/cyclical patterns
(via trend(), season(), and harmon()).
All new formula functions require that their arguments are time
series objects (i.e., "ts" or "zoo").
Dynamic models: An example would be d(y) ~ L(y, 2), where
d(x, k) is diff(x, lag = k) and L(x, k) is
lag(x, lag = -k), note the difference in sign. The default
for k is in both cases 1. For L(), it
can also be vector-valued, e.g., y ~ L(y, 1:4).
Trends: y ~ trend(y) specifies a linear time trend where
(1:n)/freq is used by default as the regressor. n is the
number of observations and freq is the frequency of the series
(if any, otherwise freq = 1). Alternatively, trend(y, scale = FALSE)
would employ 1:n and time(y) would employ the original time index.
Seasonal/cyclical patterns: Seasonal patterns can be specified
via season(x, ref = NULL) and harmonic patterns via
harmon(x, order = 1).
season(x, ref = NULL) creates a factor with levels for each cycle of the season. Using
the ref argument, the reference level can be changed from the default
first level to any other. harmon(x, order = 1) creates a matrix of
regressors corresponding to cos(2 * o * pi * time(x)) and
sin(2 * o * pi * time(x)) where o is chosen from 1:order.
See below for examples and M1Germany for a more elaborate application.
Furthermore, a nuisance when working with lm is that it offers only limited
support for time series data, hence a major aim of dynlm is to preserve
time series properties of the data. Explicit support is currently available
for "ts" and "zoo" series. Internally, the data is kept as a "zoo"
series and coerced back to "ts" if the original dependent variable was of
that class (and no internal NAs were created by the na.action).
To specify a set of instruments, formulas of type y ~ x1 + x2 | z1 + z2
can be used where z1 and z2 represent the instruments. Again,
the extended formula processing described above can be employed for all variables
in the model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | ###########################
## Dynamic Linear Models ##
###########################
## multiplicative SARIMA(1,0,0)(1,0,0)_12 model fitted
## to UK seatbelt data
data("UKDriverDeaths", package = "datasets")
uk <- log10(UKDriverDeaths)
dfm <- dynlm(uk ~ L(uk, 1) + L(uk, 12))
dfm
## explicitly set start and end
dfm <- dynlm(uk ~ L(uk, 1) + L(uk, 12), start = c(1975, 1), end = c(1982, 12))
dfm
## remove lag 12
dfm0 <- update(dfm, . ~ . - L(uk, 12))
anova(dfm0, dfm)
## add season term
dfm1 <- dynlm(uk ~ 1, start = c(1975, 1), end = c(1982, 12))
dfm2 <- dynlm(uk ~ season(uk), start = c(1975, 1), end = c(1982, 12))
anova(dfm1, dfm2)
plot(uk)
lines(fitted(dfm0), col = 2)
lines(fitted(dfm2), col = 4)
## regression on multiple lags in a single L() call
dfm3 <- dynlm(uk ~ L(uk, c(1, 11, 12)), start = c(1975, 1), end = c(1982, 12))
anova(dfm, dfm3)
## Examples 7.11/7.12 from Greene (1993)
data("USDistLag", package = "lmtest")
dfm1 <- dynlm(consumption ~ gnp + L(consumption), data = USDistLag)
dfm2 <- dynlm(consumption ~ gnp + L(gnp), data = USDistLag)
plot(USDistLag[, "consumption"])
lines(fitted(dfm1), col = 2)
lines(fitted(dfm2), col = 4)
if(require("lmtest")) encomptest(dfm1, dfm2)
###############################
## Time Series Decomposition ##
###############################
## airline data
data("AirPassengers", package = "datasets")
ap <- log(AirPassengers)
ap_fm <- dynlm(ap ~ trend(ap) + season(ap))
summary(ap_fm)
## Alternative time trend specifications:
## time(ap) 1949 + (0, 1, ..., 143)/12
## trend(ap) (1, 2, ..., 144)/12
## trend(ap, scale = FALSE) (1, 2, ..., 144)
## Exhibit 3.5/3.6 from Cryer & Chan (2008)
if(require("TSA")) {
data("tempdub", package = "TSA")
td_lm <- dynlm(tempdub ~ harmon(tempdub))
summary(td_lm)
plot(tempdub, type = "p")
lines(fitted(td_lm), col = 2)
}
|
Loading required package: zoo
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
Time series regression with "ts" data:
Start = 1970(1), End = 1984(12)
Call:
dynlm(formula = uk ~ L(uk, 1) + L(uk, 12))
Coefficients:
(Intercept) L(uk, 1) L(uk, 12)
0.1826 0.4310 0.5112
Time series regression with "ts" data:
Start = 1975(1), End = 1982(12)
Call:
dynlm(formula = uk ~ L(uk, 1) + L(uk, 12), start = c(1975, 1),
end = c(1982, 12))
Coefficients:
(Intercept) L(uk, 1) L(uk, 12)
0.5418 0.2072 0.6228
Analysis of Variance Table
Model 1: uk ~ L(uk, 1)
Model 2: uk ~ L(uk, 1) + L(uk, 12)
Res.Df RSS Df Sum of Sq F Pr(>F)
1 94 0.24019
2 93 0.13598 1 0.10421 71.272 4.007e-13 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Analysis of Variance Table
Model 1: uk ~ 1
Model 2: uk ~ season(uk)
Res.Df RSS Df Sum of Sq F Pr(>F)
1 95 0.32151
2 84 0.07277 11 0.24874 26.102 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Analysis of Variance Table
Model 1: uk ~ L(uk, 1) + L(uk, 12)
Model 2: uk ~ L(uk, c(1, 11, 12))
Res.Df RSS Df Sum of Sq F Pr(>F)
1 93 0.13598
2 92 0.13253 1 0.0034481 2.3936 0.1253
Loading required package: lmtest
Encompassing test
Model 1: consumption ~ gnp + L(consumption)
Model 2: consumption ~ gnp + L(gnp)
Model E: consumption ~ gnp + L(consumption) + L(gnp)
Res.Df Df F Pr(>F)
M1 vs. ME 15 -1 12.569 0.0029371 **
M2 vs. ME 15 -1 27.093 0.0001067 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Time series regression with "ts" data:
Start = 1949(1), End = 1960(12)
Call:
dynlm(formula = ap ~ trend(ap) + season(ap))
Residuals:
Min 1Q Median 3Q Max
-0.156370 -0.041016 0.003677 0.044069 0.132324
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.726780 0.018894 250.180 < 2e-16 ***
trend(ap) 0.120826 0.001432 84.399 < 2e-16 ***
season(ap)Feb -0.022055 0.024211 -0.911 0.36400
season(ap)Mar 0.108172 0.024212 4.468 1.69e-05 ***
season(ap)Apr 0.076903 0.024213 3.176 0.00186 **
season(ap)May 0.074531 0.024215 3.078 0.00254 **
season(ap)Jun 0.196677 0.024218 8.121 2.98e-13 ***
season(ap)Jul 0.300619 0.024221 12.411 < 2e-16 ***
season(ap)Aug 0.291324 0.024225 12.026 < 2e-16 ***
season(ap)Sep 0.146690 0.024229 6.054 1.39e-08 ***
season(ap)Oct 0.008532 0.024234 0.352 0.72537
season(ap)Nov -0.135186 0.024240 -5.577 1.34e-07 ***
season(ap)Dec -0.021321 0.024246 -0.879 0.38082
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.0593 on 131 degrees of freedom
Multiple R-squared: 0.9835, Adjusted R-squared: 0.982
F-statistic: 649.4 on 12 and 131 DF, p-value: < 2.2e-16
Loading required package: TSA
Attaching package: 'TSA'
The following objects are masked from 'package:stats':
acf, arima
The following object is masked from 'package:utils':
tar
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.