Create table Stargazer function mq in R

Asked

Viewed 142 times

1

I have the data below and would like to create a table Stargazer to generate in latex a pdf. How could I do?

x=matrix(rnorm(1500),500,3)
mq(x)

> mq(x)
Ljung-Box Statistics:  
        m       Q(m)     df    p-value
 [1,]   1.0      15.2     9.0     0.08
 [2,]   2.0      42.2    18.0     0.00
 [3,]   3.0      52.9    27.0     0.00
 [4,]   4.0      68.2    36.0     0.00
 [5,]   5.0      80.5    45.0     0.00
 [6,]   6.0      92.7    54.0     0.00
 [7,]   7.0      97.6    63.0     0.00
 [8,]   8.0     104.8    72.0     0.01
 [9,]   9.0     110.0    81.0     0.02
[10,]  10.0     127.5    90.0     0.01
[11,]  11.0     134.5    99.0     0.01
[12,]  12.0     141.7   108.0     0.02
[13,]  13.0     155.9   117.0     0.01
[14,]  14.0     168.6   126.0     0.01
[15,]  15.0     174.6   135.0     0.01
[16,]  16.0     184.8   144.0     0.01
[17,]  17.0     191.9   153.0     0.02
[18,]  18.0     199.3   162.0     0.02
[19,]  19.0     204.6   171.0     0.04
[20,]  20.0     220.3   180.0     0.02
[21,]  21.0     227.1   189.0     0.03
[22,]  22.0     242.4   198.0     0.02
[23,]  23.0     246.3   207.0     0.03
[24,]  24.0     256.1   216.0     0.03 

1 answer

2


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} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.