Title: | Tests of Matrix Structure for Construct Validation |
---|---|
Description: | Tests for block-diagonal structure in symmetric matrices (e.g. correlation matrices) under the null hypothesis of exchangeable off-diagonal elements. As described in Segal et al. (2019), these tests can be useful for construct validation either by themselves or as a complement to confirmatory factor analysis. Monte Carlo methods are used to approximate the permutation p-value with Hubert's Gamma (Hubert, 1976) and a t-statistic. This package also implements the chi-squared statistic described by Steiger (1980). Please see Segal, et al. (2019) <doi:10.1007/s11336-018-9647-4> for more information. |
Authors: | Brian D. Segal [aut, cre] |
Maintainer: | Brian D. Segal <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.0.0 |
Built: | 2025-01-22 05:11:48 UTC |
Source: | https://github.com/bdsegal/matrixstructest |
A dataset containing answers to a Big Five Personality Test conducted by http://personality-testing.info. These data were collected (c. 2012) through an interactive online personality test. The test was constructed with items from the International Personality Item Pool. Participants were informed that their responses would be recorded and used for research at the beginning of the test and asked to confirm their consent at the end of the test. The items were rated on a five point scale where 1=Disagree, 3=Neutral, 5=Agree. All were presented on one page in the order E1, N1, A1, C1, O1, E2,...
big5
big5
A data frame with 19,719 rows of 57 variables:
1=Mixed Race, 2=Arctic (Siberian, Eskimo), 3=Caucasian (European), 4=Caucasian (Indian), 5=Caucasian (Middle East), 6=Caucasian (North African, Other), 7=Indigenous Australian, 8=Native American, 9=North East Asian (Mongol, Tibetan, Korean Japanese, etc), 10=Pacific (Polynesian, Micronesian, etc), 11=South East Asian (Chinese, Thai, Malay, Filipino, etc), 12=West African, Bushmen, Ethiopian, 13=Other
Entered as text (individuals reporting age < 13 were not recorded)
Response to "is English your native language?". 1=yes, 2=no
1=Male, 2=Female, 3=Other
"What hand do you use to write with?". 1=Right, 2=Left, 3=Both
The participant's technical location. ISO country code
How the participant came to the test. Based on HTTP Referer. 1=from another page on the test website, 2=from google, 3=from facebook, 4=from any url with ".edu" in its domain name, 6=other source, or HTTP Referer not provided
I am the life of the party.
I don't talk a lot.
I feel comfortable around people.
I keep in the background.
I start conversations.
I have little to say.
I talk to a lot of different people at parties.
I don't like to draw attention to myself.
I don't mind being the center of attention.
I am quiet around strangers.
I get stressed out easily.
I am relaxed most of the time.
I worry about things.
I seldom feel blue.
I am easily disturbed.
I get upset easily.
I change my mood a lot.
I have frequent mood swings.
I get irritated easily.
I often feel blue.
I feel little concern for others.
I am interested in people.
I insult people.
I sympathize with others' feelings.
I am not interested in other people's problems.
I have a soft heart.
I am not really interested in others.
I take time out for others.
I feel others' emotions.
I make people feel at ease.
I am always prepared.
I leave my belongings around.
I pay attention to details.
I make a mess of things.
I get chores done right away.
I often forget to put things back in their proper place.
I like order.
I shirk my duties.
I follow a schedule.
I am exacting in my work.
I have a rich vocabulary.
I have difficulty understanding abstract ideas.
I have a vivid imagination.
I am not interested in abstract ideas.
I have excellent ideas.
I do not have a good imagination.
I am quick to understand things.
I use difficult words.
I spend time reflecting on things.
I am full of ideas.
This dataset is for demonstration purposes only. Please see http://personality-testing.info/privacypolicy.html and http://personality-testing.info/about for more information.
http://personality-testing.info/_rawdata/
This sub-routine outputs 1 if i and j are in at least one group together,
and 0 otherwise, and is called by matrixStrucTest
and
prepBoxPlots
.
deltaSub(i, j, group_list)
deltaSub(i, j, group_list)
i |
First index |
j |
Second index |
group_list |
List of indices for each block |
This sub-routine is called by matrixStrucTest
and prepBoxPlots
.
makeGroupList(groups, A)
makeGroupList(groups, A)
groups |
Character string in lavaan syntax specifying groups |
A |
A Distance or similarity matrix. Must have column names |
group_list List of column indices of A corresponding to each group
This function computes permutation p-values for Hubert's Gamma and t-statistics for both overall and block-specific tests.
matrixStrucTest(A, group_list = NULL, groups = NULL, B = 1000, absolute = TRUE)
matrixStrucTest(A, group_list = NULL, groups = NULL, B = 1000, absolute = TRUE)
A |
Distance or similarity matrix, e.g. correlation |
group_list |
List of column indices of A for each group. Either |
groups |
CFA model in lavaan syntax. Either |
B |
Number of Monte Carlo resamples (defaults to B=1000) |
absolute |
Use the absolute values of A (defaults to TRUE) |
pt_overall_one_sided: Overall one-sided p-value using t statistic
pt_overall_two_sided: Overall two-sided p-value using t statistic
pt_multi_one_sided: Block-specific one-sided p-values using t statistic
pt_multi_two_sided: Block-specific two-sided p-values using t statistic
t0 Observed overall: t statistic
t0k: Observed block-specific t statistic
t_overall: Vector of overall t statistics from permuted A
t_max_one_sided: Vector of max t statistics from permuted A (one-sided)
t_max_two_sided: Vector of max t statistics from permuted A (two-sided)
pG_overall_one_sided: Overall one-sided p-value using Hubert's Gamma
pG_overall_two_sided: Overall two-sided p-value using Hubert's Gamma
pG_multi_one_sided: Block-specific one-sided p-values using Hubert's Gamma
pG_multi_two_sided: Block-specific two-sided p-values using Hubert's Gamma
Gamma0: Observed overall Hubert's Gamma
Gamma0k: Observed block-specific Hubert's Gamma
Gamma_overall: Vector of Hubert's Gamma statistics from permuted A
Gamma_max_one_sided: Vector of max Hubert's Gamma statistics from permuted A (one-sided)
Gamma_max_two_sided: Vector of max Hubert's Gamma statistics from permuted A (two-sided)
B: number of Monte Carlo resamples
group_list: List of column/row indices corresponding to each group
# example for matrixStrucTest package library(matrixStrucTest) data("big5") # get column numbers for questionnaire items items <- grep("[0-9]", colnames(big5)) # compute Spearman's correlation matrix A <- cor(big5[, items], use = "complete.obs", method = "spearman") # specify the groups groups <- "extrovert ~ E1 + E2 + E3 + E4 + E5 + E6 + E7 + E8 + E9 + E10 neurotic ~ N1 + N2 + N3 + N4 + N5 + N6 + N7 + N8 + N9 + N10 agreeable ~ A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 conscientious ~ C1 + C2 + C3 + C4 + C5 + C6 + C7 + C8 + C9 + C10 open ~ O1 + O2 + O3 + O4 + O5 + O6 + O7 + O8 + O9 + O10" # compute permutation p-values # Note: Using small B for fast checking on CRAN. Set B >= 1000 in practice. result <- matrixStrucTest(A = A, groups = groups, B = 100, absolute = TRUE) # Note: two-sided p-values from Hubert's Gamma printed by default # other results available by directing accessing them from the # returned object result # Alternative approach for specifying the groups as a list of column/row indices extrovert <- grep("E", colnames(A)) neurotic <- grep("N", colnames(A)) agreeable <- grep("A", colnames(A)) conscientious <- grep("C", colnames(A)) open <- grep("O", colnames(A)) # put blocks/groups in list group_list <- list(extrovert = extrovert, neurotic = neurotic, agreeable = agreeable, conscientious = conscientious, open = open) # Note: Using small B for fast checking on CRAN. Set B >= 1000 in practice. result <- matrixStrucTest(A = A, group_list = group_list, B = 100, absolute = TRUE) # Note: two-sided p-values from Hubert's Gamma printed by default # other results available by directing accessing them from the # returned object result # Visualize groups library(ggplot2) library(reshape2) ord <- unlist(result$group_list) diag(A) <- NA # remove diagonals from color scale Am <- melt(A[ord, ord]) names(Am) <- c("x", "y", "value") Am$y <- factor(Am$y, levels = rev(levels(Am$y))) ggplot(aes(x = x, y = y, fill = abs(value)), data = Am)+ geom_tile()+ theme_bw(18)+ scale_fill_gradient2(space="Lab", name="abs(Cor)", lim = c(0, 1))+ labs(x = "", y = "")+ theme(axis.text.x = element_text(angle = 90, vjust = .35,hjust=1))
# example for matrixStrucTest package library(matrixStrucTest) data("big5") # get column numbers for questionnaire items items <- grep("[0-9]", colnames(big5)) # compute Spearman's correlation matrix A <- cor(big5[, items], use = "complete.obs", method = "spearman") # specify the groups groups <- "extrovert ~ E1 + E2 + E3 + E4 + E5 + E6 + E7 + E8 + E9 + E10 neurotic ~ N1 + N2 + N3 + N4 + N5 + N6 + N7 + N8 + N9 + N10 agreeable ~ A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 conscientious ~ C1 + C2 + C3 + C4 + C5 + C6 + C7 + C8 + C9 + C10 open ~ O1 + O2 + O3 + O4 + O5 + O6 + O7 + O8 + O9 + O10" # compute permutation p-values # Note: Using small B for fast checking on CRAN. Set B >= 1000 in practice. result <- matrixStrucTest(A = A, groups = groups, B = 100, absolute = TRUE) # Note: two-sided p-values from Hubert's Gamma printed by default # other results available by directing accessing them from the # returned object result # Alternative approach for specifying the groups as a list of column/row indices extrovert <- grep("E", colnames(A)) neurotic <- grep("N", colnames(A)) agreeable <- grep("A", colnames(A)) conscientious <- grep("C", colnames(A)) open <- grep("O", colnames(A)) # put blocks/groups in list group_list <- list(extrovert = extrovert, neurotic = neurotic, agreeable = agreeable, conscientious = conscientious, open = open) # Note: Using small B for fast checking on CRAN. Set B >= 1000 in practice. result <- matrixStrucTest(A = A, group_list = group_list, B = 100, absolute = TRUE) # Note: two-sided p-values from Hubert's Gamma printed by default # other results available by directing accessing them from the # returned object result # Visualize groups library(ggplot2) library(reshape2) ord <- unlist(result$group_list) diag(A) <- NA # remove diagonals from color scale Am <- melt(A[ord, ord]) names(Am) <- c("x", "y", "value") Am$y <- factor(Am$y, levels = rev(levels(Am$y))) ggplot(aes(x = x, y = y, fill = abs(value)), data = Am)+ geom_tile()+ theme_bw(18)+ scale_fill_gradient2(space="Lab", name="abs(Cor)", lim = c(0, 1))+ labs(x = "", y = "")+ theme(axis.text.x = element_text(angle = 90, vjust = .35,hjust=1))
This sub-routine is called by matrixStrucTest
and prepBoxPlots
.
matrixStrucTestSub(A, group_list_ord, Delta, multi_group_ind, A_upper_ind, K)
matrixStrucTestSub(A, group_list_ord, Delta, multi_group_ind, A_upper_ind, K)
A |
Distance or similarity matrix, e.g. correlation |
group_list_ord |
List of groupings for ordered matrix A |
Delta |
Delta matrix |
multi_group_ind |
List of indicator matrices for membership in block k test |
A_upper_ind |
indicator matrix for upper triangular elements |
K |
Total number of hypothesized blocks |
Gamma_overall: Overall Hubert's Gamma
Gamma_multi: Block-specific Hubert's Gamma
t_overall: Overall t-statistic (unequal variance)
t_multi: Block-specific t-statistics
Ak_list: List of values in A used for each block-specific test
Deltak_list: List of values in Delta used for each block-specific test
A_upper: Upper triangular elements of A (used for box plot function)
Delta_upper: Upper triangular elements of Delta (used for box plot function)
This sub-routine outputs TRUE if either i
or j
are in
group
, FALSE otherwise, and is called by matrixStrucTest
and prepBoxPlots
.
multiSub(i, j, group)
multiSub(i, j, group)
i |
First index |
j |
Second index |
group |
Indices for items in group |
This function prepares the data for making box plots.
prepBoxPlots(A, groups = NULL, group_list = NULL, absolute = TRUE)
prepBoxPlots(A, groups = NULL, group_list = NULL, absolute = TRUE)
A |
Distance or similarity matrix, e.g. correlation |
groups |
CFA model in lavaan syntax. Either |
group_list |
List of groupings. Either |
absolute |
Use the absolute values of A (defaults to TRUE) |
multi: data frame for making box plots for block-specific tests
overall: data frame for making box plots for overall test
library(matrixStrucTest) library(ggplot2) data("big5") # get column numbers for questionnaire items items <- grep("[0-9]", colnames(big5)) # compute Spearman's correlation matrix A <- cor(big5[, items], use = "complete.obs", method = "spearman") groups <- "extrovert ~ E1 + E2 + E3 + E4 + E5 + E6 + E7 + E8 + E9 + E10 neurotic ~ N1 + N2 + N3 + N4 + N5 + N6 + N7 + N8 + N9 + N10 agreeable ~ A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 conscientious ~ C1 + C2 + C3 + C4 + C5 + C6 + C7 + C8 + C9 + C10 open ~ O1 + O2 + O3 + O4 + O5 + O6 + O7 + O8 + O9 + O10" # Make box plots contrasting within and between group correlations box <- prepBoxPlots(A = A, groups = groups, absolute = TRUE) ggplot(aes(x = as.factor(delta), y = a), data = box$overall)+ geom_boxplot()+ theme_bw(22)+ labs(x = expression(Delta), y="|a|") dev.new(width = 12, height = 5) ggplot(aes(x = as.factor(delta), y = a), data = box$multi)+ geom_boxplot()+ facet_grid(~block)+ theme_bw(22)+ labs(x = expression(Delta), y = "|a|")
library(matrixStrucTest) library(ggplot2) data("big5") # get column numbers for questionnaire items items <- grep("[0-9]", colnames(big5)) # compute Spearman's correlation matrix A <- cor(big5[, items], use = "complete.obs", method = "spearman") groups <- "extrovert ~ E1 + E2 + E3 + E4 + E5 + E6 + E7 + E8 + E9 + E10 neurotic ~ N1 + N2 + N3 + N4 + N5 + N6 + N7 + N8 + N9 + N10 agreeable ~ A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 conscientious ~ C1 + C2 + C3 + C4 + C5 + C6 + C7 + C8 + C9 + C10 open ~ O1 + O2 + O3 + O4 + O5 + O6 + O7 + O8 + O9 + O10" # Make box plots contrasting within and between group correlations box <- prepBoxPlots(A = A, groups = groups, absolute = TRUE) ggplot(aes(x = as.factor(delta), y = a), data = box$overall)+ geom_boxplot()+ theme_bw(22)+ labs(x = expression(Delta), y="|a|") dev.new(width = 12, height = 5) ggplot(aes(x = as.factor(delta), y = a), data = box$multi)+ geom_boxplot()+ facet_grid(~block)+ theme_bw(22)+ labs(x = expression(Delta), y = "|a|")
matrixStrucTest
This function prints results from an object returned by matrixStrucTest
.
## S3 method for class 'mst' print(x, ...)
## S3 method for class 'mst' print(x, ...)
x |
Output from matrixStrucTest |
... |
Further arguments passed to print |
This sub-routine is called by X2fun
.
sigmaRhoFun(j, k, h, m, A)
sigmaRhoFun(j, k, h, m, A)
j |
First index |
k |
Second index |
h |
Third index |
m |
Fourth index |
A |
Correlation matrix |
This sub-routine is called by X2fun
.
sigmaZFun(s, t, index, A, Sigma)
sigmaZFun(s, t, index, A, Sigma)
s |
First index |
t |
Second index |
index |
Matrix with two columns with index pairs given by rows |
A |
Correlation matrix |
Sigma |
Variance-covariances of correlation matrix A |
This function computed the goodness-of-fit chi-squared statistic described by Steiger (1980). Reference: Steiger, J. H. (1980). Tests for comparing elements of a correlation matrix. Psychological Bulletin, 87(2) 245-251.
X2Fun(data, group_list, corMethod = "spearman")
X2Fun(data, group_list, corMethod = "spearman")
data |
Data frame |
group_list |
List of column indices of A for each group |
corMethod |
Type of correlations; passed to cor() |