readData: Read External Correlation/Covariance Matrices

readDataR Documentation

Read External Correlation/Covariance Matrices

Description

It reads full/lower triangle/stacked vectors of correlation/covariance data into a list of correlation/covariance matrices.

Usage

readFullMat(file, ...)
readStackVec(file, ...)
readLowTriMat(file, no.var, ...)

Arguments

file

File name of the data.

no.var

The number of variables in the data.

...

Further arguments to be passed to scan for readLowTriMat and to read.table for readFullMat and readStackVec.

Value

A list of correlation/covariance matrices.

Author(s)

Mike W.-L. Cheung <mikewlcheung@nus.edu.sg>

Examples


## Write two full correlation matrices into a file named "fullmat.dat".
## x2 is missing in the second matrix.
## The content of "fullmat.dat" is
# 1.0 0.3 0.4
# 0.3 1.0 0.5
# 0.4 0.5 1.0
# 1.0 NA 0.4
# NA NA NA
# 0.4 NA 1.0

## cat("1.0 0.3 0.4\n0.3 1.0 0.5\n0.4 0.5 1.0
## 1.0 NA 0.4\nNA NA NA\n0.4 NA 1.0",
## file="fullmat.dat", sep="")

## Read the correlation matrices from a file
## my.full <- readFullMat("fullmat.dat")

## Read the correlation matrices from a string
x <-
"1.0 0.3 0.4
0.3 1.0 0.5
0.4 0.5 1.0
1.0 NA 0.4
NA NA NA
0.4 NA 1.0"

my.full <- readFullMat(textConnection(x))

## my.full
# $`1`
#     x1  x2  x3
# x1 1.0 0.3 0.4
# x2 0.3 1.0 0.5
# x3 0.4 0.5 1.0
#
# $`2`
#     x1 x2  x3
# x1 1.0 NA 0.4
# x2  NA NA  NA
# x3 0.4 NA 1.0

## Write two lower triangle correlation matrices into a file named "lowertriangle.dat".
## x2 is missing in the second matrix.
## The content of "lowertriangle.dat" is
# 1.0 
# 0.3 1.0 
# 0.4 0.5 1.0
# 1.0
# NA NA 
# 0.4 NA 1.0
## cat("1.0\n0.3 1.0\n0.4 0.5 1.0\n1.0\nNA NA\n0.4 NA 1.0",
##     file="lowertriangle.dat", sep="")

## Read the lower triangle correlation matrices from a file
## my.lowertri <- readLowTriMat(file = "lowertriangle.dat", no.var = 3)

## Read the correlation matrices from a string
x <-
"1.0 
0.3 1.0 
0.4 0.5 1.0
1.0
NA NA 
0.4 NA 1.0"

my.lowertri <- readLowTriMat(textConnection(x), no.var = 3)

## my.lowertri
# $`1`
#     x1  x2  x3
# x1 1.0 0.3 0.4
# x2 0.3 1.0 0.5
# x3 0.4 0.5 1.0
#
# $`2`
#     x1 x2  x3
# x1 1.0 NA 0.4
# x2  NA NA  NA
# x3 0.4 NA 1.0

## Write two vectors of correlation coefficients based on
##  column major into a file named "stackvec.dat".
## x2 is missing in the second matrix.
## The content of "stackvec.dat" is
# 1.0 0.3 0.4 1.0 0.5 1.0
# 1.0 NA 0.4 NA NA 1.0
## cat("1.0 0.3 0.4 1.0 0.5 1.0\n1.0 NA 0.4 NA NA 1.0\n",
##     file="stackvec.dat", sep="")

## Read the stack vectors from a file
## my.vec <- readStackVec("stackvec.dat")

## Read the stack vectors from a string
x <- "
1.0 0.3 0.4 1.0 0.5 1.0
1.0 NA 0.4 NA NA 1.0"

my.vec <- readStackVec(textConnection(x))

## my.vec
# $`1`
#     x1  x2  x3
# x1 1.0 0.3 0.4
# x2 0.3 1.0 0.5
# x3 0.4 0.5 1.0
#
# $`2`
#    x1 x2  x3
# x1 1.0 NA 0.4
# x2  NA NA  NA
# x3 0.4 NA 1.0


mikewlcheung/metasem documentation built on Sept. 17, 2024, 7:06 a.m.