KNN_Timeseries_Assignment(1)
.docx
keyboard_arrow_up
School
Northeastern University *
*We aren’t endorsed by this school
Course
6020
Subject
Aerospace Engineering
Date
Apr 3, 2024
Type
docx
Pages
32
Uploaded by ChiefReindeerMaster1020
Week1 Assignment KNN Time Series
Mohit Manjaria
9/24/2020
ALY 6020 Predictive Analytics
Week1 Assignment KNN Time Series
Mohit Manjaria
Instructor: Marco Montes de Oca
Winter 2021
January 28th 2021
Northeastern University
Introduction
In this assignment, a dataset of search interest of all categories for Predictive analytics term
from January 2061 to January 2021 is selected for K-Nearest Neighbors Analysis. In this dataset, the numbers represent search interest relative to the highest point on the chart for
the given region and time. A value of 100 is the peak popularity for the term. A value of 50 means that the term is half as popular. A score of 0 means there was not enough data for this term. Analysis
In this part, time series forecasting with KNN regression will be performed. According to the auto-regressive model and model requirements that are given, three dimension, four dimension, five dimension and six dimension models with K values from 1 to 10 will be explored. Step 1: Installing libraries
library
(tinytex)
library
(neighbr)
library
(readr)
library
(tsfknn)
library
(zoo)
## ## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## ## as.Date, as.Date.numeric
library
(ggplot2)
Step 2: Importing the data and checking the data
knnt <-
read.csv
(
"C:/Users/mohit/Downloads/multiTimeline.csv"
)
head
(knnt)
## Week Predictive.analytics...United.States.
## 1 1/31/2016 60
## 2 2/7/2016 54
## 3 2/14/2016 58
## 4 2/21/2016 53
## 5 2/28/2016 43
## 6 3/6/2016 54
dim
(knnt)
## [1] 259 2
summary
(knnt)
## Week Predictive.analytics...United.States.
## Length:259 Min. : 9.00 ## Class :character 1st Qu.: 42.00 ## Mode :character Median : 52.00 ## Mean : 53.07 ## 3rd Qu.: 63.00 ## Max. :100.00
#KNN Model
pred <-
knn_forecasting
(knnt
$
Predictive.analytics...United.States., h=
1
, lags =
1
:
2
, k=
1
)
pred
$
prediction
## Time Series:
## Start = 260 ## End = 260
## Frequency = 1 ## [1] 34
pred
$
neighbors
## [1] 259
#Plotting time series
autoplot
(pred, h=
1
)
#Calculating accuracy for k = 1
ro <-
rolling_origin
(pred, h=
1
)
ro
$
global_accu #(Evaluating Using RMSE, MAE, MAPE)
## RMSE MAE MAPE ## 11.00000 11.00000 32.35294
ro
$
predictions
## h=1
## [1,] 45
ro
$
h_accu
## h=1
## RMSE 11.00000
## MAE 11.00000
## MAPE 32.35294
#Calculating Euclidean Distance install.packages(“philentropy”) library(philentropy)
#knn.dist(knnt, dist.meth = “euclidean”, p = 2)
#For n=2 or K = 3
pred <-
knn_forecasting
(knnt
$
Predictive.analytics...United.States., h=
1
, lags =
1
:
2
, k=
3
)
pred
$
prediction
## Time Series:
## Start = 260 ## End = 260 ## Frequency = 1 ## [1] 44.66667
pred
$
neighbors
## [1] 259 218 206
#Plotting time series
autoplot
(pred, h=
1
)
#Calculating accuracy
ro <-
rolling_origin
(pred, h=
1
)
ro
$
global_accu #(Evaluating Using RMSE, MAE, MAPE)
## RMSE MAE MAPE ## 16.33333 16.33333 48.03922
ro
$
predictions
## h=1
## [1,] 50.33333
ro
$
h_accu
## h=1
## RMSE 16.33333
## MAE 16.33333
## MAPE 48.03922
#For n=2, k = 5
pred <-
knn_forecasting
(knnt
$
Predictive.analytics...United.States., h=
1
, lags =
1
:
2
, k=
5
)
pred
$
prediction
## Time Series:
## Start = 260 ## End = 260 ## Frequency = 1 ## [1] 50.6
pred
$
neighbors
## [1] 259 218 206 149 256
#Plotting time series
autoplot
(pred, h=
1
)
#Calculating accuracy
ro <-
rolling_origin
(pred, h=
1
)
ro
$
global_accu #(Evaluating Using RMSE, MAE, MAPE)
## RMSE MAE MAPE ## 20.00000 20.00000 58.82353
ro
$
predictions
## h=1
## [1,] 54
ro
$
h_accu
## h=1
## RMSE 20.00000
## MAE 20.00000
## MAPE 58.82353
#for n = 2, k = 7
pred <-
knn_forecasting
(knnt
$
Predictive.analytics...United.States., h=
1
, lags =
1
:
2
, k=
7
)
pred
$
prediction
## Time Series:
## Start = 260
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help