我正在尝试使用R中的poLCA包进行潜在类分析。我有284个变量(列)。逐个写入所有数据帧参数(列)是麻烦的并且是不可取的。我正在寻找一种简洁的方式来编写列而无需多打字。我已经生成了样本数据框,我用它来拟合一个简单的LCA模型:
library(poLCA);
set.seed(134);
#data
df<-as.data.frame(cbind(v1=rbinom(100, 1, 0.2)+1,
v2=rbinom(100,1,0.4)+1,
v3=rbinom(100,1,0.3)+1,
v4=rbinom(100, 1, 0.2)+1,
v5=rbinom(100,1,0.1)+1,
v6=rbinom(100,1,0.35)+1,
v7=rbinom(100, 1, 0.25)+1,
v8=rbinom(100,1,0.45)+1,
v9=rbinom(100,1,0.32)+1,
v10=rbinom(100, 1, 0.29)+1));
#poLCA model estimation - runs fine
f1<-cbind(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10)~1;
system.time(fit<-poLCA(f1,data=df, nclass=3, maxiter=1000, graphs=FALSE,
tol=1e-10, na.rm=TRUE, probs.start=NULL));
#Instead of typing v1, v2, v3,.....,v10 in the formula 'f1' above, I re-wrote 'f1'
as follows:
f2<-cbind(paste("v", c(1:10), sep=""))~1
#The above formula, 'f2', however combines all data frame arguments (columns) with
"double quote" wrapped around. Entering this formula inside poLCA function generates
errors as "non-numeric arguments with mathematical function".
#Thus, poLCA function below generates errors because f2 was not created correctly!!!
system.time(fit<-poLCA(f2, data=df, nclass=3, maxiter=1000, graphs=FALSE,
tol=1e-10, na.rm=TRUE, probs.start=NULL));
如何在上面创建f2等效的f1?我非常感谢您的回复。谢谢。
(系统信息:&#34; R版本3.1.0;&#34; x86_64-w64-mingw32&#34 ;;&#34; poLCA_1.4.1&#34;)
答案 0 :(得分:0)
非常糟糕的解决方案。至少它有效,但我确信有更正确的方法。
f2 = as.formula(paste("cbind(", paste("v", c(1:10), sep = "", collapse = ","), ")~1", sep ="", collapse=""))