# Decomposing South African GDP data

This article contains various filters that can be used to decompose South Africa s GDP. The first thing we do is to clear all variables in the current environment. This can be done with the following command.

```rm(list = ls())
graphics.off()
Copy code```

As mentioned earlier, South Africa s GDP data is stored as a time series in gdp, and we execute the following command.

```
gdp <- ts(dat.tmp, start = c(1960, 2), frequency = 4)
Copy code```

To ensure that the results of these calculations and extractions are correct, we check the graphs of the data.

```plot(gdp)
Copy code```

## Linear filter removes linear trend of data

In order to estimate a linear trend, we can use a linear regression model that includes a time trend and a constant. In order to estimate such a model, we use the lm command as follows.

```lin.mod\$fitted.values  # Fitted values  are related to time trends
ts(lin.trend, start = c(1960, 1)) # Create a time series variable for the trend
gdp-linear # Period is the difference between data and linear trend
Copy code```

The fitted value of the regression contains information related to the linear trend. This information needs to be extracted from the model object lin.mod. In the above block, we assign these values to the time series object linear. Then remove the trend from the data to get the period.

Then we can draw this result with the help of the following command, where the trend and period are drawn on different numbers.

```plot.ts(gdp, ylab = "")
lines(linear, col = "red")
legend("topleft", legend = c("data", "trend")
Copy code```

## Hodrick-Prescott  (Hodrick-Prescott, HP) filter detrends  the data

Use the popular HP filtering method to decompose this data. In this case, we set the value of lambda to 1600, which is also a recommendation for quarterly data.

```hp(gdp, freq = 1600)
plot.ts(gdp, ylab = "") # plot time series
plot.ts(hp.decom\$cycle, ylab = "") # Draw cycle diagram
Copy code```

This seems to more accurately reflect our understanding of South Africa s economic performance.

## Use Baxter-King filter to trend data

In order to use the Baxter-King filter. In this case, we need to specify the frequency band of the period, the upper limit is set to 32, and the lower limit is set to 6.

```bk(gdp, pl = 6, pu = 32)
plot.ts(gdp, ylab = "")
plot.ts(cycle, ylab = "")
Copy code```

This seems to once again provide a fairly accurate expression for the cyclicality of South Africa's economic activities. Also note that the cycle representation is much smoother than previously provided, because noise is not included in the cycle.

## Christiano-Fitzgerald filter to trend data

The properties of this filter are very similar to those provided above. In addition, it produces results that are highly similar to the Baxter-King filter.

```
plot.ts(gdp, ylab = "")

plot.ts(cfcycle, ylab = "")
Copy code```

## Use Beveridge-Nelson decomposition method to "de-trend" data

In order to decompose the data into random trends and stationary periods, we can use the Beveridge-Nelson decomposition method. When using this technique, we need to specify the number of lag periods associated with the stationary part. In my example below, I assume there are eight lag periods.

```

plot.ts(gdp, ylab = "")
lines(bn.trend, col = "red")
plot.ts(bn.cycle, ylab = "")
Copy code```

## Different measures of comparison cycle

Then, we can combine all these results on a single graph, considering their similarities and differences. In this example, I created a time series ts.union, but I can also draw a single sequence first, and then use the lines command to draw a continuous graph on it.

``` ts.union(lin.cycle, hp.decom, bp.decom,
cf.decom, bn.cycle)

plot.ts(comb, ylab = "")
Copy code```

# Spectral decomposition

Before we consider the use of spectral technology, it is best to clear all variables in the current environment and close all graphs. The next step is to ensure that you can access the programs in these packages by using the library command.

```library(tsm)
library(TSA)
library(mFilter)
Copy code```

Use spectral techniques for decomposition. We can generate values for three time series variables and then combine them into a single variable.

```2 * cos(2 * pi * t * w) + 3 * sin(2 * pi * t *
w) # 6 cycles of frequency on no.obs point
4 * cos(2 * pi * t * w) + 5 * sin(2 * pi * t *
w) #The observation point with a frequency of 10 cycles
6 * cos(2 * pi * t * w) + 7 * sin(2 * pi * t *
w) # In the absence of observation points, the frequency is 40 cycles
y <- x1 + x2 + x3
Copy code```

In order to observe these variables, we can plot them on a separate axis.

```par(mfrow = c(2, 2), mar = c(2.2, 2.2, 2, 1), cex = 0.8)
plot(x1, type = "l", main = "x1")
plot(x2, type = "l", main = "x2")
plot(x3, type = "l", main = "x3")
plot(y, type = "l", main = "y")
Copy code```

Thereafter, we can use the period diagram to consider each attribute of these time series variables.

```gram (y, main = "y
", col = "red") copy the code```

Of course, we can use a filter to remove some unwanted components from the overall time series variables. To this end, we can apply a Christiano-Fitzgerald filter with relatively narrow upper and lower limits. After that, we used the periodogram applied to the period-related information to investigate whether it successfully eliminated some frequency components.

```cf(y0)
gram(cycle)
Copy code```

This result will show that the filter has eliminated most of the high-frequency components. In order to see how this period relates to the previous data, we plot the periodical information passing through the filter on the components. In addition, we also plot this result on the variables of the synthesis period.

```plot(x1, type = "l", lty = 1)
lines(cycle, lty = 3, lwd = 3)
plot(y, type = "l", lty = 1)
lines(cycle, lty = 3, lwd = 3)
Copy code```

In both cases, it seems to have made a reasonable description of the trend in the process.

## The Spectral Decomposition Method of South Africa's Business Cycle

In order to consider how to use these spectrum decompositions in practice, we can now consider applying these techniques to various characteristics of the South African business cycle.

The next step will be to run all the filters that are applied to different methods of identifying the business cycle in South Africa.

Now, let us apply a cycle diagram to each standard of the business cycle.

The linear filter provides a very poor result, because the trend is clearly dominant (this is not what the cycle should have). This is in contrast to the characteristics of the Hodrick-Prescott filter, whose trend information has been removed. This is also the case with the bandpass filters of Baxter & King and Christiano & Fitzgerald. In both cases, the noise has also been removed. The final result is related to the Beveridge-Nelson decomposition. We noticed that the cycle includes a lot of trends and a lot of noise.

# Wavelet decomposition

To provide an example of wavelet decomposition, we will apply this method to South African inflation data. This will allow the use of another measure of the trend derived in this process, which can be thought of as representing core inflation. Please note that this technique can be applied to single integer data of any order, so we do not need to consider the single integer order of the variable first.

Then, we will use monthly data from the Consumer Price Index, which is included in SARB s quarterly announcements. The data can be traced back to 2002. In order to calculate a year-on-year indicator of inflation, we use the diff and lag commands.

```diff/cpi [-1 * (length
- 11): length] Copy the code```

To ensure that the conversion of all these variables has been performed correctly, we plot the data.

```plot(inf.yoy)
Copy code```

Since we are mainly interested in identifying smooth trends in this case, we will use the Bechs function. Such a function is the Daubechies 4 wavelet, which applies a modified discrete wavelet transform method. In addition, we will also use three mother wavelets to process their respective high-frequency components.

```wt(yoy, "d4")
Copy code```

Then we can plot the results for each independent frequency component as shown below.

```plot.ts(yoy)
for (i in 1:4) plot.ts(d4[[i]]
Copy code```

If we now want to draw a trend (parent wavelet) on the data.

```plot.ts(inf, ylab = "inf")
lines(ren)
Copy code```

Please note that since the respective frequency bands are additive, we can add one of the mother frequency bands to the trend, as shown below.

```inf.tmp <- inf.tren + inf.d4\$w3
inf.tren2 <- ts(inf.tmp, start = c(2003, 1), frequency = 12)

plot.ts(inf.yoy, ylab = "inf")
lines(inf.tren2, col = "red")
Copy code```

# Correlation between the cyclical components of related economic variables

In order to determine whether the characteristics of the cycle are appropriate, we can consider the correlation between some different cyclical methods of macroeconomic aggregates. For example, we can consider that the periodicity of output and production (or employment) should be related in different lag periods. If they are not related, the method may not accurately describe the periodic components of the respective variables.

In the examples used in this article, the code may be a bit difficult to understand, but we encourage you to study it yourself to improve your overall understanding of this coding environment.

The next step is to read in the data and create some matrices for the various periodic components of the data.

```

yd <- dat[5:n.obs,]-dat[1:(n.obs-4),] # store output

yc_li <- matrix(rep(0, n.obs * n.var), ncol = n.var)
yc_hp <- matrix(rep(0, n.obs * n.var), ncol = n.var)
yc_bp <- matrix(rep(0, n.obs * n.var), ncol = n.var)
yc_bn <- matrix(rep(0, n.obs * n.var), ncol = n.var)
Copy code```

Use the methods included above to filter the data.

```for (i in 1:n) {

# Use linear filter to detrend the data
lin.mod <- lm(dat[, i] ~ time(dat[, i]))

# Use HP filter to trend data
yc_hp[, i] <- hp.cycle

#Use bandpass filter to trend data
yc_bp[, i] <- bp.cycle

#Beveridge-Nelson decomposition
yc_bn[, i] <- bn.[, 2]
}
Copy code```

Calculate the correlation between different lead and lag periods.

```

for (i in 1:n) {
for (j in 1:n.var) {
c_li <- leadlag(yc_li[, i], yc_li[, j], maxLeadLag)
c_hp <- leadlag(yc_hp[, i], yc_hp[, j], maxLeadLag)
c_bp
c_bn
c_yd

for (k in 1:5) {

ynamesLong[(cnt + k), 1] <- paste(ynames.tmp)
}
cnt <- cnt + 5
Copy code```

Draw the result.

```# Linear trend

barplot(corrStylizedFact)
box()
Copy code```

```

# hp filter

op <- par(mfrow = c(1, 3))
barplot(corrStyli, ylim = c(-1, 1))
box()
Copy code```

```
# beveridge nelson decomposition
barplot(coracts, ylim = c(-1, 1), col = "red")
box()
Copy code```

Most popular insights