2016-05-24 6 views
8

列のテーブルをからtexregにグループ化したいと考えています。グループ化された行のオプションのみが表示されます。groups)ここでtexreg出力に複数列を追加する

は例です:

texreg(regs, custom.coef.names = c("Intercept", rep("x", 4)), 
     custom.model.names = c("1", "2", "1", "2")) 

LaTeX出力で:

set.seed(01349) 
DF <- data.frame(y = rnorm(100), x1A = rnorm(100), x2A = rnorm(100), 
       x1B = rnorm(100), x2B = rnorm(100)) 
regs <- lapply(paste0("x", 1:2, c("A", "A", "B", "B")), function(x) 
      lm(paste0("y ~ ", x), data = DF)) 

は、ここで私は、プレーンtexregを得ることができる限り近いのです

\begin{table} 
\begin{center} 
\begin{tabular}{l c c c c } 
\hline 
      & 1 & 2 & 1 & 2 \\ 
\hline 
Intercept & $-0.13$ & $-0.13$ & $-0.11$ & $-0.11$ \\ 
      & $(0.12)$ & $(0.12)$ & $(0.12)$ & $(0.12)$ \\ 
x   & $0.02$ & $0.07$ & $0.13$ & $-0.11$ \\ 
      & $(0.13)$ & $(0.12)$ & $(0.12)$ & $(0.13)$ \\ 
\hline 
R$^2$  & 0.00  & 0.00  & 0.01  & 0.01  \\ 
Adj. R$^2$ & -0.01 & -0.01 & 0.00  & -0.00 \\ 
Num. obs. & 100  & 100  & 100  & 100  \\ 
RMSE  & 1.18  & 1.17  & 1.17  & 1.17  \\ 
\hline 
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}} 
\end{tabular} 
\caption{Statistical models} 
\label{table:coefficients} 
\end{center} 
\end{table} 

私は余分を好みます行(%のコメントで強調表示):

\begin{table} 
\begin{center} 
\begin{tabular}{l c c c c } 
\hline 
%*************A HEADER LINE HERE********************* 
& \multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} \\ % 
%**************************************************** 
      & 1 & 2 & 1 & 2 \\ 
\hline 
Intercept & $-0.13$ & $-0.13$ & $-0.11$ & $-0.11$ \\ 
      & $(0.12)$ & $(0.12)$ & $(0.12)$ & $(0.12)$ \\ 
x   & $0.02$ & $0.07$ & $0.13$ & $-0.11$ \\ 
      & $(0.13)$ & $(0.12)$ & $(0.12)$ & $(0.13)$ \\ 
\hline 
R$^2$  & 0.00  & 0.00  & 0.01  & 0.01  \\ 
Adj. R$^2$ & -0.01 & -0.01 & 0.00  & -0.00 \\ 
Num. obs. & 100  & 100  & 100  & 100  \\ 
RMSE  & 1.18  & 1.17  & 1.17  & 1.17  \\ 
\hline 
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}} 
\end{tabular} 
\caption{Statistical models} 
\label{table:coefficients} 
\end{center} 
\end{table} 

私に何か不足していますか、これを行うための組み込みの方法はありませんか?

x <- capture.output(texreg(
    regs, custom.coef.names = c("Intercept", rep("x", 4)), 
    custom.model.names = c("1", "2", "1", "2"))) 

x[6] <- paste0("& \\multicolumn{2}{c}{A} & \\multicolumn{2}{c}{B} \\\\ \n", x[6]) 

cat(x, sep = "\n") 

しかし、それは明らかにビットダクトテープ-Yです:

私の回避策があります。

答えて

0

functionのコードはコンソールに入力するか、texreggithubwebsideから読むことができます。

texreg 
function (l, file = NULL, single.row = FALSE, stars = c(0.001, 
    0.01, 0.05), custom.model.names = NULL,... 

これは4つのモデルのテーブルのtexregの出力である:

\begin{table} 
\begin{center} 
\begin{tabular}{l c c c c } 
\hline 
& Model 1 & Model 2 & Model 3 & Model 4 \\ 

Iはコード線469、テーブルのbegginingに見えた:次に

string <- paste0(string, "\\begin{tabular}{", coldef, 
      "}", linesep) 

Iが

string <- paste0(string, "\\begin{tabular}{", coldef, 
      "}", linesep) 
     ## Additions 
     string <- paste0(string, "\\\\[-1.8ex]\\hline", linesep) 
     string <- paste0(string, "& \\multicolumn{", length(l), "}{c}{\\textit{Dependent variable:}} \\\\", linesep) 
     string <- paste0(string, "\\cline{2-5}", linesep) 
     string <- paste0(string, "\\\\[-1.8ex] & \\multicolumn{", length(l), "}{c}{", dep.var, "} \\\\", linesep) 
0を追加しました

はその後、インスタンスごとに異なる名前の関数を保存:

texreg2 <- function (l, file = NULL, single.row = FALSE, ...) 

、機能がパッケージから内部機能を必要とするので、あなたの環境でのパッケージの名前空間にカスタム関数を添付する必要があります。簡単なピーシー:

environment(texreg2) <- asNamespace('texreg') 

これで新しい機能を呼び出すことができます。私の追加は、stargazerに似た、3行と従属変数の名前を含んでいます。あなたがこの方法を好きではない場合

texreg2(out, dep.var = "Normalize Citation Score") 

\begin{table} 
\begin{center} 
\begin{tabular}{l c c c c } 
\\[-1.8ex]\hline 
& \multicolumn{4}{c}{\textit{Dependent variable:}} \\ 
\cline{2-5} 
\\[-1.8ex] & \multicolumn{4}{c}{Normalize Citation Score} \\ 
\hline 
& Model 1 & Model 2 & Model 3 & Model 4 \\ 

最後に、あなたはこのquestionをチェックアウト、regexで出力を操作することができます。

+1

これは私が「マルチカラム」を追加しているところです。私はそれが関数に書かれているのを気にして、ちょうどそれをケースバイケースで扱うのはあまりにも独特なことだと思います。応答していただきありがとうございます。 – MichaelChirico

関連する問題