Learn R Programming

amen (version 1.0)

rZ_ord_fc: Simulate Z given the partial ranks

Description

Simulates a random latent matrix Z given its expectation, dyadic correlation and partial rank information provided by W

Usage

rZ_ord_fc(Z, EZ, rho, Y)

Arguments

Z
a square matrix, the current value of Z
EZ
expected value of Z
rho
dyadic correlation
Y
matrix of ordinal data

Value

  • a square matrix, the new value of Z

Examples

Run this code
## The function is currently defined as
rZ_ord_fc<-
function (Z, EZ, rho,Y)
{
    # this could be done outside this function
    uY<-sort(unique(c(Y)))
    W<-matrix(match(Y,uY),nrow(Y),nrow(Y))
    W[is.na(W)] <- -1

    sz <- sqrt(1 - rho^2)
    ut <- upper.tri(Z)
    lt <- lower.tri(Z)
    for (w in sample(c(-1, 1:length(uY)))) {
        lb <- suppressWarnings(max(Z[!is.na(W) & W == w - 1],
            na.rm = TRUE))
        ub <- suppressWarnings(min(Z[!is.na(W) & W == w + 1],
            na.rm = TRUE))
        up <- ut & W == w
        ez <- EZ[up] + rho * (t(Z)[up] - t(EZ)[up])
        Z[up] <- ez + sz * qnorm(runif(sum(up), pnorm((lb - ez)/sz),
            pnorm((ub - ez)/sz)))
        up <- lt & W == w
        ez <- EZ[up] + rho * (t(Z)[up] - t(EZ)[up])
        Z[up] <- ez + sz * qnorm(runif(sum(up), pnorm((lb - ez)/sz),
            pnorm((ub - ez)/sz)))
    }
    diag(Z) <- rnorm(nrow(Z), diag(EZ), 1)
    Z
}

Run the code above in your browser using DataLab