 # Plot Unit root test - Ljung-Box statistic over the residuals

Hello,
I want to build a graphic looking like that one:

Therefore I used ARIMA to model the Data. Attached you will find the code I am currently working with. But I don't know how to assign the values to the matrix and how to plot the p-values afterwards. It as well tells me:
"Error in ljung_mat[i, ] <- print(c(i, box\$p.value)) :
Number of elements to be replaced is not a multiple of the replacement length "

Kind regards
Luke

``````n2 = length(train)
ljung_mat <- matrix(0, nrow=(n2), ncol=1)
for(i in 1:n2)
{
box<-(Box.test(finalarima\$residuals, lag=i, type="Ljung-Box"))
ljung_mat[I,] <- print(c(i, box\$p.value))
}
``````

Can you try `ljung_mat[i,] <- box\$p.value` ?

It says:
Error in `[<-`(`*tmp*`, i, , value = box\$p.value) :
Indexing outside the limits

I am not sure what goes wrong on your side.
But generating some test data in variabele `x` this works for me :

``````set.seed(2020)
x = rnorm(50)
n2 = length(x)
ljung_mat <- matrix(0, nrow = (n2), ncol = 1)

for (i in 1:n2)
{
box <-
Box.test(
x,
lag = i,
type = "Ljung-Box")
ljung_mat[i, ] <- box\$p.value
}

plot(ljung_mat)
`````` Created on 2020-07-24 by the reprex package (v0.3.0)

1 Like

Thank you that works perfectly now! This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.