I am doing a correlation matrix for a dataframe of 4000 variable and I would like to remove the variables showing > 0.5 correlation, so I am using this command from the {caret} package.
removeme <- findCorrelation(corrMatrix, cutoff = 0.5, verbose = FALSE)
Error in if (mean(x[i, -i]) > mean(x[-j, j])) { :
missing value where TRUE/FALSE needed
The data I have is highly variable, and I get NA values here and there. To start with, I couldn't find something that can deal with NA values on the help page of this command, so I decided to remove the NA values myself.
Some variables show NA values all the way across the data, and some show few NA values. I am trying to remove the variables that are causing any NA values, so that I would be able to use the above command. Here's a minimal example of what my data looks like
dput(df) <- structure(list(GK = 1:10, HGF = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), HJI = c(2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
HDF = c(5L, 6L, 8L, 9L, 5L, 2L, 4L, 3L, 2L, 1L), KLJG = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), KLJA = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), KDA = c(10L, 11L, 15L, 18L,
11L, 10L, 10L, 15L, 12L, 13L), OIE = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), AFE = c(0L, 0L, 0L, 1L, 0L, 0L, NA,
NA, NA, NA)), .Names = c("GK", "HGF", "HJI", "HDF", "KLJG",
"KLJA", "KDA", "OIE", "AFE"), class = "data.frame", row.names = c(NA,
-10L))
corrMatrix <- cor(df,use="pairwise.complete.obs")
What would be the best idea to get rid of these annoying variables? I have tried Many commands but did not get to an ideal one that would get rid of these variables. Here are one of my trials:
removeme <- corrMatrix[,which(as.numeric(rowSums(is.na(corrMatrix))) > 100)]
The issue with this command that if there was over a 100 faulty variables (giving NA in correlation matrix) the normal variables will be removed, as the columns of the normal variable will have > 100 NA values.
I hope this edit made my question more clear. Cheers.
corrMatrix. If there areNAvalues in the matrix, then you need to decide what you want to put there, e.g. zeroes or an artificial large value, to allowfindCorrelationto complete its task. - Carl Witthoft