### Example 1: US-GDP ###
# Logarithm of test data
# -> the logarithm of the data is assumed to follow the additive model
test_data <- gdpUS
y <- log(test_data$GDP)
# Applied tsmooth function for the trend
result <- tsmooth(y, p = 1, mu = 1, Mcf = "NP", InfR = "Opt",
bStart = 0.1, bvc = "Y")
trend1 <- result$ye
# Plot of the results
t <- seq(from = 1947, to = 2019.25, by = 0.25)
plot(t, y, type = "l", xlab = "Year", ylab = "log(US-GDP)", bty = "n",
lwd = 1, lty = 3,
main = "Estimated trend for log-quarterly US-GDP, Q1 1947 - Q2 2019")
points(t, trend1, type = "l", col = "red", lwd = 1)
title(sub = expression(italic("Figure 1")), col.sub = "gray47",
cex.sub = 0.6, adj = 0)
result
if (FALSE) {
### Example 2: German Stock Index ###
# The following procedure can be considered, if (log-)returns are assumed
# to follow a model from the general class of semiparametric GARCH-type
# models (including Semi-GARCH, Semi-Log-GARCH and Semi-APARCH models among
# others) with a slowly changing variance over time due to a deterministic,
# nonparametric scale function.
# Obtain the logarithm of the squared returns
returns <- diff(log(dax$Close)) # (log-)returns
rt <- returns - mean(returns) # demeaned (log-)returns
yt <- log(rt^2) # logarithm of the squared returns
# Apply 'smoots' function to the log-data, because the logarithm of
# the squared returns follows an additive model with a nonparametric trend
# function, if the returns are assumed to follow a semiparametric GARCH-type
# model.
# In this case, the optimal inflation rate is used for p = 3.
est <- tsmooth(yt, p = 3, InfR = "Opt")
m_xt <- est$ye # estimated trend values
# Obtain the standardized returns 'eps' and the scale function 'scale.f'
res <- est$res # the detrended log-data
C <- -log(mean(exp(res))) # an estimate of a constant value needed
# for the retransformation
scale.f <- exp((m_xt - C) / 2) # estimated values of the scale function in
# the returns
eps <- rt / scale.f # the estimated standardized returns
# -> 'eps' can now be analyzed by any suitable GARCH-type model.
# The total volatilities are then the product of the conditional
# volatilities obtained from 'eps' and the scale function 'scale.f'.
}
Run the code above in your browser using DataLab