Confusion Matrix

32 views
Skip to first unread message

Elias Carvalho

unread,
Jan 28, 2019, 6:08:29 AM1/28/19
to lavaan
Dear Lavaan group

Iavaan provide us with a confusion matrix to calculate specificity, sensibility and accuracy?

Thanks

Terrence Jorgensen

unread,
Jan 29, 2019, 8:16:40 AM1/29/19
to lavaan
Iavaan provide us with a confusion matrix to calculate specificity, sensibility and accuracy?

No, lavaan stands for Latent Variable Analysis.  It is dedicated to analyses of covariance structure, so its observed/predicted data are not individual data points but rather elements of a covariance matrix (or polychoric correlation matrix estimated to underlie discrete variables).  It is trivial to construct these yourself for a particular binary outcome in your model.

myData <- read.table("http://www.statmodel.com/usersguide/chap5/ex5.16.dat")
names
(myData) <- c("u1","u2","u3","u4","u5","u6","x1","x2","x3","g")
myData$u1
<- ordered(myData$u1)
fit
<- sem('u1 + u2 ~ x1 + x2', data = myData)
## choose an outcome to focus on
DV
<- "u1"

## the slopes are in "gamma" because x1 and x2 are exogenous,
## but other slopes could be in $beta
(est <- lavInspect(fit, "est"))

## predicted values in probit scale
B
<- est$gamma[DV, ]
X
<- as.matrix(myData[ , c("x1","x2")])
yHat
<- X %*% B

## transform to probabilities
M
<- lavInspect(fit, "mean.ov")[DV] # 0
SD
<- sqrt(lavInspect(fit, "cov.ov")[DV, DV]) # 1
myData$yHat
<- qnorm(yHat, m = M, sd = SD)

## apply arbitrary cutoff of 50% to predict classification
myData$pred
.50 <- ifelse(myData$yHat >= .5, 1, 0)
tab
.50 <- table(obs = myData$u1, pred = myData$pred.50) # confusion matrix
## column %s show sensitivity (1) and specificity (0) on the diagonal
prop
.table(tab.50, margin = 2)
## row %s show PPV (1) and NPV (0) on the diagonal
prop
.table(tab.50, margin = 1)


Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Reply all
Reply to author
Forward
0 new messages