在 LaTeX 表格环境中使用 \Sexpr{}

发布于 2024-10-21 07:26:07 字数 2001 浏览 1 评论 0原文

我正在尝试使用 \Sexpr{} 将 R 对象中的值包含在 LaTeX 表中。我本质上是试图在 R 中复制 lm 对象的摘要输出,因为 xtable 的内置方法 xtable.lmxtable.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2024-10-28 07:26:07

您的 R 代码中不需要包含 Sexpr; R代码可以直接使用表达式。 Sexpr 不是一个 LaTeX 命令,尽管它看起来像一个;它是一个 Sweave 命令,因此无法将其作为 R 代码的输出。

另外,无需完全重新创建

addtorow$command <-paste('\\hline \\multicolumn{5}{l}{Residual standard error:',
                         extradat$v1, '} \\\\ ')

xtable 使用的矩阵,您只需在默认输出的基础上进行构建即可。基于您上面的内容,类似于:

mytab <- xtable(model1, caption="Summary Results", label="tab:model1")
addtorow$pos[[1]] <- dim(mytab)[1]
print(mytab, add.to.row=addtorow, sanitize.text.function=NULL, 
             caption.placement="top")

请参阅 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

addtorow$command <-paste('\\hline \\multicolumn{5}{l}{Residual standard error:',
                         extradat$v1, '} \\\\ ')

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:

mytab <- xtable(model1, caption="Summary Results", label="tab:model1")
addtorow$pos[[1]] <- dim(mytab)[1]
print(mytab, add.to.row=addtorow, sanitize.text.function=NULL, 
             caption.placement="top")

See http://people.su.se/~lundh/reproduce/sweaveintro.pdf for an example which you might be able to use as is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文