Как выполнить логистическую регрессию для нескольких переменных (столбцы в кадре данных в R) - PullRequest
0 голосов
/ 01 июля 2019

У меня есть набор данных из более чем 200 наблюдений и более 70 переменных по всем категориям.Я выполняю glm для пар переменных, используя приведенный ниже код, который отлично работает.

#Checking if there is an association between growth and antibiotic exposure among cases
modelF <- glm(case$growth~case$antibiotic_exposure, family = "binomial")
summary(modelF)

#Checking for the odd ratios and confidence interval 
exp(cbind(OR = coef(modelF),confint(modelF)))

#Checking if there is an association between growth and asthma among cases
modelH <- glm(case$growth~case$asthma, family = "binomial")
summary(modelH)

#Checking for the odd ratios and confidence interval 
exp(cbind(OR = coef(modelH),confint(modelH)))

Мне понравится цикл for, который будет выполнять вышеуказанную функцию за один раз.Я новичок в R, и я буду очень признателен за любую помощь в создании кода ниже.

Я пробовал разные коды, но продолжал получать только некоторые части кодов в цикле для работы.

#dataframe is assigned to "case"
str(case)
'data.frame':   297 obs. of  79 variables:
$ growth                  : Factor w/ 2 levels "negative","positive": 1 2 1 1 1 1 1 2 2 2 ...
 $ pid_type                : Factor w/ 2 levels "case","control": 1 1 1 1 1 1 1 1 1 1 ...
 $ sampling_site           : Factor w/ 3 levels "lesional","nasal",..: 3 2 1 3 2 1 3 2 1 3 ...

код, который я написал до сих пор

for(i in colnames(case)){
  print(i)
  model<- glm(case$growth~case[[i]], family = "binomial")
  summary(model)
  print(i)
  exp(cbind(OR =coef(model, confint(model))))
      }

Я ожидал, что будут напечатаны имена столбцов (что является единственной частью работающего кода) с последующим кратким изложением модели, имени столбца иИЛИ и confint.

Только печатаются имена столбцов.

> for(i in colnames(case)){
+   print(i)
+   model<- glm(case$growth~case[[i]], family = "binomial")
+   summary(model)
+   print(i)
+   exp(cbind(OR =coef(model, confint(model))))
+       }
[1] "pid"
[1] "pid"
Waiting for profiling to be done...
[1] "pid_type"
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels
In addition: There were 50 or more warnings (use warnings() to see the first 50)
...