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

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

model <- glm(train$100 ~ train$1:99, data = train, family = "binomial")

There is no way to find the right way to do this in R ...

+4
source share
2 answers

If you need outcome ~ var1 + var2 + ... + varN, try the following:

# 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")
+4
source

The easiest way, assuming you want to use everything except column 100 as predictor variables,

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

where v100is the name of the 100th column (the name cannot be 100if you did not do something advanced / hidden to undermine the R rules about the column names of the data frame ...)

+3
source

Source: https://habr.com/ru/post/1526953/


All Articles