I'm trying to visualize the results of chisq.test with the corrplot package and I'm getting an error I don't understand. Hope someone can enlighten me.
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")
the error is:
Error in corrplot(M, cl.pos = "b", p.mat = p.mat, sig.level = 0.005, insig = "blank") :
The matrix is not in [-1, 1]!
It does not seem to prevent the results plotting. I want to understand what it's trying to tell me here, but the documentation is not enlightening on this point. Can someone explain? Thanks.
CodePudding user response:
I don't think "M" is a correlation matrix, so if you set is.corr = FALSE
you get output (although I'm not sure whether the output is correct or not; p.mat is only 4 values, but you have 14 values in M so you get a bunch of warnings):
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