Procedure For Boxplot, Correlation and Regression in R
Procedure For Boxplot, Correlation and Regression in R
Aim: To create boxplot chart using mpg and cyl fields in mtcars.
Procedure:
1.Men on the path and file name for saving chart.
png("E:/boxplot.png")
2. type code for crea ng boxplot.
boxplot(mpg~cyl,data=mtcars,main="Mileage of cars based on number of cylinders",xlab="No of
cylinders",ylab="mileage per gallon",col=c("red","blue","green"))
3. Type dev.off() to save
Aim: to ascertain the correla on between mileage and other variables in data frame named mtcars
Mileage=mtcars$mpg
Cylinders=mtcars$cyl
cor(Mileage,Cylinders)
Result:-
-0.852162
There is a nega ve correla on between number of cylinders used and mileage
Proedure:
Mileage=mtcars$mpg
Weight=mtcars$wt
cor(Mileage,Weight)
Result:
-0.8676594
There is a nega ve correla on between number of cylinders used and weight of cars
Result:
0.4802848
There is a moderate posi ve correla on between number of gears and mileage.
Problem :- Formulate regression equa on between mileage and weight using mtcars data frame and predict
mileage for given weight 3.5
Aim: To formulate regression equa on by trea ng mileage as dependent variable and weight as independent
variable
Procedure:
1. Find coefficients for regression equa on trea ng mileage as dependent variable and mtcars as
independent variable.
M=mtcars$mpg
W=mtcars$wt
L=lm(M~W)
2. Create a data frame with given weight to predict mileage.
W1=data.frame(W=3.5)
3. Predict Mileage using L and W1 using predict()
Predict(variable stored with results of lm func on, data frame with the given value of independent
variable)
predict(L,W1)
Results:
1. Coefficients:
Es mate Std. Error t value Pr(>|t|)
(Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
W -5.3445 0.5591 -9.559 1.29e-10 ***