This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
set.seed(1)
n = 1000
nclass = 2
nitem = 2
nresp = c(2,2)
#class gamma
gamma = c(.30,.70)
#item - response prob
rho = list(
matrix(c(.8,.2,
.1,.9), 2,2,byrow = T),
matrix(c(.9,.1,
.1,.9), 2,2,byrow = T))
item = matrix(NA, nrow = n, ncol = nitem) # ; item
set.seed(1)
for (c in 1:nclass){
for (i in 1:n){
for (m in 1:nitem){
item[i,m] = sample(x=nresp[m], size = 1, replace = T, prob = rho[[m]][c,])
}
}
}
#item
library(poLCA)
## 필요한 패키지를 로딩중입니다: scatterplot3d
## 필요한 패키지를 로딩중입니다: MASS
f = cbind(V1,V2)~1
item.lc2 = as.data.frame(item)
test.lc2 = poLCA(f, item.lc2, nclass=2)
## Conditional item response (column) probabilities,
## by outcome variable, for each class (row)
##
## $V1
## Pr(1) Pr(2)
## class 1: 0.2729 0.7271
## class 2: 0.0631 0.9369
##
## $V2
## Pr(1) Pr(2)
## class 1: 0.1074 0.8926
## class 2: 0.1019 0.8981
##
## Estimated class population shares
## 0.1997 0.8003
##
## Predicted class memberships (by modal posterior prob.)
## 0.105 0.895
##
## =========================================================
## Fit for 2 latent classes:
## =========================================================
## number of observations: 1000
## number of estimated parameters: 5
## residual degrees of freedom: -2
## maximum log-likelihood: -667.5553
##
## AIC(2): 1345.111
## BIC(2): 1369.649
## G^2(2): 9.787147e-10 (Likelihood ratio/deviance statistic)
## X^2(2): 9.901484e-10 (Chi-square goodness of fit)
##
## ALERT: negative degrees of freedom; respecify model
##
###########poLCA 논문 자료 연습####################### ###################################################### # Basic latent class modeling with the carcinoma data ######################################################
## 'data.frame': 1202 obs. of 4 variables:
## $ PURPOSE : Factor w/ 3 levels "Good","Depends",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ ACCURACY: Factor w/ 2 levels "Mostly true",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ UNDERSTA: Factor w/ 2 levels "Good","Fair/Poor": 1 1 1 1 1 1 1 1 1 1 ...
## $ COOPERAT: Factor w/ 3 levels "Interested","Cooperative",..: 1 1 1 1 1 1 1 1 1 1 ...
## Conditional item response (column) probabilities,
## by outcome variable, for each class (row)
##
## $PURPOSE
## Good Depends Waste of time
## class 1: 0.2154 0.2066 0.5780
## class 2: 0.8953 0.0579 0.0468
##
## $ACCURACY
## Mostly true Not true
## class 1: 0.0297 0.9703
## class 2: 0.6367 0.3633
##
## $UNDERSTA
## Good Fair/Poor
## class 1: 0.7422 0.2578
## class 2: 0.8327 0.1673
##
## $COOPERAT
## Interested Cooperative Impatient
## class 1: 0.6478 0.2498 0.1024
## class 2: 0.8840 0.1043 0.0117
##
## Estimated class population shares
## 0.1923 0.8077
##
## Predicted class memberships (by modal posterior prob.)
## 0.1864 0.8136
##
## =========================================================
## Fit for 2 latent classes:
## =========================================================
## number of observations: 1202
## number of estimated parameters: 13
## residual degrees of freedom: 22
## maximum log-likelihood: -2783.268
##
## AIC(2): 5592.536
## BIC(2): 5658.729
## G^2(2): 79.33723 (Likelihood ratio/deviance statistic)
## X^2(2): 93.25329 (Chi-square goodness of fit)
##
## Interested Cooperative Impatient
## 4.940145 0.7603678 0.1613179
## Conditional item response (column) probabilities,
## by outcome variable, for each class (row)
##
## $A
## Pr(1) Pr(2)
## class 1: 0.0000 1.0000
## class 2: 0.8835 0.1165
##
## $B
## Pr(1) Pr(2)
## class 1: 0.0169 0.9831
## class 2: 0.6456 0.3544
##
## $C
## Pr(1) Pr(2)
## class 1: 0.2391 0.7609
## class 2: 1.0000 0.0000
##
## $D
## Pr(1) Pr(2)
## class 1: 0.4589 0.5411
## class 2: 1.0000 0.0000
##
## $E
## Pr(1) Pr(2)
## class 1: 0.0214 0.9786
## class 2: 0.7771 0.2229
##
## $F
## Pr(1) Pr(2)
## class 1: 0.5773 0.4227
## class 2: 1.0000 0.0000
##
## $G
## Pr(1) Pr(2)
## class 1: 0.0000 1.0000
## class 2: 0.8835 0.1165
##
## Estimated class population shares
## 0.5012 0.4988
##
## Predicted class memberships (by modal posterior prob.)
## 0.5 0.5
##
## =========================================================
## Fit for 2 latent classes:
## =========================================================
## number of observations: 118
## number of estimated parameters: 15
## residual degrees of freedom: 103
## maximum log-likelihood: -317.2568
##
## AIC(2): 664.5137
## BIC(2): 706.0739
## G^2(2): 62.36543 (Likelihood ratio/deviance statistic)
## X^2(2): 92.64814 (Chi-square goodness of fit)
##
## Conditional item response (column) probabilities,
## by outcome variable, for each class (row)
##
## $A
## Pr(1) Pr(2)
## class 1: 0.4872 0.5128
## class 2: 0.0000 1.0000
## class 3: 0.9427 0.0573
##
## $B
## Pr(1) Pr(2)
## class 1: 0.0000 1.0000
## class 2: 0.0191 0.9809
## class 3: 0.8621 0.1379
##
## $C
## Pr(1) Pr(2)
## class 1: 1.0000 0.0000
## class 2: 0.1425 0.8575
## class 3: 1.0000 0.0000
##
## $D
## Pr(1) Pr(2)
## class 1: 0.9424 0.0576
## class 2: 0.4138 0.5862
## class 3: 1.0000 0.0000
##
## $E
## Pr(1) Pr(2)
## class 1: 0.2494 0.7506
## class 2: 0.0000 1.0000
## class 3: 0.9449 0.0551
##
## $F
## Pr(1) Pr(2)
## class 1: 1.0000 0.0000
## class 2: 0.5236 0.4764
## class 3: 1.0000 0.0000
##
## $G
## Pr(1) Pr(2)
## class 1: 0.3693 0.6307
## class 2: 0.0000 1.0000
## class 3: 1.0000 0.0000
##
## Estimated class population shares
## 0.1817 0.4447 0.3736
##
## Predicted class memberships (by modal posterior prob.)
## 0.1949 0.4322 0.3729
##
## =========================================================
## Fit for 3 latent classes:
## =========================================================
## number of observations: 118
## number of estimated parameters: 23
## residual degrees of freedom: 95
## maximum log-likelihood: -293.705
##
## AIC(3): 633.41
## BIC(3): 697.1357
## G^2(3): 15.26171 (Likelihood ratio/deviance statistic)
## X^2(3): 20.50335 (Chi-square goodness of fit)
##
## Conditional item response (column) probabilities,
## by outcome variable, for each class (row)
##
## $A
## Pr(1) Pr(2)
## class 1: 0.0000 1.0000
## class 2: 0.6112 0.3888
## class 3: 0.9411 0.0589
## class 4: 0.0000 1.0000
##
## $B
## Pr(1) Pr(2)
## class 1: 0.0404 0.9596
## class 2: 0.0000 1.0000
## class 3: 0.8713 0.1287
## class 4: 0.0000 1.0000
##
## $C
## Pr(1) Pr(2)
## class 1: 0.2688 0.7312
## class 2: 1.0000 0.0000
## class 3: 1.0000 0.0000
## class 4: 0.1516 0.8484
##
## $D
## Pr(1) Pr(2)
## class 1: 0.6788 0.3212
## class 2: 1.0000 0.0000
## class 3: 1.0000 0.0000
## class 4: 0.2415 0.7585
##
## $E
## Pr(1) Pr(2)
## class 1: 0.0478 0.9522
## class 2: 0.2503 0.7497
## class 3: 0.9477 0.0523
## class 4: 0.0000 1.0000
##
## $F
## Pr(1) Pr(2)
## class 1: 1.0000 0.0000
## class 2: 1.0000 0.0000
## class 3: 1.0000 0.0000
## class 4: 0.2115 0.7885
##
## $G
## Pr(1) Pr(2)
## class 1: 0.0000 1.0000
## class 2: 0.4679 0.5321
## class 3: 1.0000 0.0000
## class 4: 0.0000 1.0000
##
## Estimated class population shares
## 0.2098 0.1519 0.3696 0.2687
##
## Predicted class memberships (by modal posterior prob.)
## 0.2881 0.1271 0.3729 0.2119
##
## =========================================================
## Fit for 4 latent classes:
## =========================================================
## number of observations: 118
## number of estimated parameters: 31
## residual degrees of freedom: 87
## maximum log-likelihood: -289.7889
##
## AIC(4): 641.5778
## BIC(4): 727.469
## G^2(4): 7.429509 (Likelihood ratio/deviance statistic)
## X^2(4): 11.64706 (Chi-square goodness of fit)
##
단락 분리해보자
4개의 초이스
(1) extremely well
(2) quite well
(3) not too well
(4) not well at all