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.
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.
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).
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 (typically the rank determined by Johansen test)
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
Example restrictions:
Restriction 1: In the first cointegrating vector, beta[3,1] = 0 (log_USO coefficient)
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
Assistant
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.
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:
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:
Proceed with further analysis using the restricted beta and alpha matrices
Final Steps
Proceed with Calculations:
Extract the restricted alpha matrix from vecm_restrictedrlmcoefficients.
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_restrictedbetaandvecmrestrictedrlm$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.
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)
Error in r >= z@P || r < 1 : 'length = 3' in coercion to 'logical(1)'
The error message you encountered suggests that there's an issue with how you're specifying the number of cointegrating vectors or restrictions in the blrtest() function. Specifically, the error is pointing to a problem in the condition r >= z@P || r < 1, which likely relates to how the number of cointegrating vectors or restrictions is being handled in the code.
Here are some potential solutions to address this issue:
Ensure r is an integer:
The r parameter should be a single integer value representing the number of cointegrating vectors. In the code you provided, you have r <- 4, which seems correct if you believe there are 4 cointegrating vectors based on your analysis.
Check the dimensions of R:
Ensure that the number of rows in R corresponds to the number of restrictions you are imposing. In your case, you have 3 restrictions, so R should have 3 rows.
The number of columns in R should equal the total number of parameters in the beta matrix, which is n_vars * r. Make sure this is set correctly.
Verify r_vec:
r_vec should contain the values for the restrictions you are testing. In most cases where you are imposing exclusion restrictions (i.e., setting some beta coefficients to zero), r_vec should be a vector of zeros with length equal to the number of restrictions (as you've done with r_vec <- rep(0, 3)).
Ensure compatibility with blrtest():
Double-check your use of the blrtest() function to ensure all parameters match its expected format. The H parameter should be the restriction matrix, and r should be an integer number of cointegrating relations.
Be cautious with overidentifying restrictions:
The error might indicate an issue with overidentifying restrictions. Ensure your model is identifiable after imposing the restrictions. If you have 4 cointegrating vectors and are imposing restrictions such that rank reduces below what is necessary for identification, you need to modify your approach.
Here's an adjusted check/example:
Make sure the number of cointegrating vectors and restrictions are specified correctly:
r
# Adjust the number of cointegrating vectors if needed
r <- 3 # Update based on test results if needed
# Test the restrictions
blr_test <- blrtest(johansen_test, H=R, r=r_vec)
# Check if a different version of the function requires different input
By following these steps, you should be able to diagnose the cause of the error. If you continue to face issues, you might consider consulting the function's documentation or seeking additional help to make sure that your inputs align with the expected parameters of the urca package.
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)
Error in r >= z@P || r < 1 : 'length = 3' in coercion to 'logical(1)'
The error message you're encountering suggests that there's an issue with how the function blrtest() is interpreting the variable r. In the function blrtest(), r is expected to be an integer that indicates the number of cointegrating vectors, not a vector of restriction values. The misunderstanding here might be in the naming of the variable r_vec, which you've correctly set as a vector of zeros to match the number of restrictions but inadvertently tried to pass as r to the function.
Here's how you can modify the code to correct these issues:
Clarify the role of r and r_vec:
r should be an integer that reflects the number of cointegrating vectors you've identified, which in your case you've set as 4.
r_vec is the vector of values for the restrictions (in your case, it's still correctly set to a vector of zeros).
Use r correctly in blrtest():
Pass r as the number of cointegrating vectors, not as a vector of restriction values.
Let's adjust the code accordingly:
r
# Number of cointegrating vectors should remain as 4 if this is your finding
r <- 4
# Test the restrictions
blr_test <- blrtest(johansen_test, H=R, r=4) # Here we ensure `r` is the number of cointegrating relationships
Double-check the input to blrtest():
Make sure the restriction matrix R accurately reflects the dimensionality and structure required.
Confirm that the ca.jo() object johansen_test is correctly defined.
Here's how it all fits:
r
# 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 (typically the rank determined by Johansen test)
# 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
# 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=4) # Correctly passing `r` as 4, the number of vectors
By ensuring r is correctly passed as a single integer representing the number of cointegrating vectors, you should be able to address the error you are encountering.
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=4) # Correctly passing r as 4, the number of vectors
Error in blrtest(johansen_test, H = R, r = 4) :
Row number of 'H' is unequal to VAR order.
The error message indicates a mismatch between the number of rows in your restriction matrix ( H ) (which is R in your code) and the VAR order (i.e., the number of endogenous variables in your system). In the context of the blrtest function, H should have as many rows as restrictions you wish to test (which you have) but also needs to be correctly associated with the dimensions of the problem and the rank of the cointegration space.
Here’s a breakdown of how to correct this:
Understand the Role of the Restriction Matrix ( H ):
The matrix ( R ) (your ( H )) is used for imposing restrictions on the cointegrating relations. It should match the dimensions of the cointegration space.
The number of rows of ( R ) represents the number of restrictions you are imposing.
Check the Cointegrating Rank:
The cointegrating rank ( r ) is the number of stable relationships and should be determined by the trace test (Johansen test results). If ( r = 4 ), it indicates you have 4 cointegrating vectors.
Re-examine the Setup of Restrictions:
You are imposing 3 restrictions here. Make sure these are consistent with the structure of the identified cointegration space. Check that the restriction matrix ( H ) has dimensions appropriate for ( r ) and number of variables.
Correct the blrtest() Parameter:
Ensure the restriction matrix ( R ) used in blrtest() is correct in size. The size should be ( ( \text{number of restrictions}, \text{number of variables} \times r ) ).
Given the existing framework, you might want to:
Double-check the dimension and calculation for forming R, ensuring it aligns with ( r ).
Ensure that your VAR order does not imply more restrictions than are feasible. If the test implies mismatches, you might want to fine-tune the reasons for imposing any specific number of restrictions.
Here's an example of how you might adjust the implementation:
r
# Correct restriction matrix instantiation based on understanding the problem
# Check the identified rank r_matches expectations from initial analysis
r_identified <- johansen_test@teststat[1] # A check based on what the result implies
if (r_identified != r) {
cat("Adjusting for identified rank. Check assumptions and your restriction logic.")
# Optionally handle mismatch in expected rank detection
}
# Ensure that dimension formulation of R is correct
R <- matrix(0, nrow=3, ncol=n_vars * r) # Should match what is expected from analysis
# Align with restriction application logic
blr_test <- blrtest(johansen_test, H=R, r=r_identified)
Next Steps:
Verify All Hypotheses for Restrictions:
Ensure economic rationale supports the inclusion of the specific zero constraints.
Validate any initial assumptions using test statistics or expectations from the model.
Re-evaluate R Dimensions if Needed:
Ensure any mismatch error isn’t a result of accidental dimension mismatch.
Iterate Over Assumptions:
Ensure VAR order and restrictions align with the analytic logic hardened in context to the original problem statement (link to an economic interpretation).
With these checks and adjustments, your computation related to restrictions should engage more correctly with the statistical procedures governing the urca package logic.
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 (typically the rank determined by Johansen test)
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
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=4) # Correctly passing r as 4, the number of vectors
Error in blrtest(johansen_test, H = R, r = 4) :
Row number of 'H' is unequal to VAR order.
Check the identified rank r_matches expectations from initial analysis
r_identified <- johansen_test@teststat[1] # A check based on what the result implies
if (r_identified != r) {
cat("Adjusting for identified rank. Check assumptions and your restriction logic.")
Optionally handle mismatch in expected rank detection
}
Adjusting for identified rank. Check assumptions and your restriction logic.
Ensure that dimension formulation of R is correct
R <- matrix(0, nrow=3, ncol=n_vars * r) # Should match what is expected from analysis
Align with restriction application logic
blr_test <- blrtest(johansen_test, H=R, r=r_identified)
Error in blrtest(johansen_test, H = R, r = r_identified) :
Row number of 'H' is unequal to VAR order.
The error you're encountering indicates that the number of rows in the restriction matrix ( H ) (which is R in your code) does not match the VAR order. This suggests a misunderstanding about what the "VAR order" means in this context and how the blrtest() function interprets the restriction matrix.
Understanding blrtest() Requirements
VAR Order Contextualization:
In the context of vector autoregression (VAR), the "order" usually refers to the number of lags used in the regression. However, in cointegration testing using Johansen's method, particularly using the urca package, it often expects the number of restrictions to align with dimensions relevant to the beta matrix or the cointegrating vectors.
Number of Restrictions vs. VAR Order:
The confusion might arise from interpreting VAR order in this context. Here, the function might be expecting the number of restrictions (rows in ( H )) to match the number of r, which is the rank or the number of cointegrating vectors detected. Since you have 4 cointegrating vectors, R could potentially need 4 rows.
Check Number of Restrictions:
Verify the actual restrictions you wish to impose. If your intention is to exclude certain variables in more vectors or differently than anticipated, then ensure you differentiate those.
Suggested Solution
Correct Row Number in ( H ):
Match the rows in ( H ) with either the number of cointegrating relations or test constraints as correctly interpreted by blrtest logic.
Estimate ( r ) Appropriately:
The test requires economic and statistical coherence in regard to the I(1) relationships identified in the system.
Here's a refined approach:
r
# Consider the rank limitation as identified
r_identified <- johanen_test@rank
# Check number of restrictions expected
expected_row_num <- r_identified # Now ensure R and its content align with expectation
# Correct your matrix R dimensions if errors persist
R <- matrix(0, nrow=expected_row_num, ncol=n_vars * r_identified)
# Add your specific restriction logic aligned direct into R again
# Note: Adjust restriction logic to fit the formulation and detected model reality
blr_test <- blrtest(johansen_test, H=R, r=r_identified)
DEBUG Steps:
Assess the Econometrics:
Ensure the imposed restrictions make economic model sense.
The normal procedure involves understanding whether your constraints satisfy identifiability and specification of the cointegration framework.
Verification of Process:
Check dimension assumptions and other imposed adjustments for errors, keeping in tighter coordination with urca documentation.
Iterate Over Different Models:
Temporarily adjust restriction prototyping to evaluate differently, conquering dimensionality or misplacement errors.
By checking these parameters and ensuring that the dimensions are consistent with both the theoretical setup (number of restrictions implied by economic rationale) and the statistical inference (using Johansen's approach), you should see more definite results from blrtest().