Automatically create formulas for all possible linear models

后端 未结 3 794
暗喜
暗喜 2020-11-30 07:34

Say I have a training set in a data frame train with columns ColA, ColB, ColC, etc. One of these columns designates a bin

3条回答
  •  悲&欢浪女
    2020-11-30 08:34

    vars<-c('a','b','c','d')
    library(gregmisc) 
    indexes<-unique(apply(combinations(length(vars), length(vars), repeats=T), 1, unique))
    gen.form<-function(x) as.formula(paste('~',paste( vars[x],collapse='+')))
    formulas<-lapply(indexes, gen.form)
    formulas
    

    Generates:

    R> formulas

    [[1]] ~a

    [[2]] ~a + b

    [[3]] ~a + c

    [[4]] ~a + d

    [[5]] ~a + b + c

    [[6]] ~a + b + d

    [[7]] ~a + c + d

    [[8]] ~a + b + c + d

    [[9]] ~b

    [[10]] ~b + c

    [[11]] ~b + d

    [[12]] ~b + c + d

    [[13]] ~c

    [[14]] ~c + d

    [[15]] ~d

提交回复
热议问题