R: Miscellaneous helper functions for the psych package

psych.misc {psych} | R Documentation

## Miscellaneous helper functions for the psych package

### Description

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. `cor2` correlates two data.frames (of equal length). levels2numeric and char2numeric convert dataframe columns that are categorical/levels to numeric values.

### Usage

    psych.misc()
    lowerCor(x,digits=2,use="pairwise",method="pearson")
    cor2(x,y,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(pl,package="psych",dependencies
          = c("Depends", "Imports", "LinkingTo"),find=FALSE,skip=NULL)
     levels2numeric(x)
     char2numeric(x)
     isCorrelation(x) #test if an object is a symmetric matrix

### Arguments

`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
`f` | The object returned from either a factor analysis (fa) or a principal components analysis (principal)
`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
`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))
`pl` | The name of a package (or list of packages) to be activated and then have all the examples tested.
`package` | Find the dependencies for this package, e.g., psych
`dependencies` | Which type of dependency to examine?
`find` | Look up the dependencies, and then test all of their examples
`skip` | Do not test these dependencies

### Details

`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.

It is important to remember to not call `lowerCor` when all you need is `lowerMat`!

### Value

`tableF` is fast alternative to the table function for creating two way tables of numeric variables. It does not have any of the elegant checks of the table function and thus is much faster. Used in the `tetrachoric` and `polychoric` functions to maximize speed.

`lowerCor` Finds and prints (using `lowerMat`) 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.

`lowerMat`Shows the lower triangle of a matrix, rounded to digits with titles abbreviated to digits + 3

`progressBar` Display a series of dots as we progress through a slow loop (removed from anything using multicores).

`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) }

`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 (e.g., psych) are also loaded. This is used when developing revisions to the psych package to make sure the the other packages work. Some packages will not work and/or crash the system (e.g., DeducerPlugInScaling requires Java and even with Java, crashes when loaded, even if psych is not there!). Alternatively, if testing a long list of dependencies, you can skip the first part by specifying them by name.

`cor2` will find and display the correlations between two sets of variables, rounded to digits, using the other options. If x is a list of multiple sets (two or more), then all sets are correlated.

`levels2numeric`converts character data with levels to numeric data. Used in the SAPA analyses where we code some variables, (e.g., gender, education) with character codes to help in the documentation of files, but want to do analyses of correlations with other categorical variables.

`char2numeric`converts character data with levels to numeric data. Used for cases when data from questionnaires include the response categories rathere than numeric data. Unless the levels of the data are in meaningful order, the numeric results are not useful. Most useful if doing polychoric analyses.

### See Also

`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.

### Examples

    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
    bestItems(lb,3,cut=.1)
    #to make this a latex table
    #df2latex(bestItems(lb,2,cut=.2))
    #
    data(bfi.dictionary)
    f2 <- fa(bfi[1:10],2)
    fa.lookup(f2,bfi.dictionary)

    sa1 <-sat.act[1:2]
    sa2 <- sat.act[3:4]
    sa3 <- sat.act[5:6]
    cor2(sa1,sa2)
    cor2(list(sa1,sa2))  #show within set and between set cors
    cor2(list(sa1,sa2,sa3))

[Package psych version 1.7.8 Index]