# NOT RUN {
x = data.frame(ordered(rbinom(100,1,.5))); names(x) <- c("x")
umxThresholdMatrix(x)
x = cut(rnorm(100), breaks = c(-Inf,.2,.5, .7, Inf)); levels(x) = 1:5
x = data.frame(ordered(x)); names(x) <- c("x")
tmp = umxThresholdMatrix(x)
# ==================
# = Binary example =
# ==================
require(umx)
if(!packageVersion("OpenMx") > 2.5){message("Update OpenMx to get a new version of twinData")}
data(twinData)
# ===============================================================
# = Create a series of binary and ordinal columns to work with =
# ===============================================================
# Cut to form category of 80 % obese subjects
selDVs = c("obese1", "obese2")
obesityLevels = c('normal', 'obese')
cutPoints <- quantile(twinData[, "bmi1"], probs = .2, na.rm = TRUE)
twinData$obese1 <- cut(twinData$bmi1, breaks = c(-Inf, cutPoints, Inf), labels = obesityLevels)
twinData$obese2 <- cut(twinData$bmi2, breaks = c(-Inf, cutPoints, Inf), labels = obesityLevels)
# Step 2: Make the ordinal variables into mxFactors
# this ensures ordered= TRUE + requires user to confirm levels
twinData[, selDVs] <- mxFactor(twinData[, selDVs], levels = obesityLevels)
# Repeat for three-level weight variable
selDVs = c("obeseTri1", "obeseTri2")
obesityLevels = c('normal', 'overweight', 'obese')
cutPoints <- quantile(twinData[, "bmi1"], probs = c(.4, .7), na.rm = TRUE)
twinData$obeseTri1 <- cut(twinData$bmi1, breaks = c(-Inf, cutPoints, Inf), labels = obesityLevels)
twinData$obeseTri2 <- cut(twinData$bmi2, breaks = c(-Inf, cutPoints, Inf), labels = obesityLevels)
twinData[, selDVs] <- mxFactor(twinData[, selDVs], levels = obesityLevels)
selDVs = c("obeseQuad1", "obeseQuad2")
obesityLevels = c('underWeight', 'normal', 'overweight', 'obese')
cutPoints <- quantile(twinData[, "bmi1"], probs = c(.25, .4, .7), na.rm = TRUE)
twinData$obeseQuad1 <- cut(twinData$bmi1, breaks = c(-Inf, cutPoints, Inf), labels = obesityLevels)
twinData$obeseQuad2 <- cut(twinData$bmi2, breaks = c(-Inf, cutPoints, Inf), labels = obesityLevels)
twinData[, selDVs] <- mxFactor(twinData[, selDVs], levels = obesityLevels)
# Example 1
selDVs = c("obese1", "obese2")
tmp = umxThresholdMatrix(twinData[,selDVs], sep = "", verbose = TRUE) # informative messages
# ======================================
# = Ordinal (n categories > 2) example =
# ======================================
selDVs = c("obeseTri1", "obeseTri2")
tmp = umxThresholdMatrix(twinData[,selDVs], sep = "", verbose = TRUE)
# ========================================================
# = Mix of all three kinds example (and a 4-level trait) =
# ========================================================
selDVs = umx_paste_names(c("bmi", "obese", "obeseTri", "obeseQuad"), "", 1:2)
tmp = umxThresholdMatrix(twinData[,selDVs], sep = "", verbose = TRUE)
str(tmp)
# }
Run the code above in your browser using DataLab