3
votes

I am a novice at this. I wanted to create a correlation matrix using corrplot and used the following code:

 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(dataset)

    corrplot(cor(dataset, use="complete.obs"), type="upper", order="hclust", 

    p.mat=p.mat, sig.level=0.1)

It's one I used before, but this time around I got an error message:

Error in corrplot(cor(dataset, use = "complete.obs"), type = "upper",  : 
  unused arguments (type = "upper", order = "hclust", p.mat = p.mat, sig.level = 0.01)

Any idea why that is and how I can fix it?

2
If i substitute mtcars for dataset then it runs as expected and I cannot reproduce the error. ps the answer here shows an approach with less code (but extra package) to get the pvalues. - user20650

2 Answers

2
votes

An unused argument error in R means that you are providing named arguments that do not match to the actual arguments of the function. This is generally caused by misspellings or accidental usage of similarly-named functions from different packages.

It looks like the arguments being flagged in this case are for the corrplot function of the corrplot package. There are other corrplot functions from other packages, e.g. arm. Are you sure you loaded the right package?

0
votes
library(corrplot)

dat = read.csv('yourdata')

M<- cor(t(dat[,-1]), use="complete.obs", method="pearson")
corr <- round(cor(M),1)

corrplot(corr , method = "color",is.corr = FALSE
         ,order = "hclust",hclust.method = c("complete"))