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.
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.
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.
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.
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.
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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With