我试图在R中执行潜在的类分析,但我有一些变量既是连续的,也是分类的。此外,我有52个状态或行,我正在尝试有52个潜在的类或子组。我开始用R编写代码,但是我得到了一个错误。下面是错误:Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
这是我的R代码
#Getting data into R
library(haven)
Component_3_database_11022018 <- read_sav("C:/Users/gaurelien/WRMA/APS-TARC - Documents/Evaluation/Component 3 Research Study/Data Analysis/SPSS/Source/Component 3 database 11022018.sav")
#Create a subset of the full data set reduced to 52 states
LCA<-subset(Component_3_database_11022018, State52==1)
#Loading packages
library(mclust)
library(poLCA) # only categorical indicators
library(scatterplot3d)
library(MASS)
library(orthopolynom)
library(polynom)
library(nlsem)
library(nnet)
library(Rsolnp)
library(depmixS4)
#Latent class Modeling with component 3 data
# Construcution of the dependent Mixture Models
#To avoid time-consuming mistakes in model specification, the analysis involves two steps:
#construction of a model with mix function and fitting it with fit function. family argument
#of mix function allows specifying a type of observed variables – whether they are continuous, nominal,
#or count by adding to a list corresponding distribution name, i.g. guassian or multinomial.
model_definition <- mix(list(AgencyLocation ~1, GeographicStructure ~1 , EligibilityCode ~1,
Maltreatment_Definitions_group ~ 1 ,ratio_report_per_investigator ~ 1,
census_TotalPop ~ 1, percent_belowpovertylevel_12months ~1),
family=list(multinomial(), #For every corresponding
multinomial(), # indicator a family of distribution
multinomial(),
multinomial(),
multinomial(),
multinomial(),
multinomial()), # should be indicated in the list.
data= LCA,
nstates=52,
initdata =LCA)
fit.mod <- fit(model_definition)发布于 2019-02-08 03:15:11
从技术上讲,潜在类分析应该只用于分类观察变量,而不应该用于连续变量。这就是为什么你的模型不收敛,特别是当你的连续变量有很多变化的时候。对于你的连续变量,如果可以的话,你应该尝试将它们二分法。换句话说,你应该减少它们的变化。然后再次运行您的模型。
此外,您应该尝试运行一个观察变量数量较少(少于10个)的模型。然后,当您达到收敛时,慢慢添加更多变量,并关注您的最小BIC。
如果你想保持你的连续变量不变,你可以尝试潜在轮廓分析,它允许连续变量和分类变量。此外,尽管有些人可能会对此提出异议,但我已经看到在同一模型中使用顺序变量和连续变量的结构方程建模,这将允许您保留连续变量。
发布于 2019-02-27 17:46:56
R不是最好的潜在类分析软件。我会推荐使用一个(付费的)替代品:潜在黄金或Mplus。
它们都有扩展,您可以将连续数据和分类数据结合起来进行潜在类分析。我知道它可能很贵,但它们比目前R的任何软件包都要快得多,也灵活得多。
https://stackoverflow.com/questions/53774249
复制相似问题