Write the code for this in R for a 5 series using Y, I am doing price discovery forgeneralized information share (GIS) and Gonzalo and Granger permanent-temporary decomposition (PT-GG) based information share measures for the spot, futures, and three ETFs.

Y <- log_data[, c("log_Spot", "log_Fut", "log_USO", "log_USL", "log_OIL")]
str(Y)
tibble [3,031 × 5] (S3: tbl_df/tbl/data.frame)
$ log_Spot: num [1:3031] 4.12 4.12 4.12 4.11 4.1 ...
$ log_Fut : num [1:3031] 4.12 4.12 4.12 4.11 4.1 ...
$ log_USO : num [1:3031] 4.64 4.64 4.64 4.63 4.62 ...
$ log_USL : num [1:3031] 3.14 3.15 3.14 3.14 3.13 ...
$ log_OIL : num [1:3031] 5.64 5.64 5.64 5.63 5.62 ...

attr(, "na.action")= 'omit' Named int [1:6847] 1 2 3 4 5 6 7 8 9 10 ...
..- attr(, "names")= chr [1:6847] "1" "2" "3" "4" ...
Write the code for this in R:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

Therefore, the series have the following vector error-correction (VEC) representation (Engle and Granger, 1987):

\begin{equation}
\Delta Y_t = \Pi Y_{t-1} + \sum_{i=1}^{k} A_i \Delta Y_{t-i} + \varepsilon_t, \quad \Pi = \alpha \beta^T
\tag{1}
\end{equation}

where $\beta$ and $\alpha$ are $n \times (n - 1)$ matrices of rank $n - 1$. The columns of $\beta$ consist of the $n - 1$ cointegrating vectors, and each column of $\alpha$ consists of the adjustment coefficients. The matrix $\Pi$ is decomposed in such a way that $\beta^T Y_t$ represents the vector of $n - 1$ stationary series. Let $\Omega$ denote the $n \times n$ covariance matrix of the innovation vector, i.e., $E[\varepsilon_t \varepsilon_t^T] = \Omega$. Following Stock and Watson (1988), equation (1) can be transformed into the following two equivalent vector moving average (VMA) representations (Hasbrouck, 1995):

\begin{equation}
\Delta Y_t = \Psi(L) \varepsilon_t
\tag{2}
\end{equation}

\begin{equation}
Y_t = Y_0 + \Psi(1) \sum_{i=1}^{t} \varepsilon_i + \Psi^*(L) \varepsilon_t
\tag{3}
\end{equation}

Then, the Engle-Granger representation theorem (Engle and Granger, 1987) implies the following (De Jong, 2002 and Lehmann, 2002):

\begin{equation}
\beta^T \Psi(1) = 0 \quad \text{and} \quad \Psi(1) \alpha = 0
\tag{4}
\end{equation}

Based on the above representations, $\Psi(1) \varepsilon_t$ represents the long-run impact of innovations on the unit-root series (Hasbrouck, 1995). Different information share measures considered by Hasbrouck (1995), Lien and Shrestha (2009), and Lien and Shrestha (2014) are based on this term.

\textbf{GENERALIZED INFORMATION SHARE (GIS) MEASURES}

Based on the above framework, we first discuss the GIS measure. Note that, in this study, we have five non-stationary series with four cointegrating vectors. Therefore, the cointegrating vector represented by matrix $\beta$ can be expressed as follows:

[
\beta^T =
\begin{bmatrix}
1 & -\gamma_1 & 0 & 0 & 0 \
1 & 0 & -\gamma_2 & 0 & 0 \
1 & 0 & 0 & -\gamma_3 & 0 \
1 & 0 & 0 & 0 & -\gamma_4
\end{bmatrix}
\tag{5}
]

Let $\psi_j^r$ be the $j$th row of $\Psi(1)$. Then, (n-1) cointegrating relations imply the following:

[
\psi_1^r = \gamma_{j-1} \psi_j^r, \quad j = 2, \ldots, 5 \tag{6}
]

In other words, equation (4) implies that the first row of $\Psi(1)$ is equal to the $\gamma_{j-1}$ times the $j$th row of $\Psi(1)$. Therefore, the long-run impact of innovations on the $i$th series is respectively given by

[
\psi_i^r \varepsilon_t = \psi_j^r \gamma_{i-1} \varepsilon_t, \quad i = 1, \ldots, 5 \tag{7}
]

where $\gamma_0 = 1$. When the innovations are \textit{independent} (i.e., $\Omega$ is diagonal), the variances of long-run impact on the $i$th series is given by:

[
\psi_i^r \Omega \psi_i^{rT} = \gamma_{i-1}^2 \sum_{j=1}^5 \psi_{1j}^2 \Omega_{jj} \tag{8}
]

where $\psi_{ij}$ is the $j$th element of $\psi_i^r$ and $\Omega_{ij}$ is the $(i,j)$th element of $\Omega$.

Let $S_{j,i}$ denote the contribution of the innovation of series $j$ to the total variance of the \textit{long-run impact} of innovation on series $i$. Then, we have the following:

[
S_{j,i} = \frac{\psi_{1j}^2 \Omega_{jj}}{\psi_1^r \Omega \psi_1^{rT}} \tag{9}
]

Note that $S_{j,i}$ is independent of $i$. Therefore, the contribution of the innovation of series $j$ to the total variance of the \textit{long-run impact} of innovation on any series will be the same. It is important to note that $S_{j,i}$ given by equation (9) is valid only when the innovations are independent. However, in general, the innovations are not independent. When the innovations are not independent, we diagonalize the correlation matrix and end up with the GIS measure proposed by Lien and Shrestha (2014). Let $\Lambda$ be a diagonal matrix containing the eigenvalues of the innovation correlation matrix on the diagonal, where the corresponding eigenvectors are given by the columns of matrix $G$. Then, we can calculate the information share of the $j$th series as follows:

[
S_j^G = \frac{(\psi_1^G)^2}{\psi_1^r \Omega \psi_1^{rT}} \tag{10}
]

where $\psi_1^G = \psi_1^r F^M, F^M = [G\Lambda^{-1/2} G^T V^{-1}]^{-1}$ and $\psi_j^G$ is the $j$th element of $\psi_1^G$. The information share measure given by equation (10) is referred to as the generalized information share (GIS) measure. It can be shown that the GIS measure is independent of ordering. Therefore, the GIS method leads to a unique information share, unlike the upper and lower bound for Hasbrouck IS measure.

\vspace{1cm}

\textbf{GONZALO-GRANGER PERMANENT TEMPORARY DECOMPOSITION (PT/GG) INFORMATION SHARE}

Here, we briefly describe the PT/GG method. Gonzalo and Granger (1995) propose a way of decomposing the vector of non-stationary series $Y_t$ into permanent component $f_t$ (non-stationary series) and transitory (stationary) component $\hat{Y}_t$, where the identification of these components is achieved by assuming that (i) the permanent component is a linear function of the original series and that (ii) the transitory component does not Granger cause the permanent component in the long-run.

The permanent component $f_t$ (under the linearity condition) can be written as:

[
f_t = \mu^T Y_t \tag{11}
]

where $\mu$ is an $n \times 1$ (or, 5 $\times$ 1 in this study) permanent component coefficient vector which can be shown to be orthogonal to the adjustment coefficient matrix $\alpha$, i.e., $\mu = \alpha_\perp$.

In our study, since we have five unit-root series, the permanent component, $f_t$ can be represented by:

[
f_t = \mu_1 Y_1 t + \mu_2 Y_2 t + \cdots + \mu_5 Y_5 t \tag{12}
]

If all the elements of $\mu$ are non-negative, then the PT/GG based information share of the $i$th series is given by:

[
PTGG_i = \frac{\mu_i}{\sum_{j=1}^5 \mu_j} \tag{13}
]

In the case of non-negative $\mu$, its elements correspond to the contribution of individual series to the permanent component. Therefore, the above definition of price discovery makes sense. However, there is no guarantee that all the elements of $\mu$ will be non-negative. It is important to note that the negative elements of $\mu$ may not necessarily lead to problems. For example, if some of the unit-root processes are negatively related to the common-stochastic trend, we expect the corresponding elements of $\mu$ to be negative. In such cases, where the negative elements of $\mu$ are valid, we can use the following definition of information share:

[
PTGG_i^+ = \frac{|\mu_i|}{\sum_{j=1}^5 |\mu_j|} \tag{14}
]

However, if some of the elements of $\mu$ are unexpectedly negative, one way to compute the PT/GG based information share is to replace the negative elements with zero and use equation (13). However, this will lead to all negative elements having zero information share regardless of their absolute value. Alternatively, we can add a constant number to each element of $\mu$ to obtain $\mu^*$ so that the series with the most negative element in $\mu$ will have zero information share. This method will result in the following information share measure:

[
PTGG_i^* = \frac{\mu_i^}{\sum_{j=1}^5 \mu_j^}, \quad \mu_i^* = \mu_i + (-\min {\mu_i}) \tag{15}
]

\end{document}