install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
install.packages("dplyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(dplyr)
install.packages("caret")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## 
## The following object is masked from 'package:purrr':
## 
##     lift
install.packages("readxl")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(readxl)
install.packages("corrplot")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(corrplot)
## corrplot 0.92 loaded
install.packages("e1071")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(e1071)
install.packages("glmnet")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(glmnet)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## Loaded glmnet 4.1-8
install.packages("zoo")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(zoo)
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

1. OBSTAIN

data <- read_excel("Inflasi_Kota_Medan 1.xlsx")
head(data)
## # A tibble: 6 × 3
##   Bulan    Tahun Inflasi
##   <chr>    <dbl> <chr>  
## 1 Januari   2010 0,41   
## 2 Februari  2010 0,49   
## 3 Maret     2010 -0,18  
## 4 April     2010 -0,8   
## 5 Mei       2010 1,37   
## 6 Juni      2010 -1,7
tail(data)
## # A tibble: 6 × 3
##   Bulan     Tahun Inflasi
##   <chr>     <dbl> <chr>  
## 1 Juli       2021 0,28   
## 2 Agustus    2021 -0,27  
## 3 September  2021 -0,31  
## 4 Oktober    2021 0,44   
## 5 November   2021 0,3    
## 6 Desember   2021 0,95

2. SCRUB

#Melihat struktur data

glimpse(data)
## Rows: 144
## Columns: 3
## $ Bulan   <chr> "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli"…
## $ Tahun   <dbl> 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 20…
## $ Inflasi <chr> "0,41", "0,49", "-0,18", "-0,8", "1,37", "-1,7", "2,1", "1,22"…
#Merubah isi data agar bisa di convert ke numeric

data$Inflasi <- gsub(",", ".", data$Inflasi)
#Merubah Tipe data

data$Inflasi <- as.numeric(data$Inflasi)

glimpse(data)
## Rows: 144
## Columns: 3
## $ Bulan   <chr> "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli"…
## $ Tahun   <dbl> 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 20…
## $ Inflasi <dbl> 0.41, 0.49, -0.18, -0.80, 1.37, -1.70, 2.10, 1.22, 0.45, -0.70…
summary(data)
##     Bulan               Tahun         Inflasi       
##  Length:144         Min.   :2010   Min.   :-2.1000  
##  Class :character   1st Qu.:2013   1st Qu.:-0.3650  
##  Mode  :character   Median :2016   Median : 0.3000  
##                     Mean   :2016   Mean   : 0.3431  
##                     3rd Qu.:2018   3rd Qu.: 0.9425  
##                     Max.   :2021   Max.   : 3.9600
#Mencari data Kosong
colSums(is.na(data))
##   Bulan   Tahun Inflasi 
##       0       0       0

3. Explore Data

dataCopy <- data
dataCopy <- dataCopy %>% mutate(Bulan = case_when(Bulan == "Januari" ~ 1, Bulan == "Februari" ~ 2, Bulan == "Maret" ~ 3, Bulan == "April" ~ 4, Bulan == "Mei" ~ 5, Bulan == "Juni" ~ 6, Bulan == "Juli" ~ 7, Bulan == "Agustus" ~ 8, Bulan == "September" ~ 9, Bulan == "Oktober" ~ 10, Bulan == "November" ~ 11, Bulan == "Desember" ~ 12))
str ( dataCopy$Bulan)
##  num [1:144] 1 2 3 4 5 6 7 8 9 10 ...
dataCopy2 <- dataCopy
dataCopy$Bulan <- as.character(dataCopy$Bulan)
glimpse(dataCopy$Bulan)
##  chr [1:144] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "1" "2" "3" ...
dataCopy$Per_Bulan <- paste(dataCopy$Tahun, dataCopy$Bulan, sep = "-")
glimpse(dataCopy)
## Rows: 144
## Columns: 4
## $ Bulan     <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12…
## $ Tahun     <dbl> 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, …
## $ Inflasi   <dbl> 0.41, 0.49, -0.18, -0.80, 1.37, -1.70, 2.10, 1.22, 0.45, -0.…
## $ Per_Bulan <chr> "2010-1", "2010-2", "2010-3", "2010-4", "2010-5", "2010-6", …
dataCopy$Per_Bulan <- as.Date(as.yearmon(dataCopy$Per_Bulan, "%Y-%m"))
print(dataCopy$Per_Bulan)
##   [1] "2010-01-01" "2010-02-01" "2010-03-01" "2010-04-01" "2010-05-01"
##   [6] "2010-06-01" "2010-07-01" "2010-08-01" "2010-09-01" "2010-10-01"
##  [11] "2010-11-01" "2010-12-01" "2011-01-01" "2011-02-01" "2011-03-01"
##  [16] "2011-04-01" "2011-05-01" "2011-06-01" "2011-07-01" "2011-08-01"
##  [21] "2011-09-01" "2011-10-01" "2011-11-01" "2011-12-01" "2012-01-01"
##  [26] "2012-02-01" "2012-03-01" "2012-04-01" "2012-05-01" "2012-06-01"
##  [31] "2012-07-01" "2012-08-01" "2012-09-01" "2012-10-01" "2012-11-01"
##  [36] "2012-12-01" "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01"
##  [41] "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01" "2013-09-01"
##  [46] "2013-10-01" "2013-11-01" "2013-12-01" "2014-01-01" "2014-02-01"
##  [51] "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01" "2014-07-01"
##  [56] "2014-08-01" "2014-09-01" "2014-10-01" "2014-11-01" "2014-12-01"
##  [61] "2015-01-01" "2015-02-01" "2015-03-01" "2015-04-01" "2015-05-01"
##  [66] "2015-06-01" "2015-07-01" "2015-08-01" "2015-09-01" "2015-10-01"
##  [71] "2015-11-01" "2015-12-01" "2016-01-01" "2016-02-01" "2016-03-01"
##  [76] "2016-04-01" "2016-05-01" "2016-06-01" "2016-07-01" "2016-08-01"
##  [81] "2016-09-01" "2016-10-01" "2016-11-01" "2016-12-01" "2017-01-01"
##  [86] "2017-02-01" "2017-03-01" "2017-04-01" "2017-05-01" "2017-06-01"
##  [91] "2017-07-01" "2017-08-01" "2017-09-01" "2017-10-01" "2017-11-01"
##  [96] "2017-12-01" "2018-01-01" "2018-02-01" "2018-03-01" "2018-04-01"
## [101] "2018-05-01" "2018-06-01" "2018-07-01" "2018-08-01" "2018-09-01"
## [106] "2018-10-01" "2018-11-01" "2018-12-01" "2019-01-01" "2019-02-01"
## [111] "2019-03-01" "2019-04-01" "2019-05-01" "2019-06-01" "2019-07-01"
## [116] "2019-08-01" "2019-09-01" "2019-10-01" "2019-11-01" "2019-12-01"
## [121] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01"
## [126] "2020-06-01" "2020-07-01" "2020-08-01" "2020-09-01" "2020-10-01"
## [131] "2020-11-01" "2020-12-01" "2021-01-01" "2021-02-01" "2021-03-01"
## [136] "2021-04-01" "2021-05-01" "2021-06-01" "2021-07-01" "2021-08-01"
## [141] "2021-09-01" "2021-10-01" "2021-11-01" "2021-12-01"
dataCopy <- select(dataCopy, -1)
glimpse(dataCopy)
## Rows: 144
## Columns: 3
## $ Tahun     <dbl> 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, …
## $ Inflasi   <dbl> 0.41, 0.49, -0.18, -0.80, 1.37, -1.70, 2.10, 1.22, 0.45, -0.…
## $ Per_Bulan <date> 2010-01-01, 2010-02-01, 2010-03-01, 2010-04-01, 2010-05-01,…
#Visualisasi Inflasi pertahun
ggplot(dataCopy, aes(x = Per_Bulan ,y = Inflasi)) +
geom_line(color = "darkcyan") +
labs(title = "Grafik Inflasi Kota Medan PerTahun",
x = "Tahun",
y = "Inflasi")

#Visualisasi grafik batang Inflasi perbulan
ggplot(dataCopy, aes(x = Per_Bulan ,y = Inflasi)) +
geom_bar(stat = "identity", fill = "darkmagenta") +
labs(title = "Grafik Inflasi Kota Medan PerTahun",
x = "Tahun",
y = "Inflasi")

dataset_cor <- cor(dataCopy2[,1:3])
corrplot(dataset_cor, method = "color", addCoef.col = "red")

dataset_cor
##              Bulan       Tahun     Inflasi
## Bulan   1.00000000  0.00000000  0.09970172
## Tahun   0.00000000  1.00000000 -0.08428282
## Inflasi 0.09970172 -0.08428282  1.00000000

4. Model

#Partisi Dataset dan membangun model prediksi
#Mempartisi data
set.seed(123)
train_index <- sample(1:nrow(dataCopy2), 0.8 * nrow(dataCopy2))
train_data <- dataCopy2[train_index, ]
test_data <- dataCopy2[-train_index, ]
 
 
# Model GLM
model_glm <- glm(Inflasi ~ Bulan + Tahun , data = train_data)
glm_predictions <- predict(model_glm, newdata = test_data)
 
 
# Model SVR
model_svr <- svm(Inflasi ~ Bulan + Tahun , data = train_data)
svr_predictions <- predict(model_svr, newdata = test_data)

Membandingkan antara hasil prediksi dan data asli untuk GLM

result_data_glm <- data.frame( actual= test_data$Inflasi, Predictions = glm_predictions)
ggplot(data = result_data_glm, aes(x = 1:length(test_data$Inflasi))) +
geom_line(aes(y = test_data$Inflasi, color = "Actual"), size = 1) +
geom_line(aes(y = glm_predictions, color = "GLM Prediction"), size = 1) +
labs(x = "Observasi", y = "Inflasi") +
scale_color_manual(values = c("Actual" = "blue", "GLM Prediction" = "green")) +
ggtitle("Perbandingan GLM vs Aktual") +
scale_y_continuous(labels = scales::comma) +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Membandingkan antara hasil prediksi dan data asli untuk SVR

result_data_svr <- data.frame( actual= test_data$Inflasi, Predictions = svr_predictions)
ggplot(data = result_data_svr, aes(x = 1:length(test_data$Inflasi))) +
geom_line(aes(y = test_data$Inflasi, color = "Actual"), size = 1) +
geom_line(aes(y = svr_predictions, color = "SVR Prediction"), size = 1) +
labs(x = "Observasi", y = "Inflasi") +
scale_color_manual(values = c("Actual" = "purple", "SVR Prediction" = "green")) +
ggtitle("Perbandingan SVR vs Aktual") +
scale_y_continuous(labels = scales::comma) +
theme_minimal()

5. INTERPRETASI

Dari hasil melakukan analitikal data dan analisis regresi terhadap dataset Inflasi Kota Medan. Dataset ini berisikan variabel Bulan, Tahun, dan Inflasi. Tujuan analisis adalah melakukan prediksi inflasi di kota Medan menggunakan model GLM, SVR, dan ANN Regression. dari hasil visualisasi dan evaluasi kami menemukan Model regresi SVR adalah model yang lebih dari pada keduanya

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.