I would like to know if it is possible to dynamically redefine the number of columns and rows of a table based on the data to display. Namely, I would like to create a table of the results of a VAR estimation, which should automatically have two or three columns, depending on the number of endogenous variables. Is this possible to dynamically add an additional column in the LaTeX table, based on the dimensions of the R matrix object?
I do have the following object, in this case a VAR model with three endogenous variables. This results in a data.table:
dt <- structure(list(Var1 = c(0.769609193394589, 0.0296664895070589,
2.23623485051503e-148, 0.168646879269767, 0.0501044748412352,
0.00076293106259367, 0.0283276398763084, 0.0108774314407999,
0.00920733675873214, 0.11332085808532, 0.0525935034976135, 0.0311888374773895,
0.401036698246847, 0.129930114061118, 0.00202489145457509, 0.00013141936702598,
0.0147083530090182, 0.992870983672657, -0.111042253384097, 0.108534218583886,
0.306256671270779), Var2 = c(0.00219370648887803, 0.007911074537242,
0.781553105965536, 1.08039140256749, 0.0480718334369218, 7.36951952786006e-112,
-0.00618410770110546, 0.00319257929695447, 0.0527422189888414,
-0.000606983838455166, 0.0115572151619192, 0.958114435594411,
0.128307715717084, 0.0464342638236593, 0.00572355751692026, -0.00617589275766937,
0.00646284258412713, 0.339274331468835, -0.0226517891403121,
0.0220961058842707, 0.305293138105623), Var3 = c(0.0138066477213567,
0.0308063385230423, 0.65402650661071, 0.0650397844072665, 0.0998573250370163,
0.514835342240951, 0.618196004490569, 0.0636540133711865, 0.000000000000000000000268512388733353,
0.0291100731911744, 0.0516237558174359, 0.572829608804111, -0.198986432833733,
0.204649695705541, 0.330887875932738, 0.137626149238574, 0.0576631051748573,
0.0169990617660536, 0.0236390793300487, 0.085763924744362, 0.782832545097234
), Type = c("Coefficient", "Standard Error", "p-value", "Coefficient",
"Standard Error", "p-value", "Coefficient", "Standard Error",
"p-value", "Coefficient", "Standard Error", "p-value", "Coefficient",
"Standard Error", "p-value", "Coefficient", "Standard Error",
"p-value", "Coefficient", "Standard Error", "p-value")), .Names = c("Var1",
"Var2", "Var3", "Type"), class = c("data.table", "data.frame"
), row.names = c(NA, -21L))
How can I then define the LaTeX table dynamically that I do get an additional column for each endogenous variable, but depending on the number of endogenous variables. Of course the layout will be adjusted and probably also the p-values will be left out and replaced with asterisks.
I write the dt
into a RDS file and then use the following Rnw file (in connection with a parent Rnw/tex file) to knit a tex file.
<<child-test,results="hide",include=FALSE>>=
setwd("correct working directory")
# Read in the data for the table
my.dt<- readRDS("dt.RDS")
my_inline_hook <- knitr:::.inline.hook.tex
knit_hooks$set(inline=my_inline_hook)
@
\begin{table}[htbp]
\footnotesize
\sisetup{table-parse-only}
\begin{center}
\begin{tabularx}{0.95\textwidth}{X
>{\centering}p{0.005\textwidth}
S[table-format=3.2
, round-mode=places
, round-precision=2
, table-align-text-post = false]
S[table-format=3.2
, round-mode=places
, round-precision=2
, table-align-text-post = false]
S[table-format=3.2
, round-mode=places
, round-precision=2
, table-align-text-post = false]
S[table-format=3.2
, round-mode=places
, round-precision=2
, table-align-text-post = false]
}
\toprule
& & {Mean} & {Median} & {Std. Dev.} \tabularnewline
\cmidrule{1-1} \cmidrule{3-6}
Quarterly Oil Production
& % Empty Column
& \Sexpr{as.character( my.dt[Type == "Coefficient", 1][1])} %
& \Sexpr{as.character( my.dt[Type == "Coefficient", 2][1])} %
& \Sexpr{as.character( my.dt[Type == "Coefficient", 3][1])}%
\tabularnewline
\bottomrule
\end{tabularx}\captionof{table}{Caption of table}\label{t.pvar.output}
\end{center}
\end{table}
A related question is Iteratively producing latex tables in knitr