I am able to make a correlation matrix that is formatted with the significant values and stars indicating how significant but dont know how to make it so it focuses only on VOC.
Output I have:
Greet Connect Shop Membership Solve Recap&Re-Engage Average VOC
Greet "1.0 " "0.4***" "0.3***" "0.1***" "0.2***" "0.0* " "0.5***" "0.1***"
Connect "0.4***" "1.0 " "0.4***" "0.2***" "0.4***" "0.3***" "0.8***" "0.4***"
Shop "0.3***" "0.4***" "1.0 " "0.3***" "0.2***" "0.2***" "0.7***" "0.2***"
Membership "0.1***" "0.2***" "0.3***" "1.0 " "0.2***" "0.2***" "0.5***" "0.2***"
Solve "0.2***" "0.4***" "0.2***" "0.2***" "1.0 " "0.4***" "0.6***" "0.4***"
Recap&Re-Engage "0.0* " "0.3***" "0.2***" "0.2***" "0.4***" "1.0 " "0.5***" "0.2***"
Average "0.5***" "0.8***" "0.7***" "0.5***" "0.6***" "0.5***" "1.0 " "0.4***"
VOC "0.1***" "0.4***" "0.2***" "0.2***" "0.4***" "0.2***" "0.4***" "1.0 "
What I want:
Voc
Greet. 0.1***
Connect. 0.4***
Shop. 0.2***
Membership. 0.2***
Solve 0.4***
Recap 0.2***
Average 0.4***
VOC 1.0
Below is my code currently:
#Selecting Greet, Connect, Shop, Membership, Solve, Recap & Re-Engage, Average, & VOC
QA_Behav_Overall <- select(NA_Work, c(88:94, 97))
#correlation_matrix
correlation_matrix <- function(QA_Behav_Overall,
type = "pearson",
digits = 1,
decimal.mark = ".",
use = "all",
show_significance = TRUE,
replace_diagonal = FALSE,
replacement = ""){
# check arguments
stopifnot({
is.numeric(digits)
digits >= 0
use %in% c("all", "upper", "lower")
is.logical(replace_diagonal)
is.logical(show_significance)
is.character(replacement)
})
# we need the Hmisc package for this
require(Hmisc)
# retain only numeric and boolean columns
isNumericOrBoolean = vapply(QA_Behav_Overall, function(x) is.numeric(x) | is.logical(x), logical(1))
if (sum(!isNumericOrBoolean) > 0) {
cat('Dropping non-numeric/-boolean column(s):', paste(names(isNumericOrBoolean)[!isNumericOrBoolean], collapse = ', '), 'nn')
}
QA_Behav_Overall = QA_Behav_Overall[isNumericOrBoolean]
# transform input data frame to matrix
x <- as.matrix(QA_Behav_Overall)
# run correlation analysis using Hmisc package
correlation_matrix <- Hmisc::rcorr(x, type = )
R <- correlation_matrix$r # Matrix of correlation coeficients
p <- correlation_matrix$P # Matrix of p-value
# transform correlations to specific character format
Rformatted = formatC(R, format = 'f', digits = digits, decimal.mark = decimal.mark)
# if there are any negative numbers, we want to put a space before the positives to align all
if (sum(R < 0) > 0) {
Rformatted = ifelse(R > 0, paste0(' ', Rformatted), Rformatted)
}
# add significance levels if desired
if (show_significance) {
# define notions for significance levels; spacing is important.
stars <- ifelse(is.na(p), " ", ifelse(p < .001, "***", ifelse(p < .01, "** ", ifelse(p < .05, "* ", " "))))
Rformatted = paste0(Rformatted, stars)
}
# build a new matrix that includes the formatted correlations and their significance stars
Rnew <- matrix(Rformatted, ncol = ncol(x))
rownames(Rnew) <- colnames(x)
colnames(Rnew) <- paste(colnames(x), "", sep =" ")
# replace undesired values
if (use == 'upper') {
Rnew[lower.tri(Rnew, diag = replace_diagonal)] <- replacement
} else if (use == 'lower') {
Rnew[upper.tri(Rnew, diag = replace_diagonal)] <- replacement
} else if (replace_diagonal) {
diag(Rnew) <- replacement
}
return(Rnew)
}
#View matrix in console
correlation_matrix(QA_Behav_Overall)
How do I go about focusing only on the VOC values while keeping the asterisk to indicate how significant a value is?