1

I have a dataframe called repay and I have created a vector for the variables names of the variables I am interested in called variables.

variables<-names(repay)[22:36]

I want to write a for loop that does some univariate analysis on each of the variables in variables. For example:

for (i in 1:length(variables))
{
model<-glm(Successful~ variables[i]
,data=repay
,family=binomial(link='logit'))
}

However it doesn't recognize variables[i] as a variable, giving the following error message:

Error in model.frame.default(formula = Successful ~ variables[i], data = repay, : variable lengths differ (found for 'variables[i]')

eli-k
  • 10,898
  • 11
  • 40
  • 44
Sam Dunne
  • 19
  • 1

4 Answers4

1

Alternatively you can use assign yielding in as many models as the variables. Let us consider

repay<-data.table(Successful=runif(10),a=sample(10),b=sample(10),c=runif(10))
variables<-names(repay)[2:4]

yielding:

>repay
    Successful  a  b         c
 1:  0.8457686  7  9 0.2930537
 2:  0.4050198  6  6 0.5948573
 3:  0.1994583  2  8 0.4198423
 4:  0.1471735  1  5 0.5906494
 5:  0.7765083  8 10 0.7933327
 6:  0.6503692  9  4 0.4262896
 7:  0.2449512  4  1 0.7311928
 8:  0.6754966  3  3 0.4723299
 9:  0.7792951 10  7 0.9101495
10:  0.6281890  5  2 0.9215107

Then you can perform the loop

for (i in 1:length(variables)){ assign(paste0("model",i),eval(parse(text=paste("glm(Successful~",variables[i],",data=repay,family=binomial(link='logit'))")))) }

resulting in 3 objects: model1,model2 and model3.

 >model1
 Call:  glm(formula = Successful ~ a, family = binomial(link = "logit"), 
 data = repay)
 Coefficients:
 (Intercept)            a  
   -0.36770      0.05501  
 Degrees of Freedom: 9 Total (i.e. Null);  8 Residual
 Null Deviance:     5.752 
 Residual Deviance: 5.69    AIC: 17.66

Idem for model2, model3 et.c.

amonk
  • 1,769
  • 2
  • 18
  • 27
1

Try using the formula function in R. It will allow correct interpretation of models as below:

for (i in 1:length(variables){
    myglm <- glm(formula(paste("Successful", "~", variables[i])),
                 data = repay, family = binomial(link = 'logit'))

See my post here for more things you can do in this context.

akaDrHouse
  • 2,190
  • 2
  • 20
  • 29
0

You could create a language object from a string,

var = "cyl"
lm(as.formula(sprintf("mpg ~ %s", var)), data=mtcars)
# alternative (see also substitute)
lm(bquote(mpg~.(as.name(var))), data=mtcars)
baptiste
  • 75,767
  • 19
  • 198
  • 294
0

Small workaround that might help

 for (i in 22:36) 
{
  ivar <- repay[i] #choose variable for running the model
  repay2 <- data.frame(Successful= repay$Successful, ivar) #create new data frame with 2 variables only for running the model

  #run model for new data frame repay2
  model<-glm(Successful~ ivar
             ,data=repay2
             ,family=binomial(link='logit'))
}
AK47
  • 1,318
  • 4
  • 17
  • 30