我正在尝试使用stargazer包在latex中输出数据框。我希望列名包含乳胶代码,但是stargazer不允许在数据框名称中使用乳胶代码。我也尝试使用column.labels参数,但此参数仅用于回归表,而不用于输出数据帧。这是我尝试过的两种方法。两者都没有。
代码:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
# Changing names
names(df) = c("$X$", "$Y$\\textsuperscript{1}")
# Exporting
stargazer(df, summary = F,
notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
输出(显然stargazer无法识别LaTeX代码):
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Oct 29, 2016 - 20:46:22
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} ccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \$X\$ & \$Y\$\textbackslash textsuperscript\{1\} \\
\hline \\[-1.8ex]
1 & $1$ & $6$ \\
2 & $2$ & $7$ \\
3 & $3$ & $8$ \\
4 & $4$ & $9$ \\
5 & $5$ & $10$ \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
代码:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
# Exporting
stargazer(df, summary = F,
column.labels = c("$X$", "$Y$\\textsuperscript{1}"),
notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
输出(stargazer只是忽略了参数):
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Oct 29, 2016 - 20:57:41
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} ccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& x & y \\
\hline \\[-1.8ex]
1 & $1$ & $6$ \\
2 & $2$ & $7$ \\
3 & $3$ & $8$ \\
4 & $4$ & $9$ \\
5 & $5$ & $10$ \\
\hline \\[-1.8ex]
\multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \\
\end{tabular}
\end{table}
答案 0 :(得分:2)
有点hacky,但这里的主要想法是gsub
使用我们想要的Latex代码stargazer
输出中的相关行:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
out <- capture.output(
stargazer(df, summary = F,
notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
)
# Changing names
vars = c("x" = "$X$", "y" = "$Y$\\\\textsuperscript{1}")
cat(sep = "\n",
gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
# \begin{table}[!htbp] \centering
# \caption{}
# \label{}
# \begin{tabular}{@{\extracolsep{5pt}} ccc}
# \\[-1.8ex]\hline
# \hline \\[-1.8ex]
# & $X$ & $Y$\textsuperscript{1} \\
# \hline \\[-1.8ex]
# 1 & $1$ & $6$ \\
# 2 & $2$ & $7$ \\
# 3 & $3$ & $8$ \\
# 4 & $4$ & $9$ \\
# 5 & $5$ & $10$ \\
# \hline \\[-1.8ex]
# \multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \\
# \end{tabular}
# \end{table}
使用参数align = T
时,请尝试
vars = sprintf("\\\\multicolumn\\{1\\}\\{c\\}\\{%s\\}",
c("$X$", "$Y$\\\\textsuperscript{1}"))
names(vars) <- sprintf("\\\\multicolumn\\{1\\}\\{c\\}\\{%s\\}", names(df))
cat(sep = "\n",
gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
答案 1 :(得分:0)
您可以执行类似的操作,但是在设置名称之前运行代码,您不想键入无关的名称,
stargazer(GLM.1$finalModel, GLM.2$finalModel, GLM.3$finalModel,
out = "my.GLMs.htm",
title = "Logistic Models", type = "html",
single.row = TRUE, report = "vc*", df = FALSE, header = FALSE, digits=2,
column.labels = c("GLM 1", "GLM 2", "GLM 3"), apply.coef=exp,
p.auto=F, dep.var.caption = "Odds Ratio", intercept.bottom = FALSE,
covariate.labels=c( "Gender", "Marital Status","Age", "Employed", "Education",
"Political Affiliation", "Rural", "Ethnicity", "Region", "Gross Income", "Networth","Networth-HomeEquity", "Liquid Assets"))