Note that at no time the function mq
returns an object with the values of the Ljung-Box statistics. All results are placed directly at the prompt via the command printCoefmat
:
function (x, lag = 24, adj = 0)
{
if (!is.matrix(x))
x = as.matrix(x)
nr = nrow(x)
nc = ncol(x)
g0 = var(x)
ginv = solve(g0)
qm = 0
QM = NULL
df = 0
for (i in 1:lag) {
x1 = x[(i + 1):nr, ]
x2 = x[1:(nr - i), ]
g = cov(x1, x2)
g = g * (nr - i - 1)/(nr - 1)
h = t(g) %*% ginv %*% g %*% ginv
qm = qm + nr * nr * sum(diag(h))/(nr - i)
df = df + nc * nc
dff = df - adj
mindeg = nc^2 - 1
pv = 1
if (dff > mindeg)
pv = 1 - pchisq(qm, dff)
QM = rbind(QM, c(i, qm, dff, pv))
}
pvs = QM[, 4]
dimnames(QM) = list(names(pvs), c(" m ", " Q(m) ", " df ",
" p-value"))
cat("Ljung-Box Statistics: ", "\n")
printCoefmat(QM, digits = 3)
par(mfcol = c(1, 1))
plot(pvs, ylim = c(0, 1), xlab = "m", ylab = "prob", main = "p-values of Ljung-Box statistics")
abline(h = c(0))
lines(rep(0.05, lag), lty = 2, col = "blue")
}
<bytecode: 0x112be6028>
<environment: namespace:MTS>
However, the function stargazer
requires an object to be able to create the table in Latex. To resolve this impasse, create a new function called mq2
, which will save the function results mq
in a data frame, instead of putting them on the screen:
mq2 <- function (x, lag = 24, adj = 0) {
if (!is.matrix(x))
x = as.matrix(x)
nr = nrow(x)
nc = ncol(x)
g0 = var(x)
ginv = solve(g0)
qm = 0
QM = NULL
df = 0
for (i in 1:lag) {
x1 = x[(i + 1):nr, ]
x2 = x[1:(nr - i), ]
g = cov(x1, x2)
g = g * (nr - i - 1)/(nr - 1)
h = t(g) %*% ginv %*% g %*% ginv
qm = qm + nr * nr * sum(diag(h))/(nr - i)
df = df + nc * nc
dff = df - adj
mindeg = nc^2 - 1
pv = 1
if (dff > mindeg)
pv = 1 - pchisq(qm, dff)
QM = rbind(QM, c(i, qm, dff, pv))
}
pvs = QM[, 4]
dimnames(QM) = list(names(pvs), c(" m ", " Q(m) ", " df ",
" p-value"))
par(mfcol = c(1, 1))
plot(pvs, ylim = c(0, 1), xlab = "m", ylab = "prob", main = "p-values of Ljung-Box statistics")
abline(h = c(0))
lines(rep(0.05, lag), lty = 2, col = "blue")
return(QM)
}
mq2(x)
m Q(m) df p-value
[1,] 1 7.801144 9 0.5543039
[2,] 2 16.397978 18 0.5647938
[3,] 3 25.617756 27 0.5398838
[4,] 4 36.243948 36 0.4572713
[5,] 5 44.171363 45 0.5069547
[6,] 6 50.852621 54 0.5965499
[7,] 7 64.540621 63 0.4225154
[8,] 8 74.630370 72 0.3928075
[9,] 9 80.589179 81 0.4919821
[10,] 10 85.904096 90 0.6026227
[11,] 11 90.423156 99 0.7191530
[12,] 12 102.070242 108 0.6426336
[13,] 13 106.695290 117 0.7423386
[14,] 14 113.245299 126 0.7852150
[15,] 15 122.460070 135 0.7725422
[16,] 16 130.586846 144 0.7813422
[17,] 17 152.182453 153 0.5034627
[18,] 18 159.606555 162 0.5384525
[19,] 19 168.701147 171 0.5353674
[20,] 20 173.829833 180 0.6155221
[21,] 21 178.714875 189 0.6930330
[22,] 22 184.900037 198 0.7388180
[23,] 23 189.633979 207 0.8011105
[24,] 24 196.603486 216 0.8239132
stargazer(resultado)
% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Thu, Jul 18, 2019 - 21:02:45
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} cccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
m & Q(m) & df & p-value \\
\hline \\[-1.8ex]
$1$ & $7.801$ & $9$ & $0.554$ \\
$2$ & $16.398$ & $18$ & $0.565$ \\
$3$ & $25.618$ & $27$ & $0.540$ \\
$4$ & $36.244$ & $36$ & $0.457$ \\
$5$ & $44.171$ & $45$ & $0.507$ \\
$6$ & $50.853$ & $54$ & $0.597$ \\
$7$ & $64.541$ & $63$ & $0.423$ \\
$8$ & $74.630$ & $72$ & $0.393$ \\
$9$ & $80.589$ & $81$ & $0.492$ \\
$10$ & $85.904$ & $90$ & $0.603$ \\
$11$ & $90.423$ & $99$ & $0.719$ \\
$12$ & $102.070$ & $108$ & $0.643$ \\
$13$ & $106.695$ & $117$ & $0.742$ \\
$14$ & $113.245$ & $126$ & $0.785$ \\
$15$ & $122.460$ & $135$ & $0.773$ \\
$16$ & $130.587$ & $144$ & $0.781$ \\
$17$ & $152.182$ & $153$ & $0.503$ \\
$18$ & $159.607$ & $162$ & $0.538$ \\
$19$ & $168.701$ & $171$ & $0.535$ \\
$20$ & $173.830$ & $180$ & $0.616$ \\
$21$ & $178.715$ & $189$ & $0.693$ \\
$22$ & $184.900$ & $198$ & $0.739$ \\
$23$ & $189.634$ & $207$ & $0.801$ \\
$24$ & $196.603$ & $216$ & $0.824$ \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
stargazer(resultado)
% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Thu, Jul 18, 2019 - 21:02:45
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} cccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
m & Q(m) & df & p-value \\
\hline \\[-1.8ex]
$1$ & $7.801$ & $9$ & $0.554$ \\
$2$ & $16.398$ & $18$ & $0.565$ \\
$3$ & $25.618$ & $27$ & $0.540$ \\
$4$ & $36.244$ & $36$ & $0.457$ \\
$5$ & $44.171$ & $45$ & $0.507$ \\
$6$ & $50.853$ & $54$ & $0.597$ \\
$7$ & $64.541$ & $63$ & $0.423$ \\
$8$ & $74.630$ & $72$ & $0.393$ \\
$9$ & $80.589$ & $81$ & $0.492$ \\
$10$ & $85.904$ & $90$ & $0.603$ \\
$11$ & $90.423$ & $99$ & $0.719$ \\
$12$ & $102.070$ & $108$ & $0.643$ \\
$13$ & $106.695$ & $117$ & $0.742$ \\
$14$ & $113.245$ & $126$ & $0.785$ \\
$15$ & $122.460$ & $135$ & $0.773$ \\
$16$ & $130.587$ & $144$ & $0.781$ \\
$17$ & $152.182$ & $153$ & $0.503$ \\
$18$ & $159.607$ & $162$ & $0.538$ \\
$19$ & $168.701$ & $171$ & $0.535$ \\
$20$ & $173.830$ & $180$ & $0.616$ \\
$21$ & $178.715$ & $189$ & $0.693$ \\
$22$ & $184.900$ & $198$ & $0.739$ \\
$23$ & $189.634$ & $207$ & $0.801$ \\
$24$ & $196.603$ & $216$ & $0.824$ \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
Excellent! Thank you @Marcus
– Everton Toledo