# Session I
# Installing packages and Libraries
# Introduction:-Help,Demonstration,Examples,Packages & Libraries
help("lm")
help.start()
help.search("data input")
? read.table()
find("lm")
find("mean")
example("mean")
install.packages("moments")
library(moments)
detach("package:moments",unload = TRUE)
library(MASS)
library(help=moments)
# R - Data Types
# vector
apple=c('red','green',"yellow")
apple
list=list(c(1,2,3),21.3,'sin')
list
# Matrices
M1=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
M1
M2=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3, ncol=3,byrow=T)
M2
# Arrays
A1=array(c('red','green','yellow'),dim=c(3,2,4))
A1
# Factors
apple_colours=c('red','green','red','yellow','green','red','red','green')
apple_colours
factor_apple=factor(apple_colours)
factor_apple
nlevels(factor_apple)
table(factor_apple)
# Data Frames
data=data.frame(
gender=c('M','F','F'),
weight=c(78.5,82,64),
height=c(165.3,172,180),
age=c(23,56,71)
)
data
# Operations on vectors, logical vector, index vector
x=c(3,4,5,6)
x
x+5
typeof(x+5)
x=c(3, 4, 5, 6)
x
y=c(-3,-4, -5, 7)
y
x+y
x-y
x=c(3,4,5,6)
x
y=c(7,8,9)
y
x+y
x-y
x=c(3,4,5,6)
x
y=c(7,8)
y
x+y
x-y
x=c(4,5,6,8)
x
y=x*3
y
x=c(3,4,5,6)
x
y=c(-3,-4,-5,7)
y
x*y
x=c(3,4,5,6)
x
y=c(6,7)
y
x*y
x=c(3,4,5,6)
x
y=c(7,8,9)
y
x*y
x=c(2,4,6,8)
x
y=x/2
y
x=c(4,8,12,16)
x
y=c(-2,-4,-3,8)
y
x/y
x=c(4,8,12,16)
x
y=c(2,4,3)
y
x/y
y/x
x=c(4,5,6,7)^2
x
#x=c(4,5,6,7)
#x^2
x=c(4,5,6,7)^c(2,3)
x
x=c(3,4,5,6,7)^c(2,3,4)
x
# Logical vector
v1=c(TRUE,FALSE,TRUE,NA)
v1
typeof(v1)
#Character Vector
v2=c('geeks','2','hello',57)
v2
typeof(v2)
#Complex Vector
v3=c(1+2i,3i,4-5i,-12+6i)
v3
typeof(v3)
# index vector
# If we want a single element, or a set of specific elements,we can do this by
# indexing (uses the [] brackets).
x=c(4, 17, -1, 55, 2)
x
x[3]
length(x)
x[length(x)]
x[1:4]
x[c(1,3,5)]
x[c(1,3,5)]=0
x
x[10]=2
x
# Descriptive Statistics using R
x=c(2,3,4,5)
x
z=sum(x^2)
z
x=c(2,3,4,5)
x
length(x)
z=sum(x^2)-length(x)*mean(x)^2
z
# Missing data in a vector
x=c(10,20,30,40)
x
is.na(x)
x=c(10,NA,30,40)
x
is.na(x)
result=c(1,2,1,2,2,1,1,2,1,1)
result
table(result) # Absolute Frequencies
table(result)/length(result) # Relative Frequencies
# Constructing frequency table for a data set
weight=c(32,35,45,83,74,55,68,38,35,55,66,65,42,68,72,84,67,36,42,58)
weight
range(weight)
# Creating a sequence starting from 30 to 90 at an interval of 10 integers
# denoting the width
seq=seq(30,90,by=10)
seq
weight_cut=cut(weight,seq,right=F)
weight_cut
table(weight_cut) # Absolute Frequencies
cbind(table(weight_cut))
table(weight_cut)/length(weight_cut) # Relative Frequencies
cbind(table(weight_cut)/length(weight_cut))
barplot(table(weight_cut),xlab="time",ylab="Frequency")
h=hist(weight,breaks=5,col="red")
# Kernel density plots
d=density(weight)
plot(d,main="Kernel Density of Weight of oranges")
polygon(d,col="red",border="blue")
#install and load vioplot package
install.packages("vioplot")
library(vioplot)
vioplot(weight,col="gold",main="Weight of oranges")
# Fan Plot
# install and load package plotrix
install.packages("plotrix")
library(plotrix)
age=c(10,12,14,16,10)
lbls= c("B1","B2","B3","B4","B5")
fan.plot(age,labels=lbls,main="Age of 5 Boys")
SP=c(1,1,2,1,2,3,2,2,3,3,3,1,2,3,2,2,3,1,1,3,3,1,2,1,3,3,3,2,2,2,2,1,2,2,1,1,2,1,2,3,1,1,2,2,2,1,1,3,3,1)
SP
length(SP)
table(SP)
pie3D(table(SP))
# Adding Labels and Colors
pie3D(table(SP),labels=c("SP1","SP2","SP3"),
main="Participants attending Parallel Sessions",col=c("green","red","blue"))
# Using explode option
pie3D(table(SP),explode=0.3,labels=c("SP1","SP2","SP3"),
main="Participants attending Parallel Sessions",col=c("green","red","blue"))
# Bar chart, Box plot
A=c(17, 32, 8, 53, 10)
A
barplot(A,xlab="X-axis",ylab="Y-axis",col="red",main="Bar-Chart")
barplot(A,horiz=T,xlab="X-axis",ylab="Y-axis",col ="green",main="Horizontal Bar Chart")
A=c(17, 2, 8, 13, 1, 22)
B=c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
barplot(A, names.arg = B, xlab = "Month",
ylab = "Articles", col = "steelblue",
main = "Article Chart",
cex.main = 1.5, cex.lab = 1.2, cex.axis = 1.1)
text(x=barplot(A, names.arg = B,col="steelblue",ylim=c(0,max(A)*1.5)),
y=A+1,labels=A,pos=3,cex=1.2,col="black")
# Grouped Bar Chart
colors=c("green", "blue", "red")
months=c("Jan", "Feb", "Mar", "Apr", "May")
regions=c("North_Madurai","South-Madurai","West_Madurai")
revenue=matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),nrow=3,ncol=5,byrow=TRUE)
barplot(revenue,main="Total Revenue",names.arg=months,
xlab="Month",ylab="Revenue",
col=colors,beside=T)
legend("topleft", regions, cex = 0.7, fill = colors)
# Stacked Bar Chart
colors=c("green","blue","red")
months=c("Jan","Feb","Mar","Apr","May")
regions=c("North_Madurai","South_Madurai","West_Madurai")
revenue=matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),nrow=3,ncol=5,byrow=T)
barplot(revenue,main="Total Revenue",names.arg=months,
xlab="Month",ylab="Revenue",
col=colors)
legend("topleft",regions,cex=0.7,fill=colors)
# Box plots
x=c(2,5,7,8,2,4,5,8,9,10,14,19,20,23,45,65,23,12,60)
#boxplot(x)
boxplot(x,horizontal = T)
#####################################################################################
###Session # 2 Code
# Session II
# Data Analysis Using R Programming
df1=read.csv("Payment_Details.csv")
df1
df2=read_excel("Payment_Details.xlsx")
df2
install.packages("tidyverse")
library(tidyverse)
glimpse(df1)
df1$MonthlyCharges
is.na(df1$MonthlyCharges)
df3=df1 %>% mutate(MonthlyCharges
= replace(MonthlyCharges,
is.na(MonthlyCharges),
median(MonthlyCharges, na.rm = TRUE)))
df3
df3$TotalCharges
is.na(df3$TotalCharges)
df4= df3 %>%
mutate(TotalCharges = replace(TotalCharges, TotalCharges == "na", NA)) %>%
mutate(TotalCharges = replace(TotalCharges, TotalCharges == "N/A", NA))
df4
df4$TotalCharges=as.character(df4$TotalCharges)
df4$TotalCharges
df4$TotalCharges=as.numeric(df4$TotalCharges)
df4$TotalCharges
df4
glimpse(df4)
df5=df4 %>% mutate(TotalCharges = replace(TotalCharges,
is.na(TotalCharges),
median(TotalCharges, na.rm = T)))
df5
df6= df5 %>% mutate(PaymentMethod =
replace(PaymentMethod, PaymentMethod =="",'None'))
df6
summary(df6)
# Performing Descriptive Statistics for an Imported Data set Using R
mydata=read.csv("Health_data.csv")
head(mydata)
summary(mydata)
mydata[2,5]
mydata["4","WEIGHT"]
nrow(mydata)
ncol(mydata)
mydata[2,]
mydata[,5]
boxplot(mydata$WEIGHT,horizontal=T)
mydata$WEIGHT
weight_outlier=boxplot(mydata$WEIGHT)$out
weight_outlier
library(moments)
all.moments(mydata$WEIGHT, order.max =4, central = T)
mydata$GENDER=factor(mydata$GENDER,labels=c(0,1))
head(mydata)
maledata=subset(mydata,mydata$GENDER==1)
head(maledata)
femaledata=subset(mydata,mydata$GENDER==0)
head(femaledata)
quantile(mydata$AGE)
quantile(mydata$AGE,0.25)
quantile(mydata$AGE,0.5)
quantile(mydata$AGE,0.75)
table1=table(mydata$GENDER)
table1
table2=table(mydata$GENDER,mydata$Exercise)
table2
chisq.test(table2)
cor(mydata$HEIGHT,mydata$WEIGHT)
plot(mydata$HEIGHT,mydata$WEIGHT)
reg_model=lm(mydata$WEIGHT~mydata$HEIGHT,data=mydata)
reg_model
summary(reg_model)
abline(lm(mydata$WEIGHT~mydata$HEIGHT,data=mydata))
MultipleRM=lm(mydata$BMI~mydata$HEIGHT+mydata$WEIGHT,data=mydata)
MultipleRM
summary(MultipleRM)
# Linear Algebra using R
# include package pracma
install.packages("pracma")
library(pracma)
install.packages("matlib")
library(matlib)
A=matrix(c(1,2,-1,0,0,1,3,1,2,5,1,2),nrow=3,ncol=4,byrow=T)
A
Rank(A)
#Linear Dependence and Independence of Vectors
#Consider the vectors (1,2,-1,0),(1,3,1,2),(4,2,1,0) and (6,1,0,1)
A=matrix(c(1,2,-1,0,1,3,1,2,4,2,1,0,6,1,0,1),nrow=4,ncol=4)
A
det(A)
ifelse(det(A)==0,"The vectors are linearly dependent","The vectors are linearly independent")
# Consistency and Inconsistency of a system of Linear Non-homogenous Equations
# Test for the consistency of x-y+2z=2,2x+y+4z=7,4x-y+z=4 and
# find the solution if it exist
A=matrix(c(1,-1,2,2,1,4,4,-1,1),nrow=3,ncol=3,byrow=T)
A
b=matrix(c(2,7,4))
b
showEqn(A, b)
c(R(A), R(cbind(A,b)))
ifelse(R(A)==R(cbind(A,b)),"The system is consistent","The system is inconsistent")
Solve(A,b,fractions=T)
plotEqn3d(A,b)
# Test for the consistency of the system of equations x+2y-3z=3,2x+y+4z=1,5x+4y+5z=2
# and find the solution if it exists
A=matrix(c(1,2,-3,2,1,4,5,4,5),nrow=3,ncol=3,byrow=T)
A
b=matrix(c(3,1,2))
b
showEqn(A, b)
c(R(A), R(cbind(A,b)))
ifelse(R(A)==R(cbind(A,b)),"The system is consistent","The system is inconsistent")
Solve(A,b,fractions=T)
plotEqn3d(A,b)
# Eigen Values and Eigen Vectors
A=matrix(c(3,-4,4,1,-2,4,1,-1,3),nrow=3,ncol=3,byrow=T)
A
eigen(A)
inv(A)
A %*% inv(A)
# Nullspace
A=matrix(c(3,-1,7,3,9,
-2,2,-2,7,5,
-5,9,3,3,4,
-2,6,6,3,7),nrow=4,ncol=5,byrow=T)
A
b=matrix(c(0,0,0,0))
b
rref(A)
showEqn((rref(A)),b)
################################################################################
##Session 3
# Session III(03-12-2024)
# Data Analytics Using R Programming
# Simple linear regression: model fitting and model interpretation
cars
summary(cars)
# scatter plot
plot(cars$speed,cars$dist,ylab = "Car Stopping Distance (ft)", xlab = "Speed (mph)", main = "Car Speed vs. Stopping Distance")
SLR=lm(cars$dist~cars$speed)
SLR
abline(SLR,col="red")
# Normal Assumption of Error Term
residual=SLR$residuals
residual
hist(residual,col="blue")
# Homoscedasticity Assumption
plot(SLR$residuals ~ cars$speed)
abline(0,0)
# R-squared and the Goodness-of-Fit
summary(SLR)
# Hypothesis Testing
anova(SLR)
# Table Value
Ft=qf(0.95,df1=1,df2=48)
Ft
# P-Value Approach
pv=1-pf(89.567,1,48)
pv
# Confidence and Prediction Intervals
confint(SLR,level=.95)
#Prediction and confidence intervals for the stopping distance when the speed is 15 mph
newdata = data.frame(speed = 15)
newdata
conf = predict(SLR, newdata, interval = "confidence")
conf
pred = predict(SLR, newdata, interval="predict")
pred
#####################################################################################
# Residual analysis
# Install and Load UsingR package
# library(UsingR)
data(diamond)
mydata=diamond
mydata
plot(mydata$carat,mydata$price,ylab = "Price", xlab = "Carat")
fit=lm(mydata$price ~ mydata$carat)
fit
abline(fit,col="red")
# Install and Load broom package
install.packages("broom")
library(broom)
model.diag.metrics=augment(fit)
head(model.diag.metrics)
ggplot(model.diag.metrics, aes(mydata$carat,mydata$price)) +
geom_point() +
stat_smooth(method = lm, se = F) +
geom_segment(aes(xend = mydata$carat, yend = .fitted),
color = "red",size = 0.5)
# The diagnostic plots
plot(fit,1)
plot(fit,2)
plot(fit,3)
plot(fit,4)
plot(fit,5)
# Nonlinear Regression
# Polynomial Regression
data=read.csv("TensileStrength.csv")
head(data)
plot(data$HWC,data$TS,xlab="Hardwood_Con",ylab="TensileStrength",col="red",pch=1,cex=2)
# Polynomial Regression method 1
# Fitted Quadratic model TS=a+b*HWC+c*HWC^2
model1=lm(TS~HWC+I(HWC^2),data=data)
summary(model1)
# Plotting the fitted curve
HWC_VAL=seq(0,20,0.01)
head(HWC_VAL)
TS_VAL=predict(model1,data.frame(HWC=HWC_VAL),type="response")
head(TS_VAL)
# Drawing the curve
lines(HWC_VAL,TS_VAL,col="blue",lwd=2)
# Improving the model
model1=lm(TS~HWC+I(HWC^2)+I(HWC^3),data=data)
summary(model1)
# Plotting the fitted curve
HWC_VAL=seq(0,20,0.01)
head(HWC_VAL)
TS_VAL=predict(model1,data.frame(HWC=HWC_VAL),type="response")
head(TS_VAL)
# Drawing the curve
lines(HWC_VAL,TS_VAL,col="red",lwd=2)
#####################################################################################
# Polynomial Regression method 2
model2=lm(TS~poly(HWC,2,raw=T),data=data)
summary(model2)
# Plotting the fitted curve
HWC_VAL=seq(0,20,0.01)
head(HWC_VAL)
TS_VAL=predict(model2,data.frame(HWC=HWC_VAL),type="response")
head(TS_VAL)
# Drawing the curve
lines(HWC_VAL,TS_VAL,col="blue",lwd=2)
#####################################################################################
# Nonlinear least square regression
# Michaelis-Menten Model for Chemical Kinetics to relate the initial velocity of an enzymatic reaction
# to the substrate concentration
# The model is y=(ax/x+b)+error
dt=read.csv("Puromycin.csv")
head(dt)
plot(dt$SC,dt$RV)
model=nls(RV~(a*SC)/(b+SC),data=dt,start=list(a=200,b=5))
summary(model)
SC_VAL=seq(0.02,1.5,0.001)
head(SC_VAL)
RV_VAL=predict(model,data.frame(SC = SC_VAL),data=dt)
head(RV_VAL)
lines(SC_VAL,RV_VAL,col="blue",lwd=2)
# Problem 2
# Plant Growth Model
# The model is mass=a*(1-exp(-b*week))
# Install and load minpack.lm
dt=read.csv("plants.csv")
dt
plot(dt$week,dt$mass,xlab="weeks",ylab="mass",col="red",pch=1,cex=2)
model=nlsLM(mass~a*(1-exp(-b*week)),data=dt,start=list(a=25,b=9))
summary(model)
week_val=seq(0.02,1.5,0.001)
week_val
mass_val=predict(model,data.frame(week = week_val),data=dt)
mass_val
lines(week_val,mass_val,col="blue",lwd=2)
# The condition number is the maximum of condition index.
# Multicollinearity is present when the VIF is higher than 5 to 10 or
# the condition indices are higher than 10 to 30.
library("tidyverse")
mydata=mtcars %>% select(mpg,cyl,disp,hp,wt)
head(mydata)
#Creation of a Linear Model
model=lm(mpg~., data = mydata)
model
#Check for Multicollinearity in R
#Using Correlation Matrix
library("corrplot")
corrplot(cor(mydata),method="number")
#Using VIFs
library("olsrr")
ols_vif_tol(model)
# Using Eigenvalues and Condition Index
ols_eigen_cindex(model)
#_____________________________________________________________________________________