Transposing regression table in Latex - r

I have a regression table in latex that is the result of output from applying the stargazer package on my regression results in R. I am wondering how to transpose the table such that the columns switch place with rows. In other words, I want the Lags to be where the variables are and vice versa.
Moreover, I am wondering how to reduce the space between the first and second columns as well as how to add a vertical line between columns. I've tried adding | in between, but sometimes this doesn't cover well and leaves in some space in between the top and bottom hline.
\usepackage{dcolumn}
\begin{table}[!htbp] \centering
\caption{Regression results}
\label{}
\setlength{\tabcolsep}{0pt}
\begin{tabular}{lD{.}{.}{-2} D{.}{.}{-2} D{.}{.}{-2} D{.}{.}{-2} D{.}{.}{-2} D{.}{.}{-2}}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{6}{c}{Characteristics} \\
\cline{2-7}
\\
Lags: & \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{5} & \multicolumn{1}{c}{6} \\
\hline \\[-1.8ex]
Variable 1 & -1.14^{***} & 0.04 & -1.73^{***} & -0.24 & -0.59 & -1.64^{***} \\
Variable 2 & 0.16 & -0.25 & 0.12 & -0.12 & 0.14{**} & 0.04 \\
Variable 3 & 0.46^{*} & -0.61 & 0.17 & -0.98^{***} & -0.32 & -0.09\\
Variable 4 & -0.02 & -0.005 & -0.03^{***} & -0.32 & -0.11 & 0.015 \\
\hline \\[-1.8ex]
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{6}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}

So, a few LaTeX basics:
(1) LaTeX is unsophisticated. In R, you can transpose a table or matrix or whatever with t(); there is no such analogue in LaTeX. (Caveat, maybe it exists in a package or something, but personally I wouldn't trust such an approach.)
You'll have to manually retype the table -- or, alternatively, convince R to massage the table before passing it to LaTeX output. Since you didn't discuss any R code, I'll skip over any discussion about that and focus on the LaTeX part.
(2) The building blocks of the tabular environment are: // breaks to a new line, and & (the alignment indicator) skips to a new tab. So if you have a table like
\begin{tabular}{c c c}
a & b & c \\
d & e & f
\end{tabular}
the only way to get its transpose is just to retype it:
\begin{tabular}{c c}
a & d \\
b & e \\
c & f
\end{tabular}
The number of columns is denoted by the markings immediately after \begin{tabular}; for instance, {c c c} indicates three columns, all centered. In yours, the D tab stands for a column aligned to a decimal point per the dcolumn package, so you'll need to remove two of these when you transpose your table since you'll have four column variables instead of six. After that, it will just come down to a matter of retyping the contents of the matrix.
For your other questions:
I am wondering how to reduce the space between the first and second columns
That's an interesting one that seems not to be contained to the code you posted. When I posted that exact same code into an empty LaTeX document, it didn't have that spacing that yours has; what this means is that the table is being ballooned out by some other code, most likely to justify out to a given page width or something. I can't help since I can't reproduce the artifact, but you might try enclosing the entire code block with \begin{center} and \end{center}, maybe? This might let LaTeX break the rules of how big the box containing this table is supposed to be. That's a total guess, though, and the folks at https://tex.stackexchange.com/ might be able to help more on that issue.
as well as how to add a vertical line between columns. I've tried adding | in between, but sometimes this doesn't cover well and leaves in some space in between the top and bottom hline.
First response: consider not doing vertical lines at all.
Second response: You've got the right idea, but the reason it's leaving breaks is that you don't have enough alignment tabs (&) in certain lines. For instance, putting | between the third and fourth D{}{}{} in your tabular options makes this monstrosity:
So, what went wrong? Referring to the regions I annotated in blue:
A: When you break a line with \\, it only print vertical alignment bars if you put the proper number of column advancement tokens. Fix by replacing the line \hline \\[-1.8ex] immediately before Variable 1 with \hline & & & & & & \\[-1.8ex]. (One & per desired column advancement!)
B: In this case, the use of \multicolumn overrides any other vertical alignment options. Fix by replacing
Lags: & \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{5} & \multicolumn{1}{c}{6} \\
with either
Lags: & \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c|}{3} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{5} & \multicolumn{1}{c}{6} \\
or
Lags: & \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} & \multicolumn{1}{|c}{4} & \multicolumn{1}{c}{5} & \multicolumn{1}{c}{6} \\
and note where the | was in either of the two fixes.
C: Same issue as A. Fix by replacing the \\ immediately after the \cline{2-7} with & & & & & & \\ to advance through columns and render vertical markers.
D: Don't do anything here, obviously.

Related

How can I assign a certain rank to a variable if certain conditions are met in R

I have a variable (RANK) that I am trying to assign certain values to based on certain conditions. There are 13 possible ranks (1 being the most serious type of crime to 10 being the least serious, and 13 which is basically 'missing' and what I am trying to replace based on the crime committed)
I have made a set of rules using str_detect to indicate (True or False) if a certain string of text is included in the observation. I currently have 22 rules, but will be including more as I build out the code.
I have tried both case_when and If_else. However, I know that with case_when, it will take the first condition that matches meaning things need to be in a very particular order, which unfortunately I don't think will work for me. Example code below.
cleaned_data<-dataset_toclean2 %>% mutate(
RANK=case_when(
Murder==FALSE & sexoffense==FALSE & felony==TRUE & commonlaw==FALSE & kidnap==TRUE|FALSE & robbery==TRUE|FALSE & rwdw==TRUE|FALSE & awdw==TRUE|FALSE & abuse==TRUE|FALSE & discharge==TRUE|FALSE|burglary==TRUE & second==FALSE~"5",
secondmurder==TRUE~"3",
Murder==TRUE & FirstDegree==TRUE|FALSE & conspir==TRUE|FALSE & attempt==TRUE|FALSE & access==TRUE|FALSE & aid==TRUE|FALSE & solic==TRUE|FALSE | mansla== TRUE|FALSE| Murder==TRUE~"2",
sexoffense==TRUE~"4",
TRUE ~ "13"
) )
However, when I view the cleaned_data, all of the 2's, 3's, and 4's are correct but the 5's only have a handful that are accurately changed. The rest of the observations that should be 5's remain as 13 (unranked) even though they fit my rules.
any suggestions would be really appreciated as I know it will get pretty unruly as I make more rules for the remaining ranks.

LaTeX - Decimal to Binary conversion scheme

I'm trying to replicate on LaTeX the decimal to binary conversion scheme, which should look like this:
64 | 0
32 | 0
16 | 0
And so on, considering the vertical lines have to be connected, so no vertical space between them. Is there a way to do that? That's what I've got so far:
${56\newline28\newline14\newline7\newline3\newline1\newline0\newline}\vert\right{0\newline0\newline0\newline0\newline0\newline1\newline}$
You could use a tabular:
\documentclass{article}
\begin{document}
\begin{tabular}{c|c}
64 & 0\\
32 & 0\\
16 & 0\\
\end{tabular}
\end{document}
A naive approach.
\begin{center}
\begin{tabular}{c|c|c|c}
Division by 2& Quotient & Remainder & Bit $\#$\\
\hline
11/2 & 5&1&0\\
5/2 &2& 1&1\\
2/2 &1& 0&2\\
1/2&0&1&3
\end{tabular}
\end{center}
For a systematic way see https://tex.stackexchange.com/questions/96399/how-can-i-illustrate-decimal-to-binary-conversion and the answers therein.

R markdown ! Extra alignment tab has been changed to \cr. error occured

In R markdown I want to make the table with 2 rows and 8 columns.
\begin{table}[ht]
\begin{tabular}{c|c|c|c|c|c|c|c}
\hline
variable & N & Mean & Std.Dev & SE Mean & 95% cI & T & P \\
Y & 25 & 92.5805 & ? & 0.4673 & (91.6160, ?) & ? & 0.002 \\
\hline
\end{tabular}
\end{table}
I entered this command and it returns this error message.
! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate
l.101 Y & 25 & 92.5805 &
pandoc.exe: Error producing PDF
error: pandoc document conversion failed with error 43
I guess I entered tabular right and nothing to be calculated.
They' are just texts and numbers.
I tried to search similar questions and compare the examples codes.
But I don't know how to solve it.
Specify your tablar correctly with extra vertical bars | at the beginning and at the end. In addition escape the percent sign with a backslash \%, otherwise it has a meaning as a program code.
\begin{table}[ht]
\begin{tabular}{|c|c|c|c|c|c|c|c|}
\hline
variable & N & Mean & Std.Dev & SE Mean & 95\% cI & T & P \\
Y & 25 & 92.5805 & ? & 0.4673 & (91.6160, ?) & ? & 0.002 \\
\hline
\end{tabular}
\end{table}
Hope this would work for you?
I encountered the same error when omitting a justification argument for one of the aligned columns. In this example, I want to left justify two equations using an array rather than a table:
\begin{array}{l}
\hat{z} =& \frac{\beta_0}{1 - \beta_z} \\
\hat{z}' =& \beta_0+\beta_z\left(\frac{\beta_0}{1-\beta_z}\right).
\end{array}
For which we see the same error:
! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate
Indeed the principle of the issue is the same as for \tabular, however adding vertical bars | will not fix the problem. Only by adding the missing l in \begin{array}{l} (which becomes \begin{array}{ll}) resolved the error.

Problems when adding vertical line to array and converting RMarkdown document to Word

I'm trying to add vertical lines to LaTeX arrays in my RMarkdown document. The preview in RStudio, the conversion to html and pdf work like a charm. There seems to be a problem when I try to convert this document to word:
---
title: "Array"
output: word_document
---
$$
\begin{array}{cc}
1 & 2 \\
3 & 4 \\
\end{array}
$$
$$
\begin{array}{c|c}
1 & 2 \\
3 & 4 \\
\end{array}
$$
The first array prints correctly but the second (where I added the vertical line) doesn't print at all. In the word document created, only the LaTeX code appears. When I compile, here is the error message I receives:
[WARNING] Could not convert TeX math '
\begin{array}{c|c}
1 & 2 \\
3 & 4 \\
\end{array}
', rendering as TeX:
^
unexpected "|"
expecting letter, white space or "}"
I'm currently using Word 2016.
Thanks

LaTeX/knitr/sweave table with references to R objects

I'm using knitr/R-sweave to produce a pdf-document wit LaTeX-code. I want to make a table with some numbers produced by some R calculations in the R-code-chuck above this table. When I usually want to refer to an R object in the LaTeX/knitr-outout I use \Sexpr{}, but it doesn't seem to work within tables.
Here's what I got:
\documentclass{article}
\begin{document}
<<Chunk1>>=
a= "1%"
b= "2%"
c= "3%"
d= "4%"
e= 1
f= 2
g= 3
h= 4
#
\begin{table}[]
\centering
\caption{Simple stratified data splits}
\label{my-label}
\begin{tabular}{lll}
\textbf{Dataset} & \textbf{Observations} & \textbf{Event rate}\\ \hline
Full data & \Sexpr{e} & \Sexpr{a} \\
Training set & \Sexpr{f} & \Sexpr{b} \\
Test set & \Sexpr{g} & \Sexpr{c} \\
Evaluation set & \Sexpr{h} & \Sexpr{d}
\end{tabular}
\end{table}
\end{document}
This gives "Running pdflatex.exe on test.tex...failed"
I get the following from the log
LaTeX Warning: No positions in optional float specifier.
Default added (so using `tbp') on input line 71.
LaTeX Font Info: External font cmex10' loaded for size (Font)
<7> on input line 75. LaTeX Font Info: External fontcmex10'
loaded for size (Font) <5> on input line 75.
[1{C:/Users/Frederik
Hermann/AppData/Local/MiKTeX/2.9/pdftex/config/pdftex.map}
] (test.aux) ) Here is how much of TeX's memory you used: 1244
strings out of 493634 16308 string characters out of 3143709 72572
words of memory out of 3000000 4665 multiletter control sequences out
of 15000+200000 4116 words of font info for 16 fonts, out of 3000000
for 9000 1025 hyphenation exceptions out of 8191
27i,8n,19p,284b,237s stack positions out of
5000i,500n,10000p,200000b,50000s Output written on test.pdf (1 page, 39879 bytes). PDF statistics: 18 PDF objects out of
1000 (max. 8388607) 0 named destinations out of 1000 (max. 500000) 1
words of extra memory for PDF output out of 10000 (max. 10000000)
I'm quite the rookie. Any ideas?
You are inserting things like "1%" or "2%" into the table. The percent symbol is a comment marker in LaTeX, so the rest of the line will be dropped.
You can fix this by using "1\%", "2\%", etc. (so that the percents are escaped). You need the double backslash in R code so that a single backslash makes it through to LaTeX.

Resources