使用 corrplot 绘制卡方结果会引发矩阵误差
plotting chi-square results with corrplot raises matrix error
我正在尝试使用 corrplot 包可视化 chisq.test 的结果,但出现了一个我不明白的错误。希望有人能赐教。
library(corrplot)
no <- c(330, 470, 75, 400, 1050, 170, 90)
yes <- c(1700, 1000, 250, 2100, 500, 1250, 1200)
df <- data.frame(no, yes)
#read in fn cor.mtest
cor.mtest <- function(mat, ...) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
tmp <- cor.test(mat[, i], mat[, j], ...)
p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
}
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}
p.mat <- cor.mtest(df)
chisq <- chisq.test(df)
M <- as.matrix(chisq$residuals)
corrplot(M, cl.pos = "b", p.mat = p.mat, sig.level = 0.005, insig = "blank")
错误是:
Error in corrplot(M, cl.pos = "b", p.mat = p.mat, sig.level = 0.005, insig = "blank") :
The matrix is not in [-1, 1]!
似乎并不能阻止结果绘制。我想了解它在这里试图告诉我什么,但文档在这一点上没有启发性。有人可以解释吗?谢谢
我不认为“M”是相关矩阵,所以如果你设置is.corr = FALSE
你会得到输出(虽然我不确定输出是否正确;p.mat只有 4 个值,但你在 M 中有 14 个值,所以你会收到一堆警告):
library(corrplot)
#> corrplot 0.90 loaded
no <- c(330, 470, 75, 400, 1050, 170, 90)
yes <- c(1700, 1000, 250, 2100, 500, 1250, 1200)
df <- data.frame(no, yes)
#read in fn cor.mtest
cor.mtest <- function(mat, ...) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
tmp <- cor.test(mat[, i], mat[, j], ...)
p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
}
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}
p.mat <- cor.mtest(df)
chisq <- chisq.test(df)
M <- as.matrix(chisq$residuals)
corrplot(M, is.corr = FALSE, cl.pos = "b", p.mat = p.mat, sig.level = 0.005, insig = "blank")
#> Warning in rownames(p.mat) == rownames(corr): longer object length is not a
#> multiple of shorter object length
#> Warning in corrplot(M, is.corr = FALSE, cl.pos = "b", p.mat = p.mat, sig.level
#> = 0.005, : p.mat and corr may be not paired, their rownames and colnames are not
#> totally same!
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 14, 4
由 reprex package (v2.0.1)
于 2021-10-07 创建
在我看来,仅绘制残差图更有意义:
corrplot(M, is.corr = FALSE, cl.pos = "b", cl.length = 5, insig = "blank")
我正在尝试使用 corrplot 包可视化 chisq.test 的结果,但出现了一个我不明白的错误。希望有人能赐教。
library(corrplot)
no <- c(330, 470, 75, 400, 1050, 170, 90)
yes <- c(1700, 1000, 250, 2100, 500, 1250, 1200)
df <- data.frame(no, yes)
#read in fn cor.mtest
cor.mtest <- function(mat, ...) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
tmp <- cor.test(mat[, i], mat[, j], ...)
p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
}
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}
p.mat <- cor.mtest(df)
chisq <- chisq.test(df)
M <- as.matrix(chisq$residuals)
corrplot(M, cl.pos = "b", p.mat = p.mat, sig.level = 0.005, insig = "blank")
错误是:
Error in corrplot(M, cl.pos = "b", p.mat = p.mat, sig.level = 0.005, insig = "blank") :
The matrix is not in [-1, 1]!
似乎并不能阻止结果绘制。我想了解它在这里试图告诉我什么,但文档在这一点上没有启发性。有人可以解释吗?谢谢
我不认为“M”是相关矩阵,所以如果你设置is.corr = FALSE
你会得到输出(虽然我不确定输出是否正确;p.mat只有 4 个值,但你在 M 中有 14 个值,所以你会收到一堆警告):
library(corrplot)
#> corrplot 0.90 loaded
no <- c(330, 470, 75, 400, 1050, 170, 90)
yes <- c(1700, 1000, 250, 2100, 500, 1250, 1200)
df <- data.frame(no, yes)
#read in fn cor.mtest
cor.mtest <- function(mat, ...) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
tmp <- cor.test(mat[, i], mat[, j], ...)
p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
}
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}
p.mat <- cor.mtest(df)
chisq <- chisq.test(df)
M <- as.matrix(chisq$residuals)
corrplot(M, is.corr = FALSE, cl.pos = "b", p.mat = p.mat, sig.level = 0.005, insig = "blank")
#> Warning in rownames(p.mat) == rownames(corr): longer object length is not a
#> multiple of shorter object length
#> Warning in corrplot(M, is.corr = FALSE, cl.pos = "b", p.mat = p.mat, sig.level
#> = 0.005, : p.mat and corr may be not paired, their rownames and colnames are not
#> totally same!
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 14, 4
由 reprex package (v2.0.1)
于 2021-10-07 创建在我看来,仅绘制残差图更有意义:
corrplot(M, is.corr = FALSE, cl.pos = "b", cl.length = 5, insig = "blank")