何が起こっているかを確認するには、関心のあるオブジェクトを保存してから、を使用してその構造を確認することを常にお勧めしますstr
。
library(ltm)
library(xtable)
mat <- matrix(rnorm(1000), 100, 10, dimnames = list(NULL, LETTERS[1:10]))
out <- rcor.test(mat)
str(out)
印刷されているテーブルは実際にはここに格納されていないようです。それでは、rcor.testのprintメソッドを見てみましょう。
getAnywhere(print.rcor.test)
そのメソッドが実際に出力されるマトリックスを作成しますが、それを返さないことがわかります。したがって、これからxtableを使用できるようにマトリックスを取得するには、コードを盗んでそのマトリックスを作成します。マトリックスを印刷して元のオブジェクトを返す代わりに、作成されたマトリックスを返します。
get.rcor.test.matrix <- function (x, digits = max(3, getOption("digits") - 4), ...)
{
### Modified from print.rcor.test
mat <- x$cor.mat
mat[lower.tri(mat)] <- x$p.values[, 3]
mat[upper.tri(mat)] <- sprintf("%6.3f", as.numeric(mat[upper.tri(mat)]))
mat[lower.tri(mat)] <- sprintf("%6.3f", as.numeric(mat[lower.tri(mat)]))
ind <- mat[lower.tri(mat)] == paste(" 0.", paste(rep(0, digits),
collapse = ""), sep = "")
mat[lower.tri(mat)][ind] <- "<0.001"
ind <- mat[lower.tri(mat)] == paste(" 1.", paste(rep(0, digits),
collapse = ""), sep = "")
mat[lower.tri(mat)][ind] <- ">0.999"
diag(mat) <- " *****"
cat("\n")
## Now for the modifications
return(mat)
## and ignore the rest
#print(noquote(mat))
#cat("\nupper diagonal part contains correlation coefficient estimates",
# "\nlower diagonal part contains corresponding p-values\n\n")
#invisible(x)
}
それでは、マトリックスを取得して、xtableを使用してみましょう。
ourmatrix <- get.rcor.test.matrix(out)
xtable(ourmatrix)