I am new here, but I checked other threads and have not found solution so far. I am trying to make a VAR model in R, it looks like this:
library(vars)
library(lmtest)
# from loaded data I create model:
v1 <- data.frame(PRG, VYS, r, GDP)
lag_order <- VARselect(v1, lag = 2, type = "const")$selection[1]
model <- VAR(v1, p = lag_order, type = "const")
Then I try bgtest
for checking serial autocorrelation:
res <- data.frame(residuals(model))
test_result <- bgtest(res, order = lag_order)
but I get this error message:
Error in x$terms %||% attr(x, "terms") %||% stop("no terms component nor attribute") : no terms component nor attribute
I tried to change data type of obtained residuals but this was not helpful. I also tried to fit
VAR(v1, p = lag_order, type = "const")
directly instad of formula, but that also had no impact
VAR
saves a list of lm
objects in varresult
.
Using some sample data from the package, model$varresult
contains 4 lm
objects the test may then be performed on:
library(vars)
library(lmtest)
model <- VAR(Canada, p = 2, type = "none")
bgtest(model$varresult$e)
#>
#> Breusch-Godfrey test for serial correlation of order up to 1
#>
#> data: model$varresult$e
#> LM test = 9.1337, df = 1, p-value = 0.002509
bgtest(model$varresult$prod)
#>
#> Breusch-Godfrey test for serial correlation of order up to 1
#>
#> data: model$varresult$prod
#> LM test = 0.49315, df = 1, p-value = 0.4825
bgtest(model$varresult$rw)
#>
#> Breusch-Godfrey test for serial correlation of order up to 1
#>
#> data: model$varresult$rw
#> LM test = 0.69359, df = 1, p-value = 0.4049
bgtest(model$varresult$U)
#>
#> Breusch-Godfrey test for serial correlation of order up to 1
#>
#> data: model$varresult$U
#> LM test = 0.25434, df = 1, p-value = 0.614
Created on 2024-04-18 with reprex v2.1.0