lvm2cor: Covariance To Correlation Coefficient in LVM

lvm2corR Documentation

Covariance To Correlation Coefficient in LVM

Description

Convert a covariance coefficient into a correlation coefficient in a lvm. Uses a delta method to compute the variance, confidence interval, and p-value associated with the test of no correlation.

Usage

lvmCov2Cor(
  object,
  var1,
  var2,
  null = 0,
  level = 0.95,
  robust = FALSE,
  ssc = FALSE,
  cluster = NULL
)

Arguments

object

a latent variable model (lvmfit object).

var1

the name of a latent or endogenous variable.

var2

the name of another latent or endogenous variable.

level

the confidence level used for the confidence interval.

ssc

should the standard errors of the coefficients be corrected for small sample bias?

cluster

the grouping variable relative to which the observations are iid.

Value

A data.frame containing in the fourth row the estimated correlation coefficient, standard error, confidence intervals, and p.value.

Examples


#### 0 - Simulate some data ####
library(lava)
mSim <-lvm(c(PEQ_poslife,PEQ_posself,PEQ_posmood,PEQ_possoc,PEQ_posbehav)~lv.peq,
           c(MEQ_mystical,MEQ_mood) ~ 1*lv.meq,
           c(MEQ_timespace,MEQ_ineffability) ~ lv.meq,
lv.meq[0:2]~1,
lv.peq[0:0.25]~1)
covariance(mSim) <- lv.peq ~ lv.meq
covariance(mSim) <- MEQ_timespace~MEQ_ineffability
latent(mSim) <- ~lv.peq+lv.meq

set.seed(10)
GS <- sim(mSim, 1e5, latent = TRUE) ## not observed
set.seed(10)
d <- sim(mSim, 1e3, latent = FALSE) ##  observed

#### Example 1: Fit a LVM with a single latent variable ####
m1 <-lvm(c(PEQ_poslife,PEQ_posself,PEQ_posmood,PEQ_possoc,PEQ_posbehav)~lv.peq)
latent(m1) <- ~lv.peq
e1 <- estimate(m1, d)

### 1.1 Correlation between endogenous
## lava output
keep.col <- c("PEQ_poslife","PEQ_posself")
attr(predict(e1),"cond.var")[keep.col,keep.col]
cov2cor(attr(predict(e1),"cond.var")[keep.col,keep.col])

## proposed method
lvmCov2Cor(e1, var1 = "PEQ_poslife", var2 = "PEQ_posself")

#### Example 2: Fit a LVM with two latent variables ####
m2 <- lvm(c(PEQ_poslife,PEQ_posself,PEQ_posmood,PEQ_possoc,PEQ_posbehav)~lv.peq,
         c(MEQ_mystical,MEQ_mood) ~ 1*lv.meq,
         c(MEQ_timespace,MEQ_ineffability) ~ lv.meq)
covariance(m2) <- lv.peq ~ lv.meq
covariance(m2) <- MEQ_timespace~MEQ_ineffability
latent(m2) <- ~lv.peq + lv.meq
e2 <- estimate(m2, d)

### 2.1: Correlation between endogenous
## approximated true value
c("var.meq" = var(GS$MEQ_timespace), "var.peq" = var(GS$MEQ_ineffability),
  "cov" = cov(GS$MEQ_timespace,GS$MEQ_ineffability),
  "cor" = cor(GS$MEQ_timespace,GS$MEQ_ineffability))

## estimate value
coef(e2, type = 9)["MEQ_timespace~~MEQ_ineffability",]

## proposed method
lvmCov2Cor(e2, var1 = "MEQ_timespace", var2 = "MEQ_ineffability")
lvmCov2Cor(e2, var1 = "MEQ_timespace", var2 = "MEQ_ineffability",
           robust = TRUE)

## using lava 
estimate(e2, f = function(x){

sigma1 <- x["MEQ_timespace~~MEQ_timespace"]
sigma2 <- x["MEQ_ineffability~~MEQ_ineffability"]
sigma12 <- x["MEQ_timespace~~MEQ_ineffability"]
lambda1 <- x["MEQ_timespace~lv.meq"]
lambda2 <- x["MEQ_ineffability~lv.meq"]
tau <- x["lv.meq~~lv.meq"]

Sigma1 <- sigma1 + lambda1^2*tau
Sigma2  <- sigma2 + lambda2^2*tau

return(c(sigma12/sqrt(Sigma1*Sigma2),
         (sigma12+lambda1*lambda2*tau)/sqrt(Sigma1*Sigma2)))
})

### 2.1: Correlation between latent variables
## approximated true value
c("var.meq" = var(GS$lv.meq), "var.peq" = var(GS$lv.peq),
  "cov" = cov(GS$lv.meq,GS$lv.peq), "cor" = cor(GS$lv.meq,GS$lv.peq))

## estimate value
coef(e2, type = 9)["lv.peq~~lv.meq",]

## Delta method via lvmCov2Cor
lvmCov2Cor(e2, var1 = "lv.meq", var2 = "lv.peq")
lvmCov2Cor(e2, var1 = "lv.meq", var2 = "lv.peq", robust = TRUE)

## using lava
estimate(e2, function(x){
a <- x["lv.meq~~lv.meq"]
b <- x["lv.peq~~lv.peq"]
c <- x["lv.peq~~lv.meq"]
  c(var.meq = a,
   var.peq = b,
   cov = c,
   cor = c/sqrt(a*b))
})

## not sure what is
keep.col <- c("lv.meq","lv.meq")
attr(predict(e2, x = manifest(e2), y = latent(e2)),"cond.var")
cov2cor(attr(predict(e2, x = manifest(e2), y = latent(e2)),"cond.var"))



bozenne/butils documentation built on Oct. 14, 2023, 6:19 a.m.