Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

NAME-VISHAL DEV HOTA

REGNO-21BLC1084

DATA ANALYTICS LAB4

CODE

rm(list=ls())

data<- mtcars

library(dplyr)

data<- sample_n(data,15)

# install.packages("ggplot2")

library("ggplot2")

ggplot(data,aes(x=wt,y=mpg))+geom_point() #To plot - wt/gear

cor.test(data$wt,data$mpg) # to find the correlation value

# simple linear regression

slr = lm(mpg~wt, data)

summary(slr)

plot(slr$resid) # Residual plot

qqnorm(slr$resid) #Q-Q Plot

# Multiple linear regression

mlr = lm(mpg~wt+gear, data)


summary(mlr)

plot(mlr$resid) # Residual plot

qqnorm(mlr$resid) #Q-Q Plot

EXPLANATION

This is a code written in R programming language to perform exploratory data


analysis and regression analysis on a dataset of cars. Here is a brief explanation of
each line of the code:

1. rm(list=ls()): This line removes all objects in the current R environment.


2. data <- mtcars: This line loads the mtcars dataset into the data variable.
3. library(dplyr): This line loads the dplyr package, which provides functions
for data manipulation.
4. data <- sample_n(data, 15): This line randomly selects 15 rows from
the data dataset.
5. library("ggplot2"): This line loads the ggplot2 package, which provides
functions for data visualization.
6. ggplot(data, aes(x=wt, y=mpg)) + geom_point(): This line creates a scatter
plot of mpg versus wt variables in the data dataset.
7. cor.test(data$wt, data$mpg): This line calculates the correlation coefficient
between wt and mpg variables in the data dataset.
8. slr <- lm(mpg ~ wt, data): This line fits a simple linear regression model
of mpg on wt variable in the data dataset.
9. summary(slr): This line displays the summary statistics of the slr model.
10. plot(slr$resid): This line creates a residual plot of the slr model.
11. qqnorm(slr$resid): This line creates a Q-Q plot of the slr model.
12. mlr <- lm(mpg ~ wt + gear, data): This line fits a multiple linear regression
model of mpg on wt and gear variables in the data dataset.
13. summary(mlr): This line displays the summary statistics of the mlr model.
14. `plot(mlr$resid)

OUTPUT

You might also like