Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting predictor names in R regression

Tags:

r

regression

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.

like image 441
Baykal Avatar asked May 23 '13 21:05

Baykal


People also ask

How do I find the best predictor variable in R?

Generally variable with highest correlation is a good predictor. You can also compare coefficients to select the best predictor (Make sure you have normalized the data before you perform regression and you take absolute value of coefficients) You can also look change in R-squared value.

How do you find the predictor variable?

In secondary education settings, the equation is often expressed as y = mx + b. Where y represents the predicted variable, m refers to the slope of the line, x represents the predictor variable, and b is the point at which the regression line intercepts with the Y axis.

What does LM () do in R?

The lm() function is used to fit linear models to data frames in the R Language. It can be used to carry out regression, single stratum analysis of variance, and analysis of covariance to predict the value corresponding to data that is not in the data frame.

How do you interpret predictor variables?

Interpreting the Coefficient of a Continuous Predictor Variable. For a continuous predictor variable, the regression coefficient represents the difference in the predicted value of the response variable for each one-unit change in the predictor variable, assuming all other predictor variables are held constant.


2 Answers

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"
like image 177
Simon O'Hanlon Avatar answered Nov 06 '22 22:11

Simon O'Hanlon


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.

like image 20
Tommy Levi Avatar answered Nov 07 '22 00:11

Tommy Levi