### 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 msmooth function for the trend
results <- msmooth(y, p = 1, mu = 1, bStart = 0.1, alg = "A", method = "lpr")
res <- results$res
ye <- results$ye
# Plot of the results
t <- seq(from = 1947, to = 2019.25, by = 0.25)
matplot(t, cbind(y, ye), type = "ll", lty = c(3, 1), col = c(1, "red"),
xlab = "Years", ylab = "Log-Quartlery US-GDP",
main = "Log-Quarterly US-GDP vs. Trend, Q1 1947 - Q2 2019")
legend("bottomright", legend = c("Original series", "Estimated trend"),
fill = c(1, "red"), cex = 0.7)
results
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 setting 'alg = "A"' is used in combination with p = 3, as
# the resulting estimates appear to be more suitable than for 'alg = "B"'.
est <- msmooth(yt, p = 3, alg = "A")
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