9

I have an R code that is running about 100 regression models, grabbing R^2 values and printing them to a csv file as follows:

filename<-"Reg_Results.csv";
cat("Setting,Origin Region,Destination Region,R^2\n",file=filename,append=FALSE);   
for(setting in seq(from=1,to=3,by=1)) {
    for(i in seq(from=1,to=7,by=1)) {
        for(j in seq(from=1,to=7,by=1)) {
            RRSub<-subset(RR,ORegion==ORegions[i]&DRegion==DRegions[j]);                
            if(nrow(RRSub)>1){                  
                if(setting==1)                  
                    RRSub.LR <- lm(formula=Rev.per.Unit~RRs+Own+Miles+Category+STCC2.Description,data=RRSub); 
                if(setting==2)                  
                    RRSub.LR <- lm(formula=Rev.per.Unit~RRs+Own+Miles+Category+STCC5.Description,data=RRSub); 
                if(setting==3)                  
                    RRSub.LR <- lm(formula=Rev.per.Unit~RRs+Own+Miles+Category+STCC5.Description+OCity+DCity,data=RRSub); 
                cat(setting,file=filename,append=TRUE); 
                cat(",",file=filename,append=TRUE);                         
                cat(ORegions[i],file=filename,append=TRUE); 
                cat(",",file=filename,append=TRUE);     
                cat(DRegions[j],file=filename,append=TRUE); 
                cat(",",file=filename,append=TRUE);         
                cat(summary(RRSub.LR)$r.squared,file=filename,append=TRUE);                                 
                cat("\n",file=filename,append=TRUE);
            }
        }
    }
}

My goal is to also print the names of the predictor variables (because they will be different in each regression model due to the qualitative predictors), and their coefficients in the same .csv file.

My questions are:

  1. Any R function calls to get the names (not the coefficient values, I know how to get them) of the predictor variables?

  2. Any way to get how many predictor variables are used in the model? I will use this value to write a for loop to print the predictor names.

Baykal
  • 569
  • 2
  • 10
  • 15

3 Answers3

13

You can extract the predictor terms like this:

#  Dummy model with made-up data
mod <- lm( y ~ x + z , data = df )
#  Return character vector with predictor terms
attr(mod$terms , "term.labels")
# [1] "x" "z"

Which also works for more complicated models

mod <- lm( y ~ x + z + I(x^2) + x:z , data = df )
attr(mod$terms , "term.labels")
# [1] "x"      "z"      "I(x^2)" "x:z"
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • 1
    This should be the accepted answer. It excludes the intercept and gets the parent categorical variable of dummy predictors. – John F Nov 11 '20 at 11:11
5

You just need to use names, i.e.

names(RRSub.LR$coefficients)

and

length(names(RRSub.LR$coefficients))

Note this will include the intercept term (if you had any), but it's easy enough to drop that if you like.

Tommy Levi
  • 771
  • 5
  • 12
0

If you want it as a formula string, you can do this:

alias(mod)$Model
Y ~ Variable1 + Variable2 + Variable3 + ...
igorkf
  • 3,159
  • 2
  • 22
  • 31