diff --git a/R/match_instruments.R b/R/match_instruments.R index 676b32f..5006dd5 100644 --- a/R/match_instruments.R +++ b/R/match_instruments.R @@ -154,7 +154,7 @@ match_instruments <- function(instruments, conten$clusters[[i]]$cluster_id <- conten$clusters[[i]]$cluster_id + 1 } - # here we will convert the match_matrix to a data frame to avoid users boilerplating this code + # here we will convert the questions matrix to a data frame to avoid users boilerplating this code df <- data.frame(conten$matches[[1]]) for (x in seq_along(conten$matches)) { df[x, ] <- conten$matches[[x]] @@ -163,5 +163,16 @@ match_instruments <- function(instruments, rownames(df) <- lapply(conten$questions, function(x) paste(x$question_no, x$question_text, sep = " ")) conten$matches <- df + # here we will also convert the response options similarity matrix to a data frame + if (length(conten$response_options_similarity) > 0) { + df_response_options <- data.frame(conten$response_options_similarity[[1]]) + for (x in seq_along(conten$response_options_similarity)) { + df_response_options[x, ] <- conten$response_options_similarity[[x]] + } + colnames(df_response_options) <- lapply(conten$questions, function(x) paste(x$question_no, x$question_text, sep = " "))[seq_len(ncol(df_response_options))] # we add the [seq_len()] to account for the case when there are empty response options + rownames(df_response_options) <- lapply(conten$questions, function(x) paste(x$question_no, x$question_text, sep = " "))[seq_len(nrow(df_response_options))] + conten$response_options_similarity <- df_response_options + } + return(conten) } diff --git a/harmony_r_example.Rmd b/harmony_r_example.Rmd index 0a2d299..c347737 100644 --- a/harmony_r_example.Rmd +++ b/harmony_r_example.Rmd @@ -97,7 +97,7 @@ We can view the first few rows of the similarity matrix for the questions. head(match$matches) ``` -We can also plot a heat map of the questions similarity matrix. +We can also plot a heat map of the questions similarity matrix. Please bare in mind however that R will compress the matrix to fit the size of the plotting area, which may reduce the visible resolution for large matrices. ```{r} heatmap(as.matrix(match$matches), @@ -105,13 +105,19 @@ heatmap(as.matrix(match$matches), margins = c(10, 10)) ``` - -The first three entries in the similarity matrix for the response options are: +We can view the first few rows of the similarity matrix for the response options. ```{r} -match$response_options_similarity[[1]][c(1, 2, 3)] +head(match$response_options_similarity) ``` +We can also plot a heat map of the response options similarity matrix. + +```{r} +heatmap(as.matrix(match$response_options_similarity), + Rowv = NA, Colv = NA, main = "Response Options Similarity Matrix", + margins = c(10, 10)) +``` ## Generate a Crosswalk table diff --git a/harmony_r_example.html b/harmony_r_example.html index c43ec72..08fdcb6 100644 --- a/harmony_r_example.html +++ b/harmony_r_example.html @@ -1795,22 +1795,28 @@
We can also plot a heat map of the questions similarity matrix.
+We can also plot a heat map of the questions similarity matrix. +Please bare in mind however that R will compress the matrix to fit the +size of the plotting area, which may reduce the visible resolution for +large matrices.
heatmap(as.matrix(match$matches),
Rowv = NA, Colv = NA, main = "Questions Similarity Matrix",
margins = c(10, 10))
The first three entries in the similarity matrix for the response -options are:
-match$response_options_similarity[[1]][c(1, 2, 3)]
-## [[1]]
-## [1] 1
-##
-## [[2]]
-## [1] 1
-##
-## [[3]]
-## [1] 1
+We can view the first few rows of the similarity matrix for the +response options.
+head(match$response_options_similarity)
+We can also plot a heat map of the response options similarity +matrix.
+heatmap(as.matrix(match$response_options_similarity),
+ Rowv = NA, Colv = NA, main = "Response Options Similarity Matrix",
+ margins = c(10, 10))
+We can generate a crosswalk table to match questions with a cosine @@ -1911,45 +1917,45 @@
## [1] "Cluster 4: I was bothered by things that usually don’t bother me."
-## [1] "Keywords: unfriendly, dificuldade"
+## [1] "Cluster 3: I was bothered by things that usually don’t bother me."
+## [1] "Keywords: unfriendly, talked"
## [1] " I was bothered by things that usually don’t bother me."
-## [1] " I did not feel like eating; my appetite was poor."
-## [1] " I felt that I could not shake off the blues even with help from my family or friends."
## [1] " I had trouble keeping my mind on what I was doing."
## [1] " I talked less than usual."
## [1] " People were unfriendly."
## [1] " Não ser capaz de impedir ou de controlar as preocupações"
+## [1] " Preocupar-se muito com diversas coisas"
+## [1] " Ikke klart å slutte å bekymre deg eller kontrolleren bekymringene dine"
+## [1] "Cluster 2: I did not feel like eating; my appetite was poor."
+## [1] "Keywords: ficar, dificuldade, relaxar, facilmente"
+## [1] " I did not feel like eating; my appetite was poor."
+## [1] " Sentir-se nervoso/a, ansioso/a ou muito tenso/a"
## [1] " Dificuldade para relaxar"
+## [1] " Ficar tão agitado/a que se torna difícil permanecer sentado/a"
## [1] " Ficar facilmente aborrecido/a ou irritado/a"
-## [1] " Ikke klart å slutte å bekymre deg eller kontrolleren bekymringene dine"
-## [1] "Cluster 1: I felt I was just as good as other people."
-## [1] "Keywords: happy, enjoyed, future, hopeful"
+## [1] " Følt deg nervøs, engstelig eller veldig stresset"
+## [1] "Cluster 1: I felt that I could not shake off the blues even with help from my family or friends."
+## [1] "Keywords: happy, enjoyed, life, future"
+## [1] " I felt that I could not shake off the blues even with help from my family or friends."
## [1] " I felt I was just as good as other people."
+## [1] " I felt that everything I did was an effort."
## [1] " I felt hopeful about the future."
## [1] " I was happy."
## [1] " I enjoyed life."
-## [1] "Cluster 2: I felt depressed."
-## [1] "Keywords: depressed, lonely, sad, crying, spells"
+## [1] "Cluster 4: I felt depressed."
+## [1] "Keywords: sad, depressed, crying, spells"
## [1] " I felt depressed."
## [1] " I thought my life had been a failure."
-## [1] " I felt lonely."
## [1] " I had crying spells."
## [1] " I felt sad."
-## [1] " I felt that people dislike me."
-## [1] "Cluster 3: I felt that everything I did was an effort."
-## [1] "Keywords: effort, everything"
-## [1] " I felt that everything I did was an effort."
## [1] "Cluster 5: I felt fearful."
-## [1] "Keywords: fearful, sentir, sleep"
+## [1] "Keywords: fearful, lonely, sleep, restless, dislike"
## [1] " I felt fearful."
## [1] " My sleep was restless."
+## [1] " I felt lonely."
+## [1] " I felt that people dislike me."
## [1] " I could not get “going.”"
-## [1] " Sentir-se nervoso/a, ansioso/a ou muito tenso/a"
-## [1] " Preocupar-se muito com diversas coisas"
-## [1] " Ficar tão agitado/a que se torna difícil permanecer sentado/a"
-## [1] " Sentir medo como se algo horrível fosse acontecer"
-## [1] " Følt deg nervøs, engstelig eller veldig stresset"
+## [1] " Sentir medo como se algo horrível fosse acontecer"