You are on page 1of 16

Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Lab Exercise 1
Q1. Take attitude data set in R and find Summary, Basic stats, histogram, Box
plot and density plot for the column Privileges.

Code:

library(fBasics)

#loading dataset

data(attitude, package="datasets")

data

#basic stats

fBasics::basicStats(data)

#summary

summary(attitude$privileges)

# histogram

hist(attitude$privileges)

#boxplot

boxplot(attitude$privileges)

#density plot

density(attitude$privileges)

plot(density(attitude$privileges))
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Output:
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Histogram ->

Boxplot ->
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Density Plot ->

Q2. Create a data frame which consist of students details (Name , Age, UG
degree, percentage of marks)

Code:

Name <- c("Rajeev", "Googol", "Himanshu", "Parul")

Age <- c(14,18,20,32)

Degree <- c("BSC", "BTech", "BCA", "MTech")

PMarks <- c(90,50,70,20)

d <- data.frame(Name, Age, Degree, PMarks)

d
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Output:

Q3. Find Scatter plot for the following data

Code:

x <- c(65,66,67,67,68,69,70,72)

y <- c(67,68,65,68,72,72,69,71)

plot(x,y,main="scatterplot", xlab="x", ylab="y", pch=15, frame=FALSE)

Output:
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Q4. A person spends his time on different activities daily (in hours): Draw a pie
chart for this information.

Code:

x <- c(9,1,2,3,7,2)

labels <- c("Office", "Exercise", "Travelling", "Shows", "Sleeping", "Misc")

pie(x, labels)

Output:

Q5. Create bar chart for (i) number of cylinders (ii) number of gears and
(iii)number of Engine(vs) in mtcars data set

Code:

mtcars

A <- table(mtcars$cyl)

B <- table(mtcars$gear)

c <- table(mtcars$vs)

barplot(A, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart of Cylinder")

barplot(B, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart of Gears")


Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

barplot(14, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart of VS")

Output:
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Lab Exercise 2
Q1. Create an excel sheet consist of marks of 30 students (0-100 range) of two
subjects and import it and find Summary, Basic stats, histogram, Box plot and
density plot for the imported data.

Code:

library(fBasics)

students <- data.frame(

student_id = 1:30,

subject1 = sample(0:100, 30, replace = TRUE),

subject2 = sample(0:100, 30, replace = TRUE)

# Write the data frame to a CSV file

write.csv(students, "students.csv", row.names = FALSE)

# Read the data from the CSV file

students_import <- read.csv("students.csv")

# Summary statistics

summary(students_import)

# Basic statistics

fBasics::basicStats(students_import$subject1)

fBasics::basicStats(students_import$subject2)
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

# Histogram

hist(students_import$subject1)

hist(students_import$subject2)

# Box plot

boxplot(students_import$subject1)

boxplot(students_import$subject2)

# Density plot

density(students_import$subject1)

plot(density(students_import$subject1))

density(students_import$subject2)

plot(density(students_import$subject2))

Output:
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Boxplot for subject1

Boxplot for subject2


Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Q2. Find the coefficient of correlation between rating and complaints columns
in attitude data set in R. Also find regression line scatter plot.

Code:

library(datasets)

# Load the attitude data set

data("attitude")

# Calculate the coefficient of correlation between rating and complaints


columns

cor(attitude$rating, attitude$complaints)

# Fit a linear regression model

model <- lm(complaints ~ rating, data = attitude)

# Generate the scatter plot and regression line

plot(attitude$rating, attitude$complaints)

abline(model, col = "red")

Output:
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

Q3. Find correlation matrix, partial correlation matrix and Multiple correlation.
Also find multiple linear regression (Assume that as dependent variable)

Code:

x1 = c(22, 15, 27, 28, 30, 42, 40)

x2 = c(12, 15, 17, 15, 42, 15, 28)

x3 = c(13, 16, 12, 18, 22, 20, 12)

data <- data.frame(x1,x2,x3)

# Find the correlation matrix

cor_matrix <- cor(data, method='pearson')

cor_matrix

# Find the partial correlation matrix

library(ppcor)

par_cor_matrix <- pcor(data, method='pearson')

par_cor_matrix

# Find the multiple correlation R1.23, R2.13, R3.12

lr1 <- lm(formula = x1~x2+x3)

summary(lr1)

lr2 <- lm(formula = x2~x1+x3)

summary(lr2)
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

lr3 <- lm(forumla = x3~x1+x2, data=data)

summary(lr3)

# Perform multiple linear regression

model <- lm(x3 ~ x1 + x2, data = data) # fit a multiple regression model with
X1 and X2 as independent variables and X3 as dependent variable

summary(model) # display the summary of the model

Output:
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P
Register Number: 21BCE5760 Name: Yash Dinesh Singh BMAT202P

You might also like