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:
Any R function calls to get the names (not the coefficient values, I know how to get them) of the predictor variables?
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.