Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show greek letters as the rownames of a matrix

Tags:

r

knitr

I am wondering how to set the rownames of a matrix including greek letters expressions in R. I use "expression", but it seems not working. Here is my code below.

b.summary = matrix(0, 8, 6)
colnames(b.summary)= c("Min.", "1st Qu.", "Median", "Mean","3rd Qu", "Max." )
rownames(b.summary)= c(expression(paste(tau, "=1", sep="")),expression(paste(sigma^2, "=1", sep="")), expression(paste(tau, "=5",sep="")), expression(paste(sigma^2, "=0.2",sep="")), expression(paste(tau, "=16", sep="")), expression(paste(sigma^2, "=0.0625",sep="")), expression(paste(tau, "1/2.25", sep="")),expression( paste(sigma^2, "=2.25", sep="")) )

When I type b.summary, the rownames shown is below:

paste(tau, "=1", sep = "")

instead of the latex expression.

The reason why I want the greek letters is that I am using knitr to create a dynamic document. I want to show the result of this matrix directly instead of creating a table manually typing all the elements of the matrix using \Sexpr{} expression. The complete code chunk in knitr is

<<coverage.b.summary, eval=TRUE, echo=FALSE>>=
 b.summary = matrix(runif(48), 8, 6)
 colnames(b.summary)= c("Min.", "1st Qu.", "Median", "Mean","3rd Qu", "Max." )
rownames(b.summary)= labels(expression(paste(tau, "=1",     sep="")),expression(paste(sigma^2, "=1", sep="")), expression(paste(tau, "=5",sep="")), expression(paste(sigma^2, "=0.2",sep="")), expression(paste(tau, "=16", sep="")), expression(paste(sigma^2, "=0.0625",sep="")), expression(paste(tau, "=1/2.25", sep="")),expression( paste(sigma^2, "=2.25", sep="")) )
b.summary
@

Thank you advance for your help!

like image 311
Crystal Avatar asked Mar 15 '15 17:03

Crystal


2 Answers

This is the best I can do under the constraint of using a matrix. The rownames cannot be R expression-classed objects. I am building a named vector called 'greeks' and pulling Unicode values from it using the names, and then using argument recycling to label alternating rows with tau and sigma^2. (The inability to use expressions means cannot have sub-scripting in matrix row names.)

greeks=c(alpha='\u03b1', tau='\u03c4', sigma='\u03c3',
                         beta='\u03b2',
                         gamma='\u03b3')

b.summary = matrix(0, 8, 6)
colnames(b.summary)= c("Min.", "1st Qu.", "Median", "Mean","3rd Qu", "Max." )
rownames(b.summary)= paste0(c( greeks['tau'], paste0(greeks['sigma'],"^2") ), 
                               c("=1","=1", "=5", "=0.2",
                                 "=16",  "=0.0625", "=2.25", "=2.25") )

> b.summary
           Min. 1st Qu. Median Mean 3rd Qu Max.
τ=1           0       0      0    0      0    0
σ^2=1         0       0      0    0      0    0
τ=5           0       0      0    0      0    0
σ^2=0.2       0       0      0    0      0    0
τ=16          0       0      0    0      0    0
σ^2=0.0625    0       0      0    0      0    0
τ=2.25        0       0      0    0      0    0
σ^2=2.25      0       0      0    0      0    0
like image 151
IRTFM Avatar answered Oct 24 '22 09:10

IRTFM


Tweaking @42- solution (this should be a comment, but an answer has better code formatting):

greeks = c(alpha='\u03b1', tau='\u03c4', sigma='\u03c3', sigmaSq='\u03c3\u00B2', beta='\u03b2', gamma='\u03b3')
b.summary = matrix(0, 8, 6)
colnames(b.summary) = c("Min.", "1st Qu.", "Median", "Mean","3rd Qu", "Max.")
p1 = c(greeks['tau'], greeks['sigmaSq'])
p2 = c("1","1", "5", "0.2", "16",  "0.0625", "2.25", "2.25")
rownames(b.summary) = paste(p1, p2, sep="=")
b.summary

produces the following

          Min. 1st Qu. Median Mean 3rd Qu Max.
τ=1          0       0      0    0      0    0
σ²=1         0       0      0    0      0    0
τ=5          0       0      0    0      0    0
σ²=0.2       0       0      0    0      0    0
τ=16         0       0      0    0      0    0
σ²=0.0625    0       0      0    0      0    0
τ=2.25       0       0      0    0      0    0
σ²=2.25      0       0      0    0      0    0

In my particular use case I am KnitR'ing a 'kable' of variance inflation factors and s.d. inflation factors for the mtcars dataset:

cars.vif = rbind(
    t(vif(cars.model)[,1]),
    t(sqrt(vif(cars.model))[,1])
)
rownames(cars.vif) = c("\u03c3", "\u03c3\u00B2")
kable(cars.vif)

Rownames with Greek symbol and superscript

KnitR'ing this requires xelatex and font that contains σ i.e. Arial

like image 2
Darren Bishop Avatar answered Oct 24 '22 08:10

Darren Bishop