Setting a formula for GLM as a sum of columns in R

前端 未结 2 2011
北荒
北荒 2021-01-14 02:46

I am trying to set the formula for GLM as the ensemble of columns in train - train$1:99:

model <- glm(train$100 ~ t         


        
相关标签:
2条回答
  • The simplest way, assuming that you want to use all but column 100 as predictor variables, is

     model <- glm(v100 ~. , data = train, family = "binomial")
    

    where v100 is the name of the 100th column (the name can't be 100 unless you have done something advanced/sneaky to subvert R's rules about data frame column names ...)

    0 讨论(0)
  • 2021-01-14 03:11

    If you need outcome ~ var1 + var2 + ... + varN, then try this:

    # Name of the outcome column
    f1 <- colnames(train)[100]
    
    # Other columns seperated by "+"
    f2 <- paste(colnames(train)[1:99], collapse = "+")
    
    #glm
    model <- glm(formula = as.formula(paste(f1, f2, sep = "~")),
                 data = train,
                 family = "binomial")
    
    0 讨论(0)
提交回复
热议问题