2

With the commands:

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

I generate the following table in rstudio to use in my latex file:

enter image description here

It's possible generate the table below, with Greek letters and multicolumn, with the command xtable in Rstudio software?

enter image description here

If not, is it possible to generate this simpler:

enter image description here

2 Answers 2

2

After help of @nebi, it was possible to complete the table as desired. Just delete all "NA" and "clear" manually in latex.

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

enter image description here

1

In the function print.xtable, there is different sanitize function to add markup in the table. You can have a look at the part 4.1 Markup in table in the The xtable Gallery vignette ( vignette("xtableGallery") )

For your code, you have first to escape the latex command (your parameter variable (i.e $\\gamma 0 (\\mu 2)$) and seconde then to sanitze your print.xtable call with the argument sanitize.text.function = function(x) {x}

For your example, to make it reroducible, don't forget to explain the file your are using and to call the packages and libraries.

As a .Rmd file, this give mutatis 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})
```

output

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .