0
votes

I am trying to run a logistic regression on multiple dependent variables, 'traits', which are columns in a data frame, against a single independent variable, 'score'. I'm trying to do this as either one line of code or in a loop - but not succeeding at it!

For just doing columns 4 to 10, I've tried:

glm_output <- glm(data.matrix(myData[,4:10]) ~ myData$Sscore, na.action=na.exclude, family=binomial(link= "logit"))

I get the error message: "Error in eval(expr, envir, enclos) : for the 'binomial' family, y must be a vector of 0 and 1's or a 2 column matrix where col 1 is no. successes and col 2 is no. failures". My columns are factors, coded as 1 and 2's and it will happily do them individually. So I'm not sure why it doesn't like it.

I've also tried:

`for(i in names(myData))
{       
fit <- glm(myData[,i] ~ myData$score, data=myData, na.action=na.exclude) 
 }

However this gives me one coefficient rather than one for every column of the data frame.

Any help would be much appreciated!

1
It the formula y goes first, eg. myData$Sscore ~ data.matrix(myData[,4:10]). Could you provide myData?m-dz
Sscore is the independent variables, so is right as he wrote.adaien
Do you want as a result an array with your coefficient or a list with every fit?adaien
@adiana, my bad, of course you are right in this case.m-dz
@R.Mitchell, just extract what you need from fit inside the for loop and store it in a list or data.frame.m-dz

1 Answers

4
votes

Find all the name of the independent variables to use

regressors<-setdiff(names(myData),"Sscore")

Run the regression for every independent variable

lapply(regressors,function(name){
glm(myData[,name] ~ myData$Sscore, na.action=na.exclude, family=binomial(link= "logit"))
})

If you want the coefficients in an array you can type:

sapply(regressors,function(name){
coef(glm(myData[,name] ~ myData$Sscore, na.action=na.exclude, family=binomial(link= "logit")))
})