user237554
user237554

Reputation: 89

plotting chi-square results with corrplot raises matrix error

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.

Upvotes: 1

Views: 784

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26650

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

Created on 2021-10-07 by the reprex package (v2.0.1)

In my opinion, just plotting the residuals makes more sense:

corrplot(M, is.corr = FALSE, cl.pos = "b", cl.length = 5, insig = "blank")

example.png

Upvotes: 1

Related Questions