# Original link:http://tecdat.cn/?p=23000

# Break down South Africa’s GDP data

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

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

## Load the data

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)`

To ensure that these calculations and extracts are correct, let’s examine the graph of the data.

`plot(gdp)`

## Linear filter _ removes the linear trend of data _

To estimate a linear trend, we can use a linear regression model that includes both time trends and constants. To estimate such a model, we use the lm command, as follows.

`Lin.mod $fitted. Values # TS (Lin.trend, start = C (1960, 1)) # Create a time series variable for the trend. GDP-LINEAR # Periods are the difference between the data and the linear trend`

The fitting value of the regression contains information about the linear trend. This information needs to be extracted from the model object lin.mod, and in the block above, we assign these values to the time series object LINEAR. Then you strip out the trend from the data, and you get the cycle.

We can then plot the result with the following command, where the trend and the period are plotted on different numbers.

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

## _ Hodrick-Prescott _ (HP) _ Filters _ for data de-trending

This data should be decomposed using the popular HP filtering method. In this case, we set the value of lambda to 1600, which is also the recommendation for quarterly data.

`HP (GDP, freq = 1600) plot.ts(GDP, ylab = "") plot.ts(HP. Decom $cycle, ylab = ""`

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

## Use the Baxter-King filter to remove the trend data

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

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

Again, this seems to provide a fairly accurate description of the cyclical nature of economic activity in South Africa. Also note that the representation of the period is much smoother than previously provided because noise is not included in the period.

## Christiano-Fitzgerald filter to trend data

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

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

## The Beveridge-Nelson decomposition was used to “de-trend” the data

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

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

## Compare different measures of cycles

We can then combine all these results on a single graph, taking into account their similarities and differences. In this example, I create a time series ts.union, but I could also draw a single sequence first and then draw contigous graphs on it using the lines command.

```
ts.union(lin.cycle, hp.decom, bp.decom,
cf.decom, bn.cycle)
plot.ts(comb, ylab = "")
```

# Spectral decomposition

Before we consider using spectral techniques, it is a good idea to clear all variables in the current environment and close all graphs. The next step is to make sure that you can access the programs in these packages by using the Library command.

```
library(tsm)
library(TSA)
library(mFilter)
```

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

`2 * cos (2 * PI * t * w \ [1 \]) + 3 * sin (2 * PI * t * w \ [1 \]) # no. Six cycles of frequency of obs point 4 * cos (2 * PI * t * w \ [2 \]) + 5 * sin (2 * PI * t * w\[2\]) # Frequency of 10 cycles 6 * cos(2 * PI * t * w\[3\]) + 7 * sin(2 * PI * t * w\[3\]) # In case of no observation point, The frequency is 40 periods y < -x1 + x2 + x3`

To look at 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", the main = "x1") the plot (x2, type = "l", main = "x2") plot(x3, type = "l", main = "x3") plot(y, type = "l", main = "y")`

Thereafter, we can use periodic graphs to consider each of these time series variables.

`gram(y, main = "y", col = "red")`

Of course, we can use a filter to remove unwanted elements from the overall time series variable. For this we can apply Christiano-Fitzgerald filters with relatively narrow upper and lower limits. Thereafter, we use the periodigram, which is applied to period-related information, to investigate whether it succeeds in removing some of the frequency components.

```
cf(y0)
gram(cycle)
```

This result will show that the filter has eliminated most of the high frequency components. To see how this period relates to the previous data, we plot the periodic information that passes through the filter on the component. In addition, we plot this result on variables for 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)
```

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

## Spectral decomposition of the business cycle in South Africa

To consider how these spectrum decompositions might be used in practice, we can now consider applying these techniques to various features of the South African business cycle.

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

Now, let’s apply a cycle diagram for each criterion of the business cycle.

The linear filter provides a poor result because the trend is clearly dominant (which is not what the period should be). This is in contrast to the characteristics of the Hodrick-Prescott filter, where the trend information has been removed. The same is true of band-pass filters from Baxter & King and Christiano & Fitzgerald. In both cases, the noise has also been removed. The final result is related to the Beveridge-Nelson decomposition, and we note that the period includes a large number of trends and a large number of noises.

# Wavelet decomposition

To provide an example of wavelet decomposition, we will apply the method to South African inflation data. This will allow for the use of another measure of trends derived in the process, which can be thought of as representing core inflation. 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.

We will then use the monthly data on the Consumer Price Index, which is included in the SARB’s quarterly announcement. The data goes back to 2002. To calculate year-on-year indicators of inflation, we use the DIFF and LAG commands.

`diff/cpi\[-1 * (length - 11):length\]`

To ensure that all of these variables have been converted correctly, we plot the data.

`plot(inf.yoy)`

Since we are primarily interested in identifying smoothing trends in this case, we will use the Bezis function. Such a function is the Daubechies 4 wavelet, which uses a modified discrete wavelet transform method. In addition, we will use three female wavelets to process each of the high frequency components.

`wt(yoy, "d4")`

We can then plot the results for each individual frequency component, as shown below.

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

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

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

Note that since the respective bands are additive, we can add one of the parent 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")
```

# The correlation between the cyclical components of related economic variables

To determine whether the characteristics of the cycle are appropriate, we can consider the correlations between some of the different cyclical approaches to macroeconomic aggregates. For example, we can consider that the cycles of output and production (or employment) should be correlated during different lags. If they are not correlated, then the method may not accurately describe the periodic component of the respective variables.

In the examples used in this article, the code may be a little hard to understand, but we encourage you to do your own research to improve your overall understanding of the 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.o bs, \] - dat \ [: (n.o 1 bs - 4) \] # storage output yc_li < - matrix (rep (0, n.o bs * n.v ar), 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)`

Filter the data using the methods included above.

`Lin. mod < -lm (dat\[, I \] ~ time(dat\[, I \]))) # yc_HP \[, I \] < - HP. # cycle using band-pass filter to trend data yc_bp \ [I \] < - bp. Cycle # Beveridge - Nelson decomposition yc_bn \ [I \] < - bn. \ [2 \]}`

Calculate the correlation between different lead time and lag time.

```
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
```

Draw the result.

`Barplot (CorrstylizedFact) box()`

`# HP filter op < -par (mfrow = c(1, 3)) barplot(corrStyli, ylim = c(-1, 1)) box()`

`Barplot (coracts, ylim = c(-1, 1), col = "red") box()`