使用以下命令:
---
output: pdf_document
---
```{r, results='asis',message=FALSE,warning=FALSE}
library(knitr)
library(xtable)
url <- 'https://cdn.rawgit.com/fsbmat/StackOverflow/master/'
dt1 <- read.table(paste0(url, 'sim50.txt'), header = TRUE)
attach(dt1)
dt2 <- read.table(paste0(url, 'sim200.txt'), header = TRUE)
attach(dt2)
dt3 <- read.table(paste0(url, 'sim1000.txt'), header = TRUE)
attach(dt3)
gamma0=1.23
gamma1=0.07
gamma2=1.05
#gamma3=0.7
beta0=1.18
beta1=0.23
beta2=0.25
phi1 <- 0.69
rho <- 0.03
truevalue <- c(gamma0,gamma1,gamma2,beta0,beta1,beta2,phi1,rho)
eqm=function(x,theta){
N=length(x)
sqrt(sum(((x-theta)^2))/N)}
m <- dt1
m2 <- dt2
m3 <- dt3
eqmest=c(eqm(x=m[,1],theta=truevalue[1]),
eqm(x=m[,2],theta=truevalue[2]),
eqm(x=m[,3],theta=truevalue[3]),
eqm(x=m[,4],theta=truevalue[4]),
eqm(x=m[,5],theta=truevalue[5]),
eqm(x=m[,6],theta=truevalue[6]),
eqm(x=m[,7],theta=truevalue[7]),
eqm(x=m[,8],theta=truevalue[8]))
eqmest2=c(eqm(x=m2[,1],theta=truevalue[1]),
eqm(x=m2[,2],theta=truevalue[2]),
eqm(x=m2[,3],theta=truevalue[3]),
eqm(x=m2[,4],theta=truevalue[4]),
eqm(x=m2[,5],theta=truevalue[5]),
eqm(x=m2[,6],theta=truevalue[6]),
eqm(x=m2[,7],theta=truevalue[7]),
eqm(x=m2[,8],theta=truevalue[8]))
eqmest3=c(eqm(x=m3[,1],theta=truevalue[1]),
eqm(x=m3[,2],theta=truevalue[2]),
eqm(x=m3[,3],theta=truevalue[3]),
eqm(x=m3[,4],theta=truevalue[4]),
eqm(x=m3[,5],theta=truevalue[5]),
eqm(x=m3[,6],theta=truevalue[6]),
eqm(x=m3[,7],theta=truevalue[7]),
eqm(x=m3[,8],theta=truevalue[8]))
parameter <- c("gamma0 (mu2)","gamma1 (mu2)", "gamma2 (mu2)", "beta0 (mu1)", "beta1 (mu1)",
"beta2 (mu1)","phi1 (sd)","rho (cor)")
dt <- data.frame(Parameters=parameter,VV=truevalue,Mean=cbind(colMeans(dt1)),EQM=eqmest,
Mean=cbind(colMeans(dt2)),EQM=eqmest2,Mean=cbind(colMeans(dt3)),
EQM=eqmest3)
kable(dt, digits = 5, align = c('c','c','c','c','c','c','c','c'))
print(xtable(dt,digits=c(5,5,5,5,5,5,5,5,5), caption = "",align = rep("c",9)),
caption.placement = "top", include.rownames = FALSE, type = "latex")
```
我在 rstudio 中生成下表以在我的 latex 文件中使用:
xtable
是否可以使用Rstudio 软件中的命令生成下面包含希腊字母和多列的表格?
如果没有,是否可以生成更简单的方法:
答案1
在@nebi 的帮助下,可以按要求完成表格。只需在 latex 中手动删除所有“NA”和“clear”即可。
---
output: pdf_document
---
```{r, results='asis',message=FALSE,warning=FALSE}
library(knitr)
library(xtable)
url <- 'https://cdn.rawgit.com/fsbmat/StackOverflow/master/'
dt1 <- read.table(paste0(url, 'sim50.txt'), header = TRUE)
attach(dt1)
dt2 <- read.table(paste0(url, 'sim200.txt'), header = TRUE)
attach(dt2)
dt3 <- read.table(paste0(url, 'sim1000.txt'), header = TRUE)
attach(dt3)
gamma0=1.23
gamma1=0.07
gamma2=1.05
#gamma3=0.7
beta0=1.18
beta1=0.23
beta2=0.25
phi1 <- 0.69
rho <- 0.03
truevalue <- c(gamma0,gamma1,gamma2,beta0,beta1,beta2,phi1,rho)
eqm=function(x,theta){
N=length(x)
sqrt(sum(((x-theta)^2))/N)}
m <- dt1
m2 <- dt2
m3 <- dt3
eqmest=c(eqm(x=m[,1],theta=truevalue[1]),
eqm(x=m[,2],theta=truevalue[2]),
eqm(x=m[,3],theta=truevalue[3]),
eqm(x=m[,4],theta=truevalue[4]),
eqm(x=m[,5],theta=truevalue[5]),
eqm(x=m[,6],theta=truevalue[6]),
eqm(x=m[,7],theta=truevalue[7]),
eqm(x=m[,8],theta=truevalue[8]))
eqmest2=c(eqm(x=m2[,1],theta=truevalue[1]),
eqm(x=m2[,2],theta=truevalue[2]),
eqm(x=m2[,3],theta=truevalue[3]),
eqm(x=m2[,4],theta=truevalue[4]),
eqm(x=m2[,5],theta=truevalue[5]),
eqm(x=m2[,6],theta=truevalue[6]),
eqm(x=m2[,7],theta=truevalue[7]),
eqm(x=m2[,8],theta=truevalue[8]))
eqmest3=c(eqm(x=m3[,1],theta=truevalue[1]),
eqm(x=m3[,2],theta=truevalue[2]),
eqm(x=m3[,3],theta=truevalue[3]),
eqm(x=m3[,4],theta=truevalue[4]),
eqm(x=m3[,5],theta=truevalue[5]),
eqm(x=m3[,6],theta=truevalue[6]),
eqm(x=m3[,7],theta=truevalue[7]),
eqm(x=m3[,8],theta=truevalue[8]))
parameter <- c("$\\gamma_{1}$","$\\gamma_{2}$", "$\\gamma_{3}$", "$\\beta_{1}$", "$\\beta_{2}$","$\\beta_{3}$","$\\phi$","$\\rho$")
dt <- data.frame(Parâmetros=parameter,VV=truevalue,Clear=rep("NA",8),Média=cbind(colMeans(dt1)),EQM=eqmest,Clear=rep("NA",8),Média=cbind(colMeans(dt2)),EQM=eqmest2,Clear=rep("NA",8),Média=cbind(colMeans(dt3)),EQM=eqmest3)
print(xtable(dt,digits=c(5,5,5,5,5,5,5,5,5,5,5,5), caption = " ",align = rep("c",12)),
caption.placement = "top", include.rownames = FALSE,include.colnames = FALSE,
type = "latex",
sanitize.text.function = function(x) {x},add.to.row = list(
pos = list(0),
command = c(
"& &&\\multicolumn{2}{c}{n=50}&&\\multicolumn{2}{c}{n=200}&&\\multicolumn{2}{c}{n=1000} \\\\
\\cline{4-5} \\cline{7-8} \\cline{10-11}
Parâmetros & VV & Clear & Média & EQM& Clear & Média & EQM& Clear & Média & EQM\\\\"
)))
答案2
在函数中print.xtable
,有不同的sanitize
函数可以在表格中添加标记。你可以看看部分4.1 表格标记在里面xtable 画廊晕影 ( vignette("xtableGallery")
)
对于您的代码,您有第一的退出 latex 命令(你的parameter variable
(即$\\gamma 0 (\\mu 2)$
)和第二然后使用参数清理你的 print.xtable 调用sanitize.text.function = function(x) {x}
对于您的示例,为了使其可重现,请不要忘记解释您正在使用的文件并调用包和库。
作为 .Rmd 文件,这给出予以适当修改
---
output: pdf_document
---
```{r, results='asis'}
library(knitr)
library(xtable)
url <- 'https://cdn.rawgit.com/fsbmat/StackOverflow/master/'
dt1 <- read.table(paste0(url, 'sim50.txt'), header = TRUE)
attach(dt1)
dt2 <- read.table(paste0(url, 'sim200.txt'), header = TRUE)
attach(dt2)
dt3 <- read.table(paste0(url, 'sim1000.txt'), header = TRUE)
attach(dt3)
gamma0=1.23
gamma1=0.07
gamma2=1.05
#gamma3=0.7
beta0=1.18
beta1=0.23
beta2=0.25
phi1 <- 0.69
rho <- 0.03
truevalue <- c(gamma0,gamma1,gamma2,beta0,beta1,beta2,phi1,rho)
eqm=function(x,theta){
N=length(x)
sqrt(sum(((x-theta)^2))/N)}
m <- dt1
m2 <- dt2
m3 <- dt3
eqmest=c(eqm(x=m[,1],theta=truevalue[1]),
eqm(x=m[,2],theta=truevalue[2]),
eqm(x=m[,3],theta=truevalue[3]),
eqm(x=m[,4],theta=truevalue[4]),
eqm(x=m[,5],theta=truevalue[5]),
eqm(x=m[,6],theta=truevalue[6]),
eqm(x=m[,7],theta=truevalue[7]),
eqm(x=m[,8],theta=truevalue[8]))
eqmest2=c(eqm(x=m2[,1],theta=truevalue[1]),
eqm(x=m2[,2],theta=truevalue[2]),
eqm(x=m2[,3],theta=truevalue[3]),
eqm(x=m2[,4],theta=truevalue[4]),
eqm(x=m2[,5],theta=truevalue[5]),
eqm(x=m2[,6],theta=truevalue[6]),
eqm(x=m2[,7],theta=truevalue[7]),
eqm(x=m2[,8],theta=truevalue[8]))
eqmest3=c(eqm(x=m3[,1],theta=truevalue[1]),
eqm(x=m3[,2],theta=truevalue[2]),
eqm(x=m3[,3],theta=truevalue[3]),
eqm(x=m3[,4],theta=truevalue[4]),
eqm(x=m3[,5],theta=truevalue[5]),
eqm(x=m3[,6],theta=truevalue[6]),
eqm(x=m3[,7],theta=truevalue[7]),
eqm(x=m3[,8],theta=truevalue[8]))
parameter <- c("$\\gamma 0 (\\mu 2)$","$\\gamma 1 (\\mu 2)$", "$\\gamma 2 (\\mu 2)$", "$\\beta 0 (\\mu 1)$", "$\\beta 1 (\\mu 1)$","$\\beta 2 (\\mu 1)$","$\\phi 1 (sd)$","$\\rho (cor)$")
dt <- data.frame(Parameters=parameter,VV=truevalue,Mean=cbind(colMeans(dt1)),EQM=eqmest,Mean=cbind(colMeans(dt2)),EQM=eqmest2,Mean=cbind(colMeans(dt3)),EQM=eqmest3)
print(xtable(dt,digits=c(5,5,5,5,5,5,5,5,5), caption = "",align = rep("c",9)),
caption.placement = "top", include.rownames = FALSE, type = "latex",
sanitize.text.function = function(x) {x})
```