Skip to content

Instantly share code, notes, and snippets.

View Harshit1694's full-sized avatar

HARSHIT GUPTA Harshit1694

View GitHub Profile
#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")
#Decomposing the data into its trend, seasonal, and random error components
tsdata_decom <- decompose(tsdata, type = "multiplicative")
plot(tsdata_decom)
#Check the cycle of data and plot the raw data
as.data.frame(tsdata)
cycle(tsdata)
plot(tsdata, ylab="Passengers (1000s)", type="o")
#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
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)]
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",
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,]
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
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)
#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