# NOT RUN {
## Simple Time-Series: Airpassengers
D(AirPassengers) # 1st difference, same as fdiff(AirPassengers)
D(AirPassengers,-1) # forward difference
G(AirPassengers) # growth rate, same as fgrowth(AirPassengers)
G(AirPassengers, logdiff = TRUE) # log-difference
D(AirPassengers,1,2) # second difference
G(AirPassengers,1,2) # growth rate of growth rate
D(AirPassengers,12) # seasonal difference (data is monthly)
G(AirPassengers,12) # seasonal growth rate (data is monthly)
D(AirPassengers,-2:2,1:3) # sequence of leaded/lagged and iterated differences
# let's do some visual analysis
plot(AirPassengers) # plot the series - seasonal pattern is evident
plot(stl(AirPassengers, "periodic")) # Seasonal decomposition
plot(D(AirPassengers,c(1,12),1:2)) # plotting ordinary and seasonal first and second differences
plot(G(AirPassengers,c(1,12),1:2)) # same using growth rates
plot(stl(window(G(AirPassengers,12), # Taking seasonal growth rate removes most seasonal variation
1950), "periodic"))
## Time-Series Matrix of 4 EU Stock Market Indicators, recorded 260 days per year
plot(G(EuStockMarkets,c(0,260))) # Plot series and annual growth rates
summary(lm(L260G1.DAX ~., G(EuStockMarkets,260))) # Annual growth rate of DAX regressed on the
# growth rates of the other indicators
## World Development Panel Data
head(fgrowth(num_vars(wlddev), 1, 1, # Computes growth rates of numeric variables
wlddev$country, wlddev$year)) # fgrowth/fdiff require externally inputs...
head(G(wlddev, 1, 1, ~country, ~year)) # Growth of numeric variables, id's attached
head(G(wlddev, 1, 1, ~country)) # Without t: Works because data is ordered
head(G(wlddev, 1, 1, PCGDP + LIFEEX ~ country, ~year)) # Growth of GDP per Capita & Life Expectancy
head(G(wlddev, 0:1, 1, ~ country, ~year, cols = 9:10)) # Same, also retaining original series
head(G(wlddev, 0:1, 1, ~ country, ~year, 9:10, # Dropping id columns
keep.ids = FALSE))
# Dynamic Panel-Data Models:
summary(lm(G(PCGDP,1,1,iso3c,year) ~ # GDP growth regressed on it's lagged level
L(PCGDP,1,iso3c,year) + # and the growth rate of Life Expanctancy
G(LIFEEX,1,1,iso3c,year), data = wlddev))
g = qF(wlddev$country) # Omitting t and precomputing g allows for a
summary(lm(G(PCGDP,1,1,g) ~ L(PCGDP,1,g) + # bit more parsimonious specification
G(LIFEEX,1,1,g), wlddev))
summary(lm(G1.PCGDP ~., # Now adding level and lagged level of
L(G(wlddev,0:1,1, ~ country, ~year,9:10),0:1, # LIFEEX and lagged growth rates
~ country, ~year, keep.ids = FALSE)[-1]))
## Using plm can make things easier, but avoid attaching or 'with' calls:
pwlddev <- plm::pdata.frame(wlddev, index = c("country","year"))
head(G(pwlddev, 0:1, 1, 9:10)) # Again growth rates of LIFEEX and PCGDP
PCGDP <- pwlddev$PCGDP # A panel-Series of GDP per Capita
D(PCGDP) # Differencing the panel series.
summary(lm(G1.PCGDP ~., # Running the dynamic model again ->
data = L(G(pwlddev,0:1,1,9:10),0:1, # code becomes a bit simpler
keep.ids = FALSE)[-1]))
# One could be tempted to also do something like this, but THIS DOES NOT WORK!!!:
# lm drops the attributes (-> with(pwlddev, PCGDP) drops attr. so G.default and L.matrix are used)
summary(lm(G(PCGDP) ~ L(G(PCGDP,0:1)) + L(G(LIFEEX,0:1),0:1), pwlddev))
# To make it work, one needs to create pseries (note: attach(pwlddev) also won't work)
LIFEEX <- pwlddev$LIFEEX
summary(lm(G(PCGDP) ~ L(G(PCGDP,0:1)) + L(G(LIFEEX,0:1),0:1))) # THIS WORKS !!
## Using dplyr:
library(dplyr)
wlddev %>% group_by(country) %>%
select(PCGDP,LIFEEX) %>% D(0:1,1:2) # Adding a first and second difference
wlddev %>% group_by(country) %>%
select(year,PCGDP,LIFEEX) %>% D(0:1,1:2,year) # Also using t (safer)
wlddev %>% group_by(country) %>% # Growth rates, dropping id's
select(year,PCGDP,LIFEEX) %>% G(0:1,1:2,year, keep.ids = FALSE)
# }
Run the code above in your browser using DataLab