## Multiple Regression with R##
#One should have the basic concept in statistics and R to understand this and the different terms associated with
this work sheet.
#Please read my Simple Linear Regression With R (http://www.slideshare.net/jeromerick/simple-linear-regression-with-
r-52336944) to get a better understanding of this lesson.
# We will use R's in-built data - USJudgeRatings
data("USJudgeRatings") # loading the data into R
?USJudgeRatings
reg1<- lm(RTEN ~ CONT+INTG+DILG+DECI+FAMI, data = USJudgeRatings) #regression equation for predicting worthy of
retaining the judges using the variables CONT+INTG+DILG+DECI+FAMI
reg1
help(lm)
summary(reg1)
cor(USJudgeRatings$CONT,USJudgeRatings$INTG, method = "pearson") # calculating the correlation between two variables
using Pearson’s correlation method.
plot(reg1) # checking the regression diagnostic plots for this model
anova(reg1) # ANOVA
coef(reg1) #coefficients of the regression equation
confint(reg1, level = .99) # confidence interval of the coefficients
resid(reg1) # Residuals of case by case basis
hist(residuals(reg1)) # Histogram of the residuals
plot(residuals(reg1)) # Plotting the residuals
# Kindly install these packages and go through them as they provide better estimators for regression model
install.packages("selectiveInference")
install.packages("rms")
require(selectiveInference)
require(rms)
?selectiveInference
#Stepwise Regression - Please go through the concept of backward and forward regression selection model
#1. Backward stepwise regression -
reg2<-lm(RTEN ~ CONT+INTG+DMNR+DILG+CFMG+DECI+PREP+FAMI+ORAL+WRIT+PHYS, data = USJudgeRatings) # We start Backward
stepwise regression with all the variables and then eliminating each
summary(reg2)
regb<-step(reg2, direction = "backward", trace = 0) # Trace = 0 means we dont want to print all the steps
summary(regb)
#2. Forward stepwise regression -
reg0<-lm(RTEN ~ 1, data = USJudgeRatings) # We start Forward stepwise regression with the minimal model.
regf<-step(reg0, direction = "forward",scope = (~CONT+INTG+DMNR+DILG+CFMG+DECI+PREP+FAMI+ORAL+WRIT+PHYS), data =
USJudgeRatings, trace = 0) # Trace = 0 means we dont want to print all the steps
summary(regf)
## By Jerome Gomes ##
## For queries and more information feel free to contact me @ jeromegomes89@gmail.com ##
## If you want this R-Script then mail me at the above mail id ##

Multiple regression with R

  • 1.
    ## Multiple Regressionwith R## #One should have the basic concept in statistics and R to understand this and the different terms associated with this work sheet. #Please read my Simple Linear Regression With R (http://www.slideshare.net/jeromerick/simple-linear-regression-with- r-52336944) to get a better understanding of this lesson. # We will use R's in-built data - USJudgeRatings data("USJudgeRatings") # loading the data into R ?USJudgeRatings reg1<- lm(RTEN ~ CONT+INTG+DILG+DECI+FAMI, data = USJudgeRatings) #regression equation for predicting worthy of retaining the judges using the variables CONT+INTG+DILG+DECI+FAMI reg1 help(lm) summary(reg1) cor(USJudgeRatings$CONT,USJudgeRatings$INTG, method = "pearson") # calculating the correlation between two variables using Pearson’s correlation method. plot(reg1) # checking the regression diagnostic plots for this model anova(reg1) # ANOVA coef(reg1) #coefficients of the regression equation confint(reg1, level = .99) # confidence interval of the coefficients resid(reg1) # Residuals of case by case basis hist(residuals(reg1)) # Histogram of the residuals plot(residuals(reg1)) # Plotting the residuals # Kindly install these packages and go through them as they provide better estimators for regression model install.packages("selectiveInference") install.packages("rms") require(selectiveInference) require(rms) ?selectiveInference #Stepwise Regression - Please go through the concept of backward and forward regression selection model #1. Backward stepwise regression - reg2<-lm(RTEN ~ CONT+INTG+DMNR+DILG+CFMG+DECI+PREP+FAMI+ORAL+WRIT+PHYS, data = USJudgeRatings) # We start Backward stepwise regression with all the variables and then eliminating each summary(reg2) regb<-step(reg2, direction = "backward", trace = 0) # Trace = 0 means we dont want to print all the steps summary(regb) #2. Forward stepwise regression - reg0<-lm(RTEN ~ 1, data = USJudgeRatings) # We start Forward stepwise regression with the minimal model. regf<-step(reg0, direction = "forward",scope = (~CONT+INTG+DMNR+DILG+CFMG+DECI+PREP+FAMI+ORAL+WRIT+PHYS), data = USJudgeRatings, trace = 0) # Trace = 0 means we dont want to print all the steps summary(regf) ## By Jerome Gomes ## ## For queries and more information feel free to contact me @ jeromegomes89@gmail.com ## ## If you want this R-Script then mail me at the above mail id ##