0% found this document useful (0 votes)
43 views5 pages

Shivam Batra (19BPS1131) 21/01/2022: List

The document performs simple linear regression and multiple linear regression on mtcars dataset in R. It loads required libraries, samples data, plots correlation between variables, performs correlation test, fits simple linear regression model between mpg and wt, analyzes residuals and fits multiple linear regression model between mpg, wt and gear variables. It analyzes residuals for multiple regression model as well.

Uploaded by

Shivam Batra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Shivam Batra (19BPS1131) 21/01/2022: List

The document performs simple linear regression and multiple linear regression on mtcars dataset in R. It loads required libraries, samples data, plots correlation between variables, performs correlation test, fits simple linear regression model between mpg and wt, analyzes residuals and fits multiple linear regression model between mpg, wt and gear variables. It analyzes residuals for multiple regression model as well.

Uploaded by

Shivam Batra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab-1

Shivam Batra(19BPS1131)

21/01/2022
rm(list=ls()) #To clear the environment

data <- mtcars #mtcars is a default dataset

library(dplyr) #dplyr is a library, which has functions related to

##
## Attaching package: 'dplyr'

## The following objects are masked from 'package:stats':


##
## filter, lag

## The following objects are masked from 'package:base':


##
## intersect, setdiff, setequal, union

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

##
## Pearson's product-moment correlation
##
## data: data$wt and data$mpg
## t = -6.5009, df = 13, p-value = 2.001e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.9577274 -0.6562026
## sample estimates:
## cor
## -0.8745029

# simple linear regression

slr = lm(mpg~wt, data)

summary(slr)

##
## Call:
## lm(formula = mpg ~ wt, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.1466 -1.2260 -0.2581 1.6960 4.4584
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 32.4919 2.2672 14.331 2.42e-09 ***
## wt -3.9343 0.6052 -6.501 2.00e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.37 on 13 degrees of freedom
## Multiple R-squared: 0.7648, Adjusted R-squared: 0.7467
## F-statistic: 42.26 on 1 and 13 DF, p-value: 2.001e-05

plot(slr$resid) # Residual plot

qqnorm(slr$resid) #Q-Q Plot


# Multiple linear regression

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

summary(mlr)

##
## Call:
## lm(formula = mpg ~ wt + gear, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.4103 -1.4951 0.0219 1.4783 3.6583
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 24.1168 6.0198 4.006 0.001742 **
## wt -3.2910 0.7217 -4.560 0.000654 ***
## gear 1.7808 1.1941 1.491 0.161702
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.266 on 12 degrees of freedom
## Multiple R-squared: 0.8015, Adjusted R-squared: 0.7685
## F-statistic: 24.23 on 2 and 12 DF, p-value: 6.111e-05

plot(mlr$resid) # Residual plot


qqnorm(mlr$resid) #Q-Q Plot

You might also like