Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Session 8- Multiple Regression

Import the file- reg2.csv, Information of the variables

id X13 - Competitive Pricing


X1 - Customer Type (Categorical variable) X14 - Warranty & Claims
X2 - Industry Type(Categorical variable) X15 - New Products
X3 - Firm Size(Categorical variable) X16 - Order & Billing
X4 - Region(Categorical variable) X17 - Price Flexibility
X5 - Distribution System(Categorical X18 - Delivery Speed
variable) X19 - Satisfaction
X6 - Product Quality X20 - Likely to Recommend
X7 - E-Commerce Activities X21 - Likely to Purchase
X8 - Technical Support X22 - Purchase Level
X9 - Complaint Resolution X23 - Consider Strategic Alliance
X10 - Advertising
X11 - Product Line
X12 - Salesforce Image

Import the file as R object


reg1<-read.csv("C:/Users/Administrator.vaio1/Desktop/reg2.csv")

#check/ explore the file


str(reg1)
head(reg1)

# Descriptive statistics of all the variables

library(psych)
describe(reg1)

library(pastecs)
stat.desc(reg1)

# here we will use x19 satisfaction as dependent variable


# and X7 - E-Commerce Activities,X8 - Technical Support,
#X9 - Complaint,Resolution,X10 - Advertising,X11 - Product Line,
#X12 - Salesforce Image,X13 - Competitive Pricing,X14 - Warranty & Claims,
#X15 - New Products,X16 Order & Billing,
#X17 - Price Flexibility,and X18 - Delivery Speed

# Remove all the missing observation


reg1train<-na.omit(reg1train)

#assumptions of MLRegression
#1. Linear relationship
# plot to check the linearity- bivaraite plot

library(ggplot2)
ggplot(reg1train, aes(reg1train$X19,reg1train$X7))+geom_smooth()

http://learnrmanoharkapse.blogspot.com
#plot a linear relation between the variables and try to find weather it is linear or not
#y~X
regx19_x7<-lm(reg1train$X19~reg1train$X7)
summary(regx19_x7)
# check the coefficient of b1 only, correlation

# use correlation
library(stats)
cor(reg1train$X7, reg1train$X19)
cor.test(reg1train$X7, reg1train$X19)

#-------- normality of the variables--------------#

library(fBasics)
library(moments)

qqnorm(X19)
qqline(X19)
boxplot(X19)
basicStats(X19)
skewness(X19)
kurtosis(X19)
shapiro.test(X19)

fit1<-lm(X19~X7+X8+X9+X10+X11+X12+X13+X14+X15+X16+X17+X18, data = reg1train)


summary(fit1)
attributes(fit1)

fit1$coefficients
fit1$residuals

# to check residual analysis, heteroscadity, etc we use plots


plot(fit1)

#-----------Multicollinearity-------------------------#
library(car)
vif(fit1) #variance inflation factors Multicollinearity

#autocorrelation
library(lmtest)
dwtest(fit1)

#test for heteroscadisticity


plot(fit1$residuals, fit1$fitted.values)
#from the plot it is not easy to analyze the pattern of the data so we will apply bptest
bptest(fit1)

# --------------------for stepwise forward backward Method------------#

library(MASS)

regstep<-stepAIC(fit1)

summary(reg1)

regboth<-stepAIC(fit1,direction = "both")

summary(regboth)

http://learnrmanoharkapse.blogspot.com
regback<-stepAIC(fit1,direction = "backward")

summary(regback)

regfor<-stepAIC(fit1,direction = "forward")

summary(regfor)

regall<-stepAIC(fit1,direction = c("both", "backward", "forward"))

summary(regall)

Best Method

library(leaps)
fit2<-regsubsets(X19~X7+X8+X9+X10+X11+X12+X13+X14+X15+X16+X17+X18, data=reg1train,
nbest = 3)
fit2
summary(fit2)

plot(fit2, scale ="r2")


plot(fit2, scale ="adjr2")
plot(fit2, scale ="bic")

http://learnrmanoharkapse.blogspot.com

You might also like