| psych.misc {psych} | R Documentation |
This is a set of minor, if not trivial, helper functions.
lowerCor finds the correlation of x variables and then prints them using
lowerMat which is a trivial, but useful, function to round off and print the lower triangle of a matrix.
reflect reflects the output of a factor analysis or principal components analysis so that one or more factors is reflected. (Requested by Alexander Weiss.)
progressBar prints out ... as a calling routine (e.g., tetrachoric) works through a tedious calculation. shannon finds the Shannon index (H) of diversity or of information. test.all tests all the examples in a package. best.items sorts a factor matrix for absolute values and displays the expanded items names. fa.lookup returns sorted factor analysis output with item labels.
psych.misc() lowerCor(x,digits=2,use="pairwise",method="pearson") lowerMat(R, digits = 2) tableF(x,y) reflect(f,flip=NULL) progressBar(value,max,label=NULL) shannon(x,correct=FALSE,base=2) test.all(p) best.items(x,c1=1,cut=.3, abs=TRUE, contents=NULL,digits=2) lookup(x,y,c1=NULL) fa.lookup(f,dictionary,digits=2)
R |
A rectangular matrix or data frame (probably a correlation matrix) |
x |
A data matrix or data frame or a vector depending upon the function. |
y |
A data matrix or data frame or a vector |
digits |
round to digits |
use |
Should pairwise deletion be done, or one of the other options to cor |
method |
"pearson", "kendall", "spearman" |
value |
the current value of some looping variable |
max |
The maximum value the loop will achieve |
label |
what function is looping |
f |
The object returned from either a factor analysis (fa) or a principal components analysis (principal) |
flip |
The factor or components to be reversed keyed (by factor number) |
correct |
Correct for the maximum possible information in this item |
base |
What is the base for the log function (default=2, e implies base = exp(1)) |
p |
The name of a package to be activated and then have all the examples tested. |
c1 |
The column number or name of the factor loading to sort on for best.items or to find in lookup |
cut |
Return all values in abs(x[,c1]) > cut. |
abs |
if TRUE, sort by absolute value in best.items |
contents |
a two column data.frame of item names and item content. Allows for long values for item output, even though the column names are short. |
dictionary |
a data.frame with rownames corresponding to rownames in the f$loadings matrix, and entries (may be multiple columns) of item content. |
corFiml is a very short wrapper function that uses the power of the lavaan package. corFiml calls three lavaan functions: getMissingPatterns, getMissingPatternStats and estimate.moments.fiml. corFiml is included here to allow for Full Information Maximum Likelihood (FIML) correlation and covariance matrices without digging into the lavaan package. lavaan in turn requires a number of other packages, including boot, MASS, mnormt, pbivnorm, and quadprog.
lowerCor prints out the lower off diagonal matrix rounded to digits with column names abbreviated to digits + 3 characters, but also returns the full and unrounded matrix. By default, it uses pairwise deletion of variables. It in turn calls
lowerMat which does the pretty printing.
best.items and lookup are simple helper functions to summarize correlation matrices or factor loading matrices. best.items will sort the specified column (c1) of x on the basis of the (absolute) value of the column. The return as a default is just the rowname of the variable with those absolute values > cut. If there is a dictionary of item content and item names, then include the contents as a two column matrix with item name and item content.
lookup is used by best.items and will find values in c1 of y that match those in x. It returns those rows of y of that match x.
Suppose that you have a "dictionary" of the many variables in a study but you want to consider a small subset of them in a data set x. Then, you can find the entries in the dictionary corresponding to x by lookup(rownames(x),y) If the column is not specified, then it will match by rownames(y).
fa.lookup is used when examining the output of a factor analysis and one wants the corresponding variable names and contents. The returned object may then be printed in LaTex by using the df2latex function with the char option set to TRUE.
Similarly, given a correlation matrix, r, of the x variables, if you want to find the items that most correlate with another item or scale, and then show the contents of that item from the dictionary, best.items(r,c1=column number or name of x, contents = y)
The lower triangle of a matrix, rounded to digits with titles abbreviated to digits + 3 (lowerMat) or a series of dots (progressBar).
lowerCor prints the lower diagonal correlation matrix but returns (invisibly) the full correlation matrix found with the use and method parameters. The default values are for pairwise deletion of variables, and to print to 2 decimal places.
tableF (for tableFast) is a cut down version of table that does no error checking, nor returns pretty output, but is significantly faster than table. It will just work on two integer vectors. This is used in polychoric an tetrachoric for about a 50% speed improvement for large problems.
shannon finds Shannon's H index of information. Used for estimating the complexity or diversity of the distribution of responses in a vector or matrix.
H = -∑{p_i log(p_i) }
link{test.all} allows one to test all the examples in specified package. This allows us to make sure that those examples work when other packages are also loaded.
best.items returns a sorted list of factor loadings or correlations with the labels as provided in the contents field.
lookup is a very simple implementation of the match function.
fa.lookup takes a factor/cluster analysis object (or just a keys like matrix), sorts it using fa.sort and then matches by row.name to the corresponding dictionary entries.
corr.test to find correlations, count the pairwise occurrences, and to give significance tests for each correlation. r.test for a number of tests of correlations, including tests of the difference between correlations. lowerUpper will display the differences between two matrices.
lowerMat(Thurstone)
lb <- lowerCor(bfi[1:10]) #finds and prints the lower correlation matrix,
# returns the square matrix.
#fiml <- corFiml(bfi[1:10]) #FIML correlations require lavaan package
#lowerMat(fiml) #to get pretty output
f3 <- fa(Thurstone,3)
f3r <- reflect(f3,2) #reflect the second factor
#find the complexity of the response patterns of the iqitems.
round(shannon(iqitems),2)
#test.all('BinNor') #Does the BinNor package work when we are using other packages
best.items(lb,3,cut=.1)
#to make this a latex table
#df2latex(best.items(lb,2,cut=.2))
#
data(bfi.dictionary)
f2 <- fa(bfi[1:10],2)
fa.lookup(f2,bfi.dictionary)