Generar tabla con letras griegas y multicolumnas en rstudio para látex

Generar tabla con letras griegas y multicolumnas en rstudio para látex

Con los comandos:

---
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")
```

Genero la siguiente tabla en rstudio para usar en mi archivo de látex:

ingrese la descripción de la imagen aquí

¿Es posible generar la siguiente tabla, con letras griegas y varias columnas, con el comando xtableen el software Rstudio?

ingrese la descripción de la imagen aquí

Si no, ¿es posible generar esto más simple?

ingrese la descripción de la imagen aquí

Respuesta1

Con la ayuda de @nebi, fue posible completar la tabla como se deseaba. Simplemente elimine todo "NA" y "borre" manualmente en látex.

---
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\\\\"
)))

ingrese la descripción de la imagen aquí

Respuesta2

en la funcionprint.xtable, hay diferentessanitize función diferente para agregar marcas en la tabla. Puedes echarle un vistazo a la pieza.4.1 Marcado en la tablaen elLa galería xtableviñeta ( vignette("xtableGallery"))

Para tu código, tienesprimeropara escapar del comando de látex (tu parameter variable(es decir$\\gamma 0 (\\mu 2)$ ) ysegundoluego para desinfectar su llamada print.xtable con el argumentosanitize.text.function = function(x) {x}

Para su ejemplo, para hacerlo reducible, no olvide explicar el archivo que está utilizando y llamar a los paquetes y bibliotecas.

Como archivo .Rmd, esto le damutatis mutandis

---
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})
```

producción

información relacionada