This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Testing the stationarity of the data | |
#Augmented Dickey-Fuller Test | |
adf.test(tsdata) | |
#Autocorrelation test | |
autoplot(acf(tsdata,plot=FALSE))+ labs(title="Correlogram of Air Passengers data") | |
tsdata_decom$random | |
autoplot(acf(tsdata_decom$random[7:138],plot=FALSE))+ labs(title="Correlogram of Air Passengers Random Component") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Decomposing the data into its trend, seasonal, and random error components | |
tsdata_decom <- decompose(tsdata, type = "multiplicative") | |
plot(tsdata_decom) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Check the cycle of data and plot the raw data | |
as.data.frame(tsdata) | |
cycle(tsdata) | |
plot(tsdata, ylab="Passengers (1000s)", type="o") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Installing packages and calling out the libraries | |
install.packages("summarytools") | |
install.packages("tseries") | |
install.packages("forecast") | |
library(forecast) | |
library(ggplot2) | |
library(tseries) | |
library(summarytools) | |
#Reading the Airpaseengers data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("glmnet") | |
library(glmnet) | |
train$Item_Weight[is.na(train$Item_Weight)] <- mean(train$Item_Weight, na.rm = TRUE) | |
train$Outlet_Size[is.na(train$Outlet_Size)] <- "Small" | |
train$Item_Visibility[train$Item_Visibility == 0] <- mean(train$Item_Visibility) | |
train$Outlet_Establishment_Year=2013 - train$Outlet_Establishment_Year | |
train<-train[c(-1)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dummies) | |
train$Item_Weight[is.na(train$Item_Weight)] <- mean(train$Item_Weight, na.rm = TRUE) | |
train$Outlet_Size[is.na(train$Outlet_Size)] <- "Small" | |
train$Item_Visibility[train$Item_Visibility == 0] <- mean(train$Item_Visibility) | |
train$Outlet_Establishment_Year=2013 - train$Outlet_Establishment_Year | |
X<-train[c(-1,-12)] | |
X <- dummy.data.frame(X, names=c("Item_Type","Item_Fat_Content","Outlet_Identifier","Outlet_Size", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X<-train[c(2,6,8)] | |
names((X)) | |
X$Item_Weight[is.na(X$Item_Weight)] <- mean(X$Item_Weight, na.rm = TRUE) | |
Y<-train[c(12)] | |
names((Y)) | |
set.seed(567) | |
part <- sample(2, nrow(X), replace = TRUE, prob = c(0.7, 0.3)) | |
X_train<- X[part == 1,] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("rsq") | |
library(rsq) | |
train<-read.table(file.choose(),sep = ",",header = T) #Importing the train set | |
train[train==""] <- NA #Filling blank values with NA | |
names(train) | |
X<-train[c(6,8)] #Creating new data with two variables | |
names((X)) | |
Y<-train[c(12)] #Storing the dependent variable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("class") | |
library(class) | |
#Normalization | |
normalize <- function(x) { | |
return ((x - min(x)) / (max(x) - min(x))) } | |
norm <- as.data.frame(lapply(data[,1:4], normalize)) | |
set.seed(123) | |
data_spl <- sample(1:nrow(norm),size=nrow(norm)*0.7,replace = FALSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Writing the function to predict kNN | |
knn_predict <- function(test, train, k_value){ | |
pred <- c() | |
#LOOP-1 | |
for(i in c(1:nrow(test))){ | |
dist = c() | |
char = c() | |
setosa =0 | |
versicolor = 0 | |
virginica = 0 |