Skip to main content

Command Palette

Search for a command to run...

DS Prac

DS

Updated
•4 min read•View as Markdown

********************************PRACTICAL 3*****************************************
Aim: Principal component analysis(PCA)
install.packages("e1071")
data_iris <- iris[1:4]

#Calculating the covariance matrix
Cov_data <- cov(data_iris )

#Find out the eigenvectors and eigenvalues using the covariance matrix
Eigen_data <- eigen(Cov_data)

#Using the inbuilt function
PCA_data <- princomp(data_iris ,cor="False")

#Let’s now compare the output variances
Eigen_data$values 

PCA_data$sdev^2 

PCA_data$loadings[,1:4]

Eigen_data$vectors

summary(PCA_data)
biplot (PCA_data)

screeplot(PCA_data, type="lines")


#Select the first principal component for the second model
model2 = PCA_data$loadings[,1]
#For the second model, we need to calculate scores by multiplying our loadings with the data
model2_scores <- as.matrix(data_iris) %*% model2


#Loading libraries for naiveBayes model

library(class)
library(e1071) 


#Fitting the first model over the entire data
mod1<-naiveBayes(iris[,1:4], iris[,5])
#Fitting the second model using the first principal component
mod2<-naiveBayes(model2_scores, iris[,5])

table(predict(mod1, iris[,1:4]), iris[,5]) 

table(predict(mod2, model2_scores), iris[,5])
********************************PRACTICAL 4****************************************
Aim: Practical of Clustering.
install.packages('ggplot2')
#select cran USA (IA)
library(ggplot2)
scatter <- ggplot(data=iris, aes(x=Sepal.Length, , y=Sepal.Width))
scatter + geom_point(aes(color=Species, shape=Species)) + theme_bw() + xlab("Sepal Length") +
ylab("Sepal Width") + ggtitle("Sepal Length-Width")
Iris
ggplot(data=iris,aes(Sepal.Length,fill=Species))+theme_bw()+geom_density(alpha=0.25)+labs(x="Sep
al.Length",title="Species vs Sepal Length") 
vol <- ggplot(data=iris,aes(x=Sepal.Length))
> vol+stat_density(aes(ymax=..density..,ymin=-
..density..,fill=Species,color=Species),geom="ribbon",position="identity")+facet_grid(.~Species)+coor
d_flip()+theme_bw()+labs(x="Sepal Length",title="Species vs Sepal Length")
#Density Diagram
#clustering method1
> irisData <- iris[,1:4]
> totalwSS<-c()
> #Method1 kmeans clustering for 15 times in a loop
> for(i in 1:15)
+ {
+ clusterIRIS <- kmeans(irisData, centers=i)
+ totalwSS[i] <- clusterIRIS$tot.withinss
+ }
>
> #Use plot function to plot values of tot_wss against no-of-clusters
> plot(x=1:15, y=totalwSS,type="b",xlab="Number of Clusters",ylab="Within groupssum-of-squares")
#Method2 Using NbClust - uses huge no of cluster suitability measuring criteria
> install.packages("NbClust")
library(NbClust)
#Set margins as: c(bottom,left,top,right)
> par(mar=c(2,2,2,2))
> nb <- NbClust(irisData,method="kmeans")
hist(nb$Best.nc[1,],breaks=15,main="Histogram for Number of Clusters") 
#Method3 Clustering Data with Silhoutte plot
library(cluster)
#For 2 Clusters
cl <- kmeans(iris[,-5],2)
#Compute and resturns the distance matrix computed by using euclidean distance measure to the
distances between the rows of a data matrix.
dis <- dist(iris[,-5])^2
#getsilhoutte coefficient
sil = silhouette(cl$cluster,dis)
plot(sil, main="Clustering Data with Sihoutte plot using 2 clusters", col=c("cyan", "blue"))
#For 4 clusters
cl <- kmeans(iris[,-5],4)
dis <- dist(iris[,-5])^2
sil = silhouette(cl$cluster,dis)
plot(sil, main="Clustering Data with Sihoutte plot using 4 clusters", col=c("cyan",
"blue","yellow","orange"))
#For 8 clusters
> library(cluster)
> cl <- kmeans(iris[,-5],8)
> #the ditances between the rows of a data matrix.
> dis <- dist(iris[,-5])^2
> #Getsilhoutte coefficient
> sil = silhouette(cl$cluster,dis)
> plot(sil, main="Clustering Data with Sihoutte plot using 8 Clusters", col=c("cyan",
"blue","yellow","orange","red","gray","green","maroon"))

********************************PRACTICAL 5************************************
Aim: Practical of Time-series forecasting.

> data("AirPassengers")
>
> #finding the class name
> class(AirPassengers)
[1] "ts"
> #Data In time series format
>
> #start of Time series format
> start(AirPassengers)
[1] 1949 1
>
> #end of time series
> end(AirPassengers)
[1] 1960 12
>
> frequency(AirPassengers)
[1] 12
>
> #The cycle of this time series is 12 months in a year
> summary(AirPassengers)
Min. 1st Qu. Median Mean 3rd Qu. Max.
104.0 180.0 265.5 280.3 360.5 622.0
>
> #The number of passengers are distributed across the spectrum
> plot(AirPassengers)
#This will plot the time series
> abline(reg=lm(AirPassengers~time(AirPassengers)))
> #This will aggregate the cycles and display a year on year trend
#this will print the cycle across years 
 cycle(AirPassengers)
> plot(aggregate(AirPassengers,FUN=mean))
#Box plot accross months will give us a sense on seasonal effect
> boxplot(AirPassengers~cycle(AirPassengers))
#Following are the ACF plots for the series:
>
> acf(log(AirPassengers))
acf(diff(log(AirPassengers))) 
(fit<-arima(log(AirPassengers),c(0,1,1),seasonal = list(order=c(0,1,1),period=12)))
> pred<-predict(fit,n.ahead=10*12)
> ts.plot(AirPassengers,2.718^pred$pred,log="y",lty=c(1,3))

******************************Practical 6*************************************

Aim: Practical of Simple/Multiple Linear Regression

> lsfit(iris$Petal.Length,iris$Petal.Width)$coefficients
>plot(iris$Petal.Length,iris$Petal.Width,pch=21,bg=c("red","green3","blue")[unclass(iris$Species)],main="Iris Data",xlab="Petal length",ylab="Petal width")
>abline(lsfit(iris$Petal.Length,iris$Petal.Width)$coefficients,col="black")
> #create sample regression model
> lm(Petal.Width~Petal.Length,data=iris)$coefficients
>summary( lm(Petal.Width~Petal.Length,data=iris))
>plot(iris$Sepal.Length,iris$Sepal.Width,pch=21,bg=c("red","green3","blue")[unclass(iris$Species)],main="Iris Data",xlab="Sepal Length",ylab="Sepal Width")
>abline(lm(Sepal.Width~Sepal.Length,data=iris)$coefficients,col="black")
>summary(lm(Sepal.Length~Sepal.Width,data=iris))
>plot(iris$Sepal.Width,iris$Sepal.Lenth,pch=21,bg=c("red","green3","blue")[unclass(iris$Species)],main="Iris Data",xlab="Sepal Width",ylab="Sepal Length")
>abline(lm(Sepal.Length~Sepal.Width,data=iris)$coefficients,col="black")
>abline(lm(Sepal.Length~Sepal.Width,data=iris[which(iris$Species=="setosa"),])$coefficient,col="red")
>abline(lm(Sepal.Length~Sepal.Width,data=iris[which(iris$Species=="versicolor"),])$coefficient,col="green3")
>abline(lm(Sepal.Length~Sepal.Width,data=iris[which(iris$Species=="virginica"),])$coefficient,col="blue")
lm(Sepal.Length~Sepal.Width,data=iris[which(iris$Species=="setosa"),])$coefficients
> lm(Sepal.Length~Sepal.Width,data=iris[which(iris$Species=="versicolor"),])$coefficients 
> lm(Sepal.Length~Sepal.Width,data=iris[which(iris$Species=="virginica"),])$coefficients 
>lm(Sepal.Length~Sepal.Width:Species+Species-1,data=iris)$coefficients
>summary (lm(Sepal.Length~Sepal.Width:Species + Species-1,data=iris))
>#Simplify with AIC (Akaike Information Criterion)
> summary(step(lm(Sepal.Length~Sepal.Width*Species,data=iris)))
>lm(Sepal.Length~Sepal.Width:Species+Species-1,data=iris)$coefficients
>lm(Sepal.Length~Sepal.Width:Species+Species,data=iris)$coefficients

************************************Practical 7******************************

Aim: Practical of Logistics Regression

> library(datasets)
> ir_data<-iris
> head(ir_data)
>str(ir_data)
>levels(ir_data$Species)
>sum(is.na(ir_data))
> ir_data<-ir_data[1:100,]
> set.seed(100)
> samp<-sample(1:100,80)
> ir_test<-ir_data[samp,]
> ir_ctrl<-ir_data[-samp,]
> install.packages("ggplot2")
> library(ggplot2)
> install.packages("GGally")
> library(GGally)
> ggpairs(ir_test)
> y<-ir_test$Species;x<-ir_test$Sepal.Length
> glfit<-glm(y~x, family='binomial')
> summary(glfit)
> #Checking Model's Predictions Let us use our Control set which we defined earlier to predict using this model:
> newdata<-data.frame(x=ir_ctrl$Sepal.Length)
> predicted_val<-predict(glfit, newdata, type="response")
> prediction<-data.frame(ir_ctrl$Sepal.Length,ir_ctrl$Species,predicted_val)
> prediction
> #Let's say that we will consider any plant to be 'versicolor' if its probability for the same is more than 0.5:
> qplot(prediction[,1],round(prediction[,3]),col=prediction[,2],xlab='Sepal Length', ylab='Prediction using Logistic Reg.')

**********************************PRACTICAL 8**********************************

AIM: PRACTICAL OF HYPOTHESIS TESTING

> x=c(6.2,6.6,7.1,7.4,7.6,7.9,8,8.3,8.4,8.5,8.6,8.8,8.8,9.1,9.2,9.4,9.4,9.7,9.9,10.2,10.4,10.8,11.3,11.9)
> t.test(x-9,alternative="two.sided",conf.level=0.95)
> #CREATE TWO SETS OF DATA 
> x=c(418,421,421,422,425,427,431,434,437,439,446,447,448,453,454,463,465)
> y=c(429,430,430,431,436,437,440,441,445,446,447)
> test2<-t.test(x,y,alternative="two.sided",mu=0,var.equal=F,conf.level=0.95)
> test2

***********************************Practical 9*********************************

Aim: Practical of Analysis of Variance

> y1=c(18.2,20.1,17.6,16.8,18.8,19.7,19.1)
> y2=c(17.4,18.7,19.1,16.4,15.9,18.4,17.7)
> y3=c(15.2,18.8,17.7,16.5,15.9,17.1,16.7)
> y=c(y1,y2,y3)
> n=rep(7,3)
> n
> group=rep(1:3,n)
> group
> tmp=tapply(y,group,stem)
> stem(y)
> tmpfn = function(x) c(sum=sum(x),mean=mean(x),var=var(x),n=length(x))
> tapply(y,group,tmpfn)
> tmpfn(y)
> data=data.frame(y=y,group=factor(group))
> fit=lm(y~group,data)
> anova(fit)
> df=anova(fit)[,"Df"]
> names(df)=c("trt","err")
> df
> alpha=c(0.05,0.01)
> qf(alpha,df["trt"],df["err"],lower.tail=FALSE)
> anova(fit)["Residuals","Sum Sq"]
> anova(fit)["Residuals","Sum Sq"]/qchisq(c(0.025,0.975),18,lower.tail=FALSE)

********************************Practical 10***********************************

Aim: Practical of Decision Tree

> mydata<-data.frame(iris)
> attach(mydata)
> install.packages("rpart")
> library(rpart)
> model<-rpart(Species~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,
+ data=mydata,
+ method="class")
> plot(model)
>text(model,use.n=TRUE,all=TRUE,cex=0.8)
> #this was REGRESSION DECISION TREE
#this is CLASSIFICATION DECISION TREE
> install.packages("tree")
> library(tree)
> model1<-tree(Species~Sepal.Length + Sepal.Width + Petal.Length +Petal.Width,
+ data=mydata,
+ method="class",
+ split="gini")
> plot(model1)
>text(model1,all=TRUE,cex=0.6)
> install.packages("party")
> library(party)
> model2<-ctree(Species~Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
+ data=mydata)
> plot(model2)