Creating Multipal regression model on mtcars
Create a model on Dataset - mtcars
data(mtcars) # Calling a data from data set from library called mtcars
View(mtcars) # To view the data
str(mtcars) # To see the structure of the data
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
summary(mtcars) # We get a basis statistical calculation like min, max, mean, median etc for the data
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
miles_per_gallon <- mtcars$mpg # calling mpg column from mtcars using filter symbol $ as a vector variable name as miles_per_gallon
# or we can use - miles_per_gallon <- mtcars[,1]
# or we can use - miles_per_gallon <- c(mtcars[,1])
# or we can use - miles_per_gallon <- list(c(mtcars[,1]))
cylinder <- mtcars$cyl # calling cyl column from mtcars using filter as symbol $ as a vector variable name as cylinder
Let us create a model - using simple linear regression (lm)
model1 <- lm(miles_per_gallon ~ cylinder)
# lm is used for regression , first we write dependent variable , ~ symbol is used to build relationship , then dependent variable
# In model1 we do a simple regression by using miles per gallon and cylinder. In tells the cause and effect in each change of independent variable how much does the dependent variable changes
model1 # Call the variable to see the output
##
## Call:
## lm(formula = miles_per_gallon ~ cylinder)
##
## Coefficients:
## (Intercept) cylinder
## 37.885 -2.876
# Analysis of model1- Even the simple use if a car runs hypothetically on cylinders it would give 37.885 miles per gallon it uses with each cylinder move that the car has, its performance will decrease 2.876 miles per gallon
Let us create a predictive model on model1
Criteria : A car having 6 cylinders
Criteria : A car having 8 cylinders
Let us create some visuals
plot <- plot(cylinder, miles_per_gallon, type= "p", main = "Gas consumption as explained by number of cylinders", sub="General information", xlab = "Number of cylinders", ylab = "Number Miles (US) per gallon", col = "firebrick")
abline(model1)

# plot will create a graphically representation of data. main makes the heading of the graph, sub is the sub heading of the graph, xlab is the name of the x axis and ylab is the name of the y axis
Create a model using multipal Regression model
Variables to be used : mpg, cyl, am
Create model1 using above 3 variables
Transmission <- mtcars$am
# calling am column from mtcars dataset in the variable name Transmission
model2 <- lm(miles_per_gallon ~ cylinder + Transmission)
#We are creating multipal regression model by using miles per gallon as a dependent variable, cylinder and transmission as independent variables
model2 # Call the variable to see the output
##
## Call:
## lm(formula = miles_per_gallon ~ cylinder + Transmission)
##
## Coefficients:
## (Intercept) cylinder Transmission
## 34.522 -2.501 2.567
Interpretation for the above Multiple Regression
- In this model, along with the number or cylinders, we include the
variable am (0 if the car has the automatic transmission, 1 if it is
manual transmission)
- If the car is automatic and the number of cylinder is zero the
hypothetical mileage of it would be 34.522 miles per gallon
- If every thing else remains constant per each cylinder more that the
car has, it will decrease by 2.501 miles per gallon
- If every thing else remains constant and car is manual its mileage
will increase by 2.567 miles per gallon