如何使用R从一组不同类型的变量中生成潜在变量?

时间:2019-06-12 13:42:21

标签: r

对于n个观测值,我想生成一个潜在变量(未观察到),我可以假设该变量是否具有特定的分布,这是由其他一组代表该潜在变量的变量组成的变量。对于我的特定情况,我想从一组代表代理能力(观察到的能力)的变量中生成潜在能力。一个变量是离散的,并且表现出正态性,另一个变量是二进制的,但是非常偏斜,最后一个是有序的分类变量。这看起来像我的数据,我想估计每个观察结果的响应。

set.seed(123877)
# number of units
n <- 1000L

# age
age <- sample(rnorm(n, 25, 10))

# cum laude 
hon <- sample(0L:1L, n, TRUE, prob = c(.9, .1) )

# prestige of university
pres <- factor(sample(1L:25L, n, TRUE), labels = 25L:1L, ordered = T)

dat <- data.frame(id=1L:n, age, hon, pres)

1 个答案:

答案 0 :(得分:0)

我使用ltm包找到了一个解决方案,下面是代码:

set.seed(123877)
u.latent <- vector()
class(u.latent) <- 'try-error'

library('ltm')
while (class(u.latent)=='try-error') {
# numer of units
n <- 1000L

# age
age <- round(rnorm(n, 25, 10))

# cum laude 
hon <- sample(0L:1L, n, TRUE, prob = c(.9, .1) )

# prestige of university
pres <- sample(1L:10L, n, TRUE)

# pres <-factor(pres, levels = 1L:25L, ordered = TRUE)
dat <- data.frame(age, hon, pres)

# latent variable  
u.latent <- try(gpcm(dat))  
}

我们可以测试模型是否适合数据:

GoF.gpcm(u.latent)
#H0 the model fits the data
#Ha: the model does not fit the data

潜在变量的估计很简单:

u.estimates <-factor.scores(u.latent)
hist(u.estimates$score.dat$z1)