0
votes

I'm using the mice package to create multiple imputations. I want to create a correlations matrix (and a matrix of p-values for the correlation coefficients. I use miceadds::micombine.cor to do this. But this gives a dataframe with variables in the first to columns, and then a number of columns to contain r, p, t-values, and the like.

I'm looking for a way to turn this dataframe into a "good old" matrix with the correlation coefficient between x and y in position [x,y], and a matrix with p-values Does anyone have an easy way to do this?

Here's some code to reproduce:

data <- mtcars
mt.mis <- prodNA(mtcars, noNA = 0.1)
imputed     <-mice(iris.mis, m = 5, maxit = 5, method = "pmm")
correlations<- miceadds::micombine.cor(mi.res=iris.mis, variables = c(1:3))

What I'm looking for is something like the output from cor(mtcars). Who can help?

1

1 Answers

1
votes

I ended up writing my own function. Can probably be done much more efficiently, but this is what I made.

cormatrix <- function(r, N){
  x <- 1
  cormatrix <- matrix(nrow = N, ncol = N)  # create empty matrix
  for (i in 1:N) {
  for (j in i:N) {
    if(j>i){
    cormatrix[i,j] <- r[x]
    cormatrix[j,i] <- r[x]
    x <- x + 1
    }
  }
}
diag(cormatrix) <- 1
cormatrix
}

You can call it with the output of micombine.cor and the number of variables in your model as arguments. So for example cormatrix(correlations$r,ncol(df)).