Multiple regression is extended version of linear regression.we have more than one predictor and one response variable.
Step to follow:
Step 1:multiple regression follows given equation,
Z <- a+b1x1+b1x2+…+bnxn
Where,
Z :is the Response Variable.
A,b1,b2..bn:are Coefficient.
X1,x2,…xn:are Pridictor Variable.
Step 2:lm() function
lm() function find out the relation between two variable (i.e linear regression) or more than two variable (i.e multiple regression).
lm(Y~x1+x2+x3..,data)
Example: we are using dataset avilable in R environment (i.e:mtcars).how dataset can access is given below:
> data("mtcars")
> print.data.frame(mtcars)
shows mtcars dataset,
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
when we want to display only 6 columns in the dataset, then
w <- head(mtcars,6)
print(w)
it will display 6 columns from mtcars,
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160.0 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875
Datsun 710 22.8 4 108.0 93 3.85 2.320
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440
Valiant 18.1 6 225.0 105 2.76 3.460
Duster 360 14.3 8 360.0 245 3.21 3.570
Merc 240D 24.4 4 146.7 62 3.69 3.190
Using vectors also we can derive dataset in r environment,
input<-mtcars[,c("mpg","disp","hp","wt","cyl")]
print(input)
following result show dataset of mtcars that contain those content which are passing to vectcor:
mpg disp hp wt cyl
Mazda RX4 21.0 160.0 110 2.620 6
Mazda RX4 Wag 21.0 160.0 110 2.875 6
Datsun 710 22.8 108.0 93 2.320 4
Hornet 4 Drive 21.4 258.0 110 3.215 6
Hornet Sportabout 18.7 360.0 175 3.440 8
Valiant 18.1 225.0 105 3.460 6
Duster 360 14.3 360.0 245 3.570 8
Merc 240D 24.4 146.7 62 3.190 4
Then we find relation among those variable using lm() function
model<-lm(mpg~disp+hp+wt+cyl,data = input)
print(model)
than it will show following result,
Call:
lm(formula = mpg ~ disp + hp + wt + cyl, data = input)
Coefficients:
(Intercept) disp hp wt cyl
40.82854 0.01160 -0.02054 -3.85390 -1.29332
Finally we find milege from disp,hp,wt and cyl using following formula,
Y = a+disp*x1+hp*x2+wt*x3+cyl*x4
or
z=40.82854+(0.01160)*160+(-0.02054)*110+(-3.85390)*2.620+(-1.29332)*6
print(z)
output:
22.568