简体   繁体   English

查找R中向量中可用值的组合总数

[英]Find total number of combinations of values available in a vector in R

All the input variables names (11 elements) are given here in this vector called x. 所有输入变量名称(11个元素)都在此称为x的向量中给出。 "qua" is the name of the output variable. “ qua”是输出变量的名称。

    x <- c("fa", "va", "ca", 
           "rs", "chl", "fsd",
           "tsd", "den",   "pH", 
           "sul", "alc")

I am trying to run a classification model with all possible combinations of the input variables and return the AIC, but I could do it taking one input variable at a time as shown in the code below: 我正在尝试使用所有可能的输入变量组合来运行分类模型并返回AIC,但是我可以一次使用一个输入变量来做到这一点,如下面的代码所示:

           var_aic <- data.frame(matrix(NA, ncol = 2, byrow = FALSE))
           colnames(var_aic) <- c("Variable", "AIC") 
           # var_aic variable defined null to store values later.

           # Now trying to store AIC of of all the models possible with its               
           # variables name taken into the account.

           for(i in 1:11){
             x <- as.formula(paste("qua ~ ", x[i]))
             model <- polr(x,  train, Hess = TRUE)
             temp <- data.frame(z[i],AIC(model))
             colnames(temp) <- c("Variable", "AIC")
             var_aic <<- rbind(var_aic, temp)
           }

Now I want to build a function which will give me result like 现在我想构建一个函数,它将给我类似的结果

         **Variable                AIC**
           fa                     1460.9
           va                     1399.4
           ca                     1678
           rs                     1460.9
           chl                    1399.4
           fsd                    1678
           tsd                    1460.9
           den                    1399.4
           pH                     1678
           sul                    1460.9
           alc                    1399.4
           fa + va                1233
           fa + ca                1800

           # Also i dont want fa + fa,..... repetitions of the same variable.

I am having a problem in doing this part. 我在执行此部分时遇到问题。 So what should I change or add so that it works? 那么,我应该对其进行更改或添加以使其起作用吗?

combi <- lapply(1:length(x), 
  function(y) apply(combn(x, y), 2, paste, collapse=" + ")
)    

combi.v <- unlist(combi)

length(combi.v) == sum(choose(length(x), 1:length(x)))
# TRUE

tail(combi.v)
# [1] "fa + va + ca + rs + fsd + tsd + den + pH + sul + alc"      
# [2] "fa + va + ca + chl + fsd + tsd + den + pH + sul + alc"     
# [3] "fa + va + rs + chl + fsd + tsd + den + pH + sul + alc"     
# [4] "fa + ca + rs + chl + fsd + tsd + den + pH + sul + alc"     
# [5] "va + ca + rs + chl + fsd + tsd + den + pH + sul + alc"     
# [6] "fa + va + ca + rs + chl + fsd + tsd + den + pH + sul + alc"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM