在 LaTeX 表格环境中使用 \Sexpr{}
我正在尝试使用 \Sexpr{}
将 R 对象中的值包含在 LaTeX 表中。我本质上是试图在 R 中复制 lm
对象的摘要输出,因为 xtable 的内置方法 xtable.lm
和 xtable.summary.lm
似乎不包括 Fstats、调整后的 R 平方等(R 控制台中 lm 对象的摘要打印输出底部的所有内容)因此我尝试通过构建一个矩阵来复制 xtable 来完成此任务.summary.lm 输出然后为额外的内容构建相关信息的数据帧,以便我可以使用 \Sexpr{}
引用这些值。我尝试通过使用 add.to.row
附加 \multicolumn{}
命令来合并 LaTeX 表最后一行的所有列,然后只是将我需要的所有信息传递到表格的该单元格中。
问题是我在 \multicolumn{}
表达式中得到 \Sexpr{}
表达式的 “未定义控制序列”
。这两个不兼容吗?如果是这样,我做错了什么,如果没有,有人知道如何做我想做的事吗?
谢谢,
这是我的代码的相关部分:
<<Test, results=tex>>=
model1 <- lm(stndfnl ~ atndrte + frosh + soph)
# Build matrix to replicate xtable.summary.lm output
x <- summary(model1)
colnames <- c("Estimate", "Std. Error", "t value", "Pr(<|t|)")
rownames <- c("(Intercept)", attr(x$terms, "term.labels"))
fpval <- pf(x$fstatistic[1],x$fstatistic[2], x$fstatistic[3], lower.tail=FALSE)
mat1 <- matrix(coef(x), nrow=length(rownames), ncol=length(colnames), dimnames=list(rownames,colnames))
# Make a data frame for extra information to be called by \Sexpr in last row of table
residse <- x$sigma
degf <- x$df[2]
multr2 <- x$r.squared
adjr2 <- x$adj.r.squared
fstat <- x$fstatistic[1]
fstatdf1 <- x$fstatistic[2]
fstatdf2 <- x$fstatistic[3]
extradat <- data.frame(v1 = round(residse,4), v2 =degf, v3=round(multr2,4), v4=round(adjr2,4),v5=round(fstat,3), v6=fstatdf1, v7=fstatdf2, v8=round(fpval,6))
addtorow<- list()
addtorow$pos <-list()
addtorow$pos[[1]] <- dim(mat1)[1]
addtorow$command <-c('\\hline \\multicolumn{5}{l}{Residual standard error:\\Sexpr{extradat$v1}} \\\\ ')
print(xtable(mat1, caption="Summary Results for Regression in Equation \\eqref{model1} ", label="tab:model1"), add.to.row=addtorow, sanitize.text.function=NULL, caption.placement="top")
I am trying to use \Sexpr{}
to include values from my R objects in a LaTeX table. I am essentially trying to replicate the summary output of a lm
object in R because xtable's built in methods xtable.lm
and xtable.summary.lm
don't seem to include the Fstats, adjusted R-squared, etc (all the stuff at the bottom of the summary printout of the lm object in R console) So I tried accomplishing this by building a matrix to replicate the xtable.summary.lm
output then construct a data frame of the relevant info for the extra stuff so I can refer to the values using \Sexpr{}
. I tried doing this by using add.to.row
to append the \multicolumn{}
command in order to merge all columns of the last row of the LaTeX table and then just pass all the information I need into that cell of the table.
The problem is that I get an "Undefined control sequence"
for the \Sexpr{}
expression in the \multicolumn{}
expression. Are these two not compatible? If so, what am I doing wrong and if not does anyone know how to do what I am trying to do?
Thanks,
Here is the relevant part of my code:
<<Test, results=tex>>=
model1 <- lm(stndfnl ~ atndrte + frosh + soph)
# Build matrix to replicate xtable.summary.lm output
x <- summary(model1)
colnames <- c("Estimate", "Std. Error", "t value", "Pr(<|t|)")
rownames <- c("(Intercept)", attr(x$terms, "term.labels"))
fpval <- pf(x$fstatistic[1],x$fstatistic[2], x$fstatistic[3], lower.tail=FALSE)
mat1 <- matrix(coef(x), nrow=length(rownames), ncol=length(colnames), dimnames=list(rownames,colnames))
# Make a data frame for extra information to be called by \Sexpr in last row of table
residse <- x$sigma
degf <- x$df[2]
multr2 <- x$r.squared
adjr2 <- x$adj.r.squared
fstat <- x$fstatistic[1]
fstatdf1 <- x$fstatistic[2]
fstatdf2 <- x$fstatistic[3]
extradat <- data.frame(v1 = round(residse,4), v2 =degf, v3=round(multr2,4), v4=round(adjr2,4),v5=round(fstat,3), v6=fstatdf1, v7=fstatdf2, v8=round(fpval,6))
addtorow<- list()
addtorow$pos <-list()
addtorow$pos[[1]] <- dim(mat1)[1]
addtorow$command <-c('\\hline \\multicolumn{5}{l}{Residual standard error:\\Sexpr{extradat$v1}} \\\\ ')
print(xtable(mat1, caption="Summary Results for Regression in Equation \\eqref{model1} ", label="tab:model1"), add.to.row=addtorow, sanitize.text.function=NULL, caption.placement="top")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 R 代码中不需要包含 Sexpr; R代码可以直接使用表达式。 Sexpr 不是一个 LaTeX 命令,尽管它看起来像一个;它是一个 Sweave 命令,因此无法将其作为 R 代码的输出。
另外,无需完全重新创建
xtable 使用的矩阵,您只需在默认输出的基础上进行构建即可。基于您上面的内容,类似于:
请参阅 http://people.su.se /~lundh/reproduct/sweaveintro.pdf 作为示例,您可以按原样使用。
You don't need to have Sexpr in your R code; the R code can use the expressions directly. Sexpr is not a LaTeX command, even though it looks like one; it's an Sweave command, so it doesn't work to have it as output from R code.
Try
Also, no need to completely recreate the matrix used by
xtable
, you can just build on the default output. Building on what you have above, something like:See http://people.su.se/~lundh/reproduce/sweaveintro.pdf for an example which you might be able to use as is.