When I ran this it did not work:

I understand that you're observing differences in the cointegrating vectors (beta matrix) between your results and those reported in the paper, particularly that the paper's beta matrix contains zeros in certain positions while yours does not. This discrepancy is likely due to the imposition of identifying restrictions on the cointegrating relationships in the paper.

Key Points to Address:

Identification in Cointegration Analysis
Imposing Restrictions on the beta Matrix
Implementing Restrictions in R using urca Package
Re-estimating the Model with Restrictions
Interpreting the Results
Let's go through each point step by step.

1. Identification in Cointegration Analysis
In the context of cointegration analysis, particularly with the Johansen method, the cointegrating vectors are not uniquely identified without further restrictions because they are determined up to a nonsingular transformation. This means that without imposing additional identifying restrictions, the estimated cointegrating vectors can be any linear combination of the true cointegrating vectors.

Implications:

The estimated beta matrix may not match the one reported in the paper unless the same restrictions are applied.
The presence of zeros in the beta matrix in the paper suggests that the authors imposed exact-identifying restrictions based on economic theory or prior knowledge.
2. Imposing Restrictions on the beta Matrix
To obtain a specific form of the beta matrix with zeros in certain positions, you need to impose linear constraints on the cointegrating vectors during estimation. These constraints are typically based on theoretical considerations, such as:

Exclusion Restrictions: Setting certain coefficients in the cointegrating vectors to zero.
Normalization Constraints: Fixing one coefficient to one to normalize the vector.
Example:

Suppose the authors believe that in one of the cointegrating relationships, the coefficient on log_USO is zero. They would impose a restriction like:

𝛽
USO
(
1
)
=
0
β 
USO
(1)
​
 =0
By imposing such restrictions, the estimated cointegrating vectors will have zeros in the specified positions.

3. Implementing Restrictions in R using urca Package
The urca package provides functionality to estimate cointegrating vectors with linear restrictions using the blrtest() function for testing restrictions and the cajorls() function for estimating the VECM with imposed restrictions.

Steps to Impose Restrictions:

Define the Restriction Matrix (R):

The restriction matrix R specifies which elements of the beta matrix are constrained. Each row represents a linear restriction.

Define the Restriction Vector (r):

The vector r contains the values to which the linear combinations are set (usually zeros for exclusion restrictions).

Use the blrtest() Function:

To test the validity of the imposed restrictions.

Use the cajorls() Function:

To estimate the VECM with the imposed restrictions.

Important Note:

The urca package does not directly allow imposing restrictions during the initial estimation with ca.jo(). However, you can estimate the model first and then impose restrictions using these functions.

4. Re-estimating the Model with Restrictions
Example Implementation:

Suppose we want to impose the following restrictions based on the paper:

In the first cointegrating vector, set the coefficients on log_USO and log_USL to zero.
In the second cointegrating vector, set the coefficient on log_OIL to zero.
And so on, based on the specific zeros in the paper's beta matrix.
Implementing in R:

R
Copy code
# Suppose we have r = 4 cointegrating vectors
r <- 4

# Initial estimation
johansen_test <- ca.jo(Y, type="trace", ecdet="const", K=K, spec="longrun")

# Define the restriction matrix R and vector r
# The dimensions of R are (number of restrictions) x (number of parameters)
# The beta coefficients are stacked column-wise in the order they appear

# Let's create an example where we set certain coefficients to zero
# For demonstration, suppose we have the following restrictions:

# Restriction 1: In the first cointegrating vector, beta[3,1] = 0 (coefficient on log_USO)
# Restriction 2: In the first cointegrating vector, beta[4,1] = 0 (coefficient on log_USL)
# Restriction 3: In the second cointegrating vector, beta[5,2] = 0 (coefficient on log_OIL)

# Number of variables (excluding constant)
n_vars <- ncol(Y)

# Total number of beta coefficients (including constants if any)
n_beta_coeffs <- n_vars * r

# Initialize R matrix with zeros
R <- matrix(0, nrow=3, ncol=n_beta_coeffs)

# Initialize r vector with zeros
r_vec <- rep(0, 3)

# Restriction 1: beta[3,1] = 0
R[1, (1 - 1) * n_vars + 3] <- 1

# Restriction 2: beta[4,1] = 0
R[2, (1 - 1) * n_vars + 4] <- 1

# Restriction 3: beta[5,2] = 0
R[3, (2 - 1) * n_vars + 5] <- 1

# Test the restrictions
blr_test <- blrtest(johansen_test, H=R, r=r_vec)

# Estimate the model with imposed restrictions
vecm_restricted <- cajorls(johansen_test, r = r, beta = blr_test$beta)

# Extract the restricted beta matrix
beta_restricted <- vecm_restricted$beta

# Transpose beta for better readability
print("Restricted Transposed Beta:")
print(t(beta_restricted))
Notes:

The restriction matrix R is constructed carefully to impose the desired zeros.
The indices in R depend on how the beta coefficients are stacked.
The function blrtest() tests the overidentifying restrictions.
The function cajorls() estimates the VECM with the imposed restrictions.
5. Interpreting the Results
After imposing the restrictions and re-estimating the model:

Check the beta_restricted Matrix: It should now have zeros in the positions where you imposed restrictions.
Compare with the Paper's Results: The estimated cointegrating vectors should now be closer to those reported in the paper.
Proceed with the Analysis: Use the restricted beta and corresponding alpha matrices for further calculations.
Important Considerations
Restrictions Must Be Theoretically Justified: Imposing arbitrary restrictions without economic justification can lead to misleading results.
Check the Identifiability: Ensure that the model remains identified after imposing restrictions.
Consistency with the Paper: Carefully read the methodology section of the paper to understand which restrictions were imposed and why.
Putting It All Together
Here's the full code snippet with the implementation:

R
Copy code
# Load necessary packages
library(urca)
library(vars)
library(tseries)
library(MASS)
library(tidyverse)

# Assuming log_data is already loaded in the environment
Y <- log_data[, c("log_Spot", "log_Fut", "log_USO", "log_USL", "log_OIL")]

# Step 2: Determine the optimal lag length using VARselect
VARselect_result <- VARselect(Y, lag.max=15, type="const")
p <- VARselect_result$selection["HQ(n)"]
K <- p - 1  # Lagged differences in Johansen test

# Step 3: Johansen cointegration test with adjusted settings
johansen_test <- ca.jo(Y, type="trace", ecdet="const", K=K, spec="longrun")
r <- 4  # Number of cointegrating vectors

# Define the restriction matrix R and vector r_vec
n_vars <- ncol(Y)
n_beta_coeffs <- n_vars * r
R <- matrix(0, nrow=3, ncol=n_beta_coeffs)
r_vec <- rep(0, 3)

# Imposing the restrictions based on the paper
# Adjust the indices according to your variables and desired restrictions

# Example restrictions:
# Restriction 1: In the first cointegrating vector, beta[3,1] = 0 (log_USO coefficient)
R[1, (1 - 1) * n_vars + 3] <- 1  # beta[3,1]

# Restriction 2: In the first cointegrating vector, beta[4,1] = 0 (log_USL coefficient)
R[2, (1 - 1) * n_vars + 4] <- 1  # beta[4,1]

# Restriction 3: In the second cointegrating vector, beta[5,2] = 0 (log_OIL coefficient)
R[3, (2 - 1) * n_vars + 5] <- 1  # beta[5,2]

# Test the restrictions
blr_test <- blrtest(johansen_test, H=R, r=r_vec)

# Estimate the model with imposed restrictions
vecm_restricted <- cajorls(johansen_test, r = r, beta = blr_test$beta)

# Extract the restricted beta matrix
beta_restricted <- vecm_restricted$beta

# Transpose beta for better readability
print("Restricted Transposed Beta:")
print(t(beta_restricted))

# Proceed with further analysis using the restricted beta and alpha matrices
Final Steps
Proceed with Calculations:

Extract the restricted alpha matrix from vecm_restricted$rlm$coefficients.
Recalculate beta_perp and alpha_perp using the restricted beta and alpha.
Continue with the computation of Psi_1, Omega, F_M, and GIS measures.
Ensure Consistency:

Verify that the imposed restrictions match those used in the paper.
Double-check the indices and positions in the restriction matrix R.
Compare Results:

After re-estimating the model with the restrictions, compare your beta matrix with that in the paper.
The zeros should now appear in the same positions.
Additional Tips
Understanding Variable Ordering:

Ensure that the variables in Y are ordered exactly as in the paper.
The ordering affects the indices in the beta matrix.
Interpreting Output:

The cajorls() function returns a list containing the estimated coefficients, including beta and alpha.
Use vecm_restricted$beta and vecm_restricted$rlm$coefficients to extract beta and alpha.
Handling Constants:

Be cautious if the model includes constants or trends; adjust the dimensions of R accordingly.
The constant term in the cointegrating vectors may be included in beta.
Conclusion
By imposing the appropriate restrictions on the beta matrix, you should be able to replicate the cointegrating vectors with zeros in the positions specified in the paper. This approach aligns your estimation procedure with that of the authors, increasing the likelihood of obtaining comparable results.


> # Load necessary packages
> library(urca)
> library(vars)
> library(tseries)
> library(MASS)
> library(tidyverse)
> 
> # Assuming log_data is already loaded in the environment
> print("Extracting relevant columns from log_data...")
[1] "Extracting relevant columns from log_data..."
> Y <- log_data[, c("log_Spot", "log_Fut", "log_USO", "log_USL", "log_OIL")]
> print("Extracted Data:")
[1] "Extracted Data:"
> print(head(Y))
# A tibble: 6 × 5
  log_Spot log_Fut log_USO log_USL log_OIL
     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1     4.12    4.12    4.64    3.14    5.64
2     4.12    4.12    4.64    3.15    5.64
3     4.12    4.12    4.64    3.14    5.64
4     4.11    4.11    4.63    3.14    5.63
5     4.10    4.10    4.62    3.13    5.62
6     4.10    4.10    4.62    3.13    5.61
> 
> # Step 2: Determine the optimal lag length using VARselect
> print("Determining optimal lag length using VARselect...")
[1] "Determining optimal lag length using VARselect..."
> VARselect_result <- VARselect(Y, lag.max=15, type="const")
> print("VARselect Result:")
[1] "VARselect Result:"
> print(VARselect_result$selection)
AIC(n)  HQ(n)  SC(n) FPE(n) 
    15      9      5     15 
> 
> p <- VARselect_result$selection["HQ(n)"]
> K <- p - 1  # Lagged differences in Johansen test
> print(paste("Optimal lag length (HQ criterion):", p))
[1] "Optimal lag length (HQ criterion): 9"
> print(paste("Lagged differences for Johansen test (K):", K))
[1] "Lagged differences for Johansen test (K): 8"
> 
> # Step 3: Johansen cointegration test with adjusted settings
> print("Performing Johansen cointegration test...")
[1] "Performing Johansen cointegration test..."
> johansen_test <- ca.jo(Y, type="trace", ecdet="const", K=K, spec="longrun")
> print("Johansen Test Summary:")
[1] "Johansen Test Summary:"
> summary(johansen_test)

###################### 
# Johansen-Procedure # 
###################### 

Test type: trace statistic , without linear trend and constant in cointegration 

Eigenvalues (lambda):
[1] 9.194013e-02 6.137328e-02 1.082481e-02 5.234867e-03 7.584923e-04
[6] 9.339895e-17

Values of teststatistic and critical values of test:

           test 10pct  5pct  1pct
r <= 4 |   2.29  7.52  9.24 12.97
r <= 3 |  18.16 17.85 19.96 24.60
r <= 2 |  51.06 32.00 34.91 41.07
r <= 1 | 242.53 49.65 53.12 60.16
r = 0  | 534.08 71.86 76.07 84.45

Eigenvectors, normalised to first column:
(These are the cointegration relations)

             log_Spot.l8 log_Fut.l8 log_USO.l8 log_USL.l8 log_OIL.l8
log_Spot.l8  1.000000000  1.0000000   1.000000  1.0000000   1.000000
log_Fut.l8  -1.003644123 -1.5256307  -3.331999  1.4487665  -5.903652
log_USO.l8  -0.006998315 -0.9992708   8.252360 13.6547508   8.825436
log_USL.l8  -0.002521254  0.7607663   4.716197 -8.9283064  -1.004589
log_OIL.l8   0.008398995  0.6448770  -8.722794 -8.0800039  -5.309850
constant     0.008065544  0.7922468   5.954291  0.1266867  10.436638
             constant
log_Spot.l8  1.000000
log_Fut.l8  -1.197880
log_USO.l8  -2.604882
log_USL.l8   1.305870
log_OIL.l8   1.247534
constant     2.071152

Weights W:
(This is the loading matrix)

            log_Spot.l8  log_Fut.l8   log_USO.l8    log_USL.l8   log_OIL.l8
log_Spot.d -0.061910397 0.004041937 -0.003984373 -0.0027565091 0.0003042509
log_Fut.d   0.338164478 0.016601908 -0.003969795 -0.0024759995 0.0002939173
log_USO.d  -0.022184653 0.037273171 -0.004320462 -0.0016669625 0.0002652621
log_USL.d   0.001740984 0.018450168 -0.005337932 -0.0004077368 0.0002501097
log_OIL.d  -0.060171749 0.039992249 -0.002479446 -0.0012813480 0.0003394962
               constant
log_Spot.d 6.558352e-15
log_Fut.d  2.942492e-14
log_USO.d  1.311205e-14
log_USL.d  1.067847e-14
log_OIL.d  8.429320e-15

> 
> r <- 4  # Number of cointegrating vectors
> print(paste("Number of cointegrating vectors assumed (r):", r))
[1] "Number of cointegrating vectors assumed (r): 4"
> 
> # Define the restriction matrix R and vector r_vec
> n_vars <- ncol(Y)
> n_beta_coeffs <- n_vars * r
> R <- matrix(0, nrow=3, ncol=n_beta_coeffs)
> r_vec <- rep(0, 3)
> 
> # Imposing the restrictions based on the paper
> print("Imposing restrictions based on the paper...")
[1] "Imposing restrictions based on the paper..."
> 
> # Example restrictions:
> # Restriction 1
> R[1, (1 - 1) * n_vars + 3] <- 1  # beta[3,1]
> print("Restriction 1 applied: beta[3,1] = 0")
[1] "Restriction 1 applied: beta[3,1] = 0"
> 
> # Restriction 2
> R[2, (1 - 1) * n_vars + 4] <- 1  # beta[4,1]
> print("Restriction 2 applied: beta[4,1] = 0")
[1] "Restriction 2 applied: beta[4,1] = 0"
> 
> # Restriction 3
> R[3, (2 - 1) * n_vars + 5] <- 1  # beta[5,2]
> print("Restriction 3 applied: beta[5,2] = 0")
[1] "Restriction 3 applied: beta[5,2] = 0"
> 
> # Test the restrictions
> print("Testing the restrictions...")
[1] "Testing the restrictions..."
> blr_test <- blrtest(johansen_test, H=R, r=r_vec)
Error in r >= z@P || r < 1 : 'length = 3' in coercion to 'logical(1)'