【问题标题】:Correlation matrix with p-values for xtable具有 xtable 的 p 值的相关矩阵
【发布时间】:2012-05-25 22:11:57
【问题描述】:

我想在Sweave 中使用带有p 值的xtable 的相关矩阵。我试过这个

library(ltm)
library(xtable)
mat <- matrix(rnorm(1000), 100, 10, dimnames = list(NULL, LETTERS[1:10]))
rcor.test(mat)
xtable(rcor.test(mat))

它会抛出这个错误:

Error in UseMethod("xtable") : 
  no applicable method for 'xtable' applied to an object of class "rcor.test"

我想知道如何获取 xtable 的 p 值的相关矩阵以在 Sweave 中使用。提前感谢您的帮助。

【问题讨论】:

  • 附带说明 - 我建议查看 knitr。它基本上是 Sweave,但很多更好用。

标签: r sweave correlation


【解决方案1】:

要查看发生了什么,我总是建议保存感兴趣的对象,然后使用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的打印方法

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)

【讨论】:

  • 真棒@Dason。非常感谢你的帮助。非常感谢。
【解决方案2】:

你也可以像这样使用自己的函数:

mat <- matrix(rnorm(1000), nrow = 100, ncol = 10,
              dimnames = list(NULL, LETTERS[1:10]))
cor_mat <- function(x, method = c("pearson", "kendall", "spearman"),
                    alternative = c("two.sided", "less", "greater")) {
    stopifnot(is.matrix(x) || is.data.frame(x))
    stopifnot(ncol(x) > 1L)
    if (is.data.frame(x)) x <- data.matrix(x)
    alternative <- match.arg(alternative)
    method <- match.arg(method)
    n <- ncol(x)
    idx <- combn(n, 2L)
    p.vals <- numeric(ncol(idx))
    for (i in seq_along(p.vals)) {
        p.vals[i] <- cor.test(x = x[, idx[1L, i]], y = x[, idx[2L, i]],
                              method = method, alternative = alternative)$p.value
    }
    res <- matrix(NA, ncol = n, nrow = n)
    res[lower.tri(res)] <- p.vals
    return(res)
}
library(xtable)
xtable(cor_mat(mat))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-22
    • 2017-08-10
    • 1970-01-01
    • 2018-10-31
    • 2016-10-26
    • 2014-08-17
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多