【问题标题】:How to generate a latent variable from a set of different kinds of variables with R?如何使用 R 从一组不同类型的变量中生成潜在变量?
【发布时间】:2019-06-12 13:42:21
【问题描述】:

对于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)

【问题讨论】:

  • 您在寻求模拟建议吗?您的问题似乎更像是一个统计问题,而不是适合 Stack Overflow 的特定编程问题。 Cross Validated 是寻求统计帮助的正确位置。对潜在变量进行逆向工程似乎很奇怪。首先生成该变量似乎更有意义,然后将代理生成为该潜在变量值的函数。
  • 我不是要模拟,也许我的问题更适合交叉验证,而且,也许 R 中有一些包可以做到这一点。例如,我刚刚发现了“ltm”包。
  • @MrFlick,我找到了解决方案,您认为我应该发布它,还是将问题删除,因为它对社区没有用处?
  • 如果您有问题的答案,请随时将其作为答案发布在下面以结束问题。

标签: r


【解决方案1】:

我找到了一个解决方案,使用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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多