# install/library necessary packages (packages for all sections go here)
library("hopkins")
## Warning: package 'hopkins' was built under R version 4.4.3
library("factoextra")
## Warning: package 'factoextra' was built under R version 4.4.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.4.3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library("cluster")
## Warning: package 'cluster' was built under R version 4.4.3
# Preparing for clustering
# import data
data <- read.csv("C:/Users/Alfie Shepherd/Downloads/Casey data FINAL/AfOD Assessment Data - Monthly - 1998 - Final.csv")
data_differenced <- read.csv("C:/Users/Alfie Shepherd/Downloads/Casey data FINAL/AfOD Assessment Data - Monthly - Differenced - 1998 - Final.csv")
data_d_differenced <- read.csv("C:/Users/Alfie Shepherd/Downloads/Casey data FINAL/AfOD Assessment Data - Monthly - Double Differenced - 1998 - Final.csv")
#removing date
nd_data <- subset(data, select = -c(Date))
nd_data_differenced <- subset(data_differenced, select = -c(Date))
nd_data_d_differenced <- subset(data_d_differenced, select = -c(Date))
# matrixing data
matrix_data <- data.matrix(nd_data)
matrix_differenced <- data.matrix(nd_data_differenced)
matrix_d_differenced <- data.matrix(nd_data_d_differenced)
# scaling
scaled_matrix_data <- scale(matrix_data)
scaled_matrix_differenced <- scale(matrix_differenced)
scaled_matrix_d_differenced <- scale(matrix_d_differenced)
# calculating Hopkin's statistic
hopkins_vals_scaled <- c(hopkins::hopkins(scaled_matrix_d_differenced), hopkins::hopkins(scaled_matrix_differenced), hopkins::hopkins(scaled_matrix_data))
print(hopkins_vals_scaled)
## [1] 1 1 1
# "1"'s for Hopkin's statistic indicate clustering in the data
# clustering data
# estimating optimal number of clusters
factoextra::fviz_nbclust(scaled_matrix_data, FUNcluster = cluster::pam, method = "silhouette")
factoextra::fviz_nbclust(scaled_matrix_differenced, FUNcluster = cluster::pam, method = "silhouette")
factoextra::fviz_nbclust(scaled_matrix_d_differenced, FUNcluster = cluster::pam, method = "silhouette")
# optimal clusters (bvased off below graphs, can change)
opt_k_data <- c(4)
opt_k_diff <- c(3)
opt_k_d_diff <- c(5)
# building PAM models
pam_scaled_matrix_data <- cluster::pam(scaled_matrix_data, opt_k_data)
pam_scaled_matrix_differenced <- cluster::pam(scaled_matrix_differenced, opt_k_diff)
pam_scaled_matrix_d_differenced <- cluster::pam(scaled_matrix_d_differenced, opt_k_d_diff)
# plotting PAM models
factoextra::fviz_cluster(pam_scaled_matrix_data)
factoextra::fviz_cluster(pam_scaled_matrix_differenced)
factoextra::fviz_cluster(pam_scaled_matrix_d_differenced)
# getting clustered info
data_clusinfo <- pam_scaled_matrix_data$clusinfo
data_meloids <- pam_scaled_matrix_data$medoids
differenced_clusinfo <- pam_scaled_matrix_differenced$clusinfo
differenced_meloids <- pam_scaled_matrix_differenced$medoids
d_diff_clusinfo <- pam_scaled_matrix_d_differenced$clusinfo
d_diff_meloids <- pam_scaled_matrix_d_differenced$medoids
print(data_clusinfo)
## size max_diss av_diss diameter separation
## [1,] 96 5.166319 2.700513 9.177241 0.4277283
## [2,] 86 5.093996 3.052057 7.134307 0.4277283
## [3,] 95 6.412698 2.848697 9.647903 1.0884363
## [4,] 47 6.090395 3.122056 6.873559 1.2010761
print(data_meloids)
## Number.of.purchases...000s....Credit.card
## 59 -0.9569238
## 149 -0.3559985
## 221 0.6306448
## 309 1.7646127
## Value.of.purchases....million....Credit.card
## 59 -1.04494792
## 149 -0.05292186
## 221 0.67477508
## 309 1.72769524
## Total.number.of.transactions...000s.
## 59 -0.9560315
## 149 -0.3584278
## 221 0.6337138
## 309 1.7644705
## Total.value.of.transactions....million. Repayments....million.
## 59 -1.04761781 -1.05481886
## 149 -0.03366735 -0.04220024
## 221 0.68746668 0.68770208
## 309 1.70995310 1.63071710
## Credit..Owner.occupier.housing..Seasonally.adjusted....billion.
## 59 -1.14734166
## 149 -0.07681454
## 221 0.54327741
## 309 1.73444406
## Credit..Investor.housing..Seasonally.adjusted....billion.
## 59 -1.1785903
## 149 -0.1746366
## 221 0.7009682
## 309 1.4632971
## Credit..Other.personal..Seasonally.adjusted....billion.
## 59 -1.2796399
## 149 0.3867117
## 221 0.8075767
## 309 0.7973117
## Short.term.non.government.securities.issued.in.Australia...Total....billion.
## 59 -0.70024374
## 149 0.94537776
## 221 -0.09930029
## 309 -0.43146734
## Long.term.non.government.securities.issued.in.Australia...Total.....billion.
## 59 -1.3087937
## 149 0.2019781
## 221 0.5239377
## 309 1.5296261
## Residential.mortgage.securities....billion.
## 59 -0.65464145
## 149 -0.07313137
## 221 -0.51781554
## 309 0.60310434
## Consumer.price.index..31.03.1985.30.06.2025.
## 59 -1.0548836
## 149 -0.1411883
## 221 0.5114512
## 309 1.8970551
## Real.GDP....million...31.03.1985.30.06.2025.
## 59 -1.1446628
## 149 -0.1838382
## 221 0.6287891
## 309 1.6091159
## Household.consumption....million...31.03.1985.30.06.2025.
## 59 -1.20857575
## 149 -0.02546988
## 221 0.71968331
## 309 1.54588341
## Business.investment....million...31.03.1985.30.06.2025.
## 59 -1.3147061
## 149 0.1005208
## 221 0.1667473
## 309 1.2350970
## Public.demand....million...31.03.1985.30.06.2025.
## 59 -1.0592677
## 149 -0.1570394
## 221 0.2755822
## 309 1.9145355
## Changes.in.inventories....million...31.03.1985.30.06.2025.
## 59 0.02167576
## 149 -0.05342163
## 221 -0.03743345
## 309 0.89270196
## Gross.national.expenditure....million...31.03.1985.30.06.2025.
## 59 -1.19562494
## 149 -0.05673138
## 221 0.53876790
## 309 1.63939758
## Real.household.disposable.income....million...31.03.1985.30.06.2025.
## 59 -1.26172676
## 149 0.04332702
## 221 0.64152223
## 309 1.26055899
## Household.saving.ratio..per.cent...31.03.1985.30.06.2025.
## 59 -1.1984439
## 149 0.5493728
## 221 0.2041251
## 309 -0.4863704
## Retail.sales..seasonally.adjusted....million.
## 59 -1.0403439
## 149 -0.2142565
## 221 0.4743470
## 309 1.8061647
## Private.dwelling.approvals..000.s. Business.conditions..Percentage.
## 59 -0.2460256 0.37162035
## 149 -0.3937093 -0.39881535
## 221 1.2677325 0.03523293
## 309 -0.5044721 0.30651311
## Year.ended.wage.growth..Percentage.
## 59 0.6164005
## 149 0.8698443
## 221 -1.5378723
## 309 1.1232882
## Non.farm.labour.productivity.per.hour..Index. Labour.force...000s.
## 59 -0.9790185 -1.12119180
## 149 -0.3117695 -0.06593578
## 221 0.9682591 0.50039441
## 309 1.0363458 1.73797679
## Employment...000s. Unemployment...000s. Hours.worked...000s.
## 59 -1.12135947 -0.2601666 -1.17682779
## 149 -0.04866095 -0.3079092 -0.03098886
## 221 0.44587226 1.0410710 0.44630977
## 309 1.79586455 -0.5740483 1.88845023
## Job.vacancies...000s..Quarterly.
## 59 -0.91227061
## 149 -0.06152056
## 221 -0.12799091
## 309 1.77397530
## Exports....million...31.03.1985.30.06.2025.
## 59 -1.1485673
## 149 -0.3085941
## 221 1.0523435
## 309 1.4085115
## Imports....million...31.03.1985.30.06.2025.
## 59 -1.3068185
## 149 0.1124044
## 221 0.5985665
## 309 1.6573884
## Trade.balance.as.a.per.cent.of.output....million...31.03.1985.30.06.2025.
## 59 -1.0902701
## 149 0.4607618
## 221 0.2022565
## 309 0.8485197
## Commodity.prices...A...Index..2023.24.100.
## 59 -1.23900729
## 149 0.26637127
## 221 -0.07631653
## 309 1.55960976
## Rural.commodity.prices...A...Index..2023.24.100.
## 59 -0.97025873
## 149 -0.08080065
## 221 0.01089606
## 309 0.90035414
## Non.rural.commodity.prices...A...Index..2023.24.100.
## 59 -1.27815328
## 149 0.32500479
## 221 -0.06792611
## 309 1.57845436
## Base.metals.prices...A...Index..2023.24.100.
## 59 -1.33184828
## 149 -0.08928236
## 221 -0.13971862
## 309 1.45131598
## Bulk.commodities.prices...A...Index..2023.24.100.
## 59 -1.26017706
## 149 0.53534434
## 221 0.09083905
## 309 1.48035561
## Gross.total.foreign.assets....billion...30.9.1988.30.6.2025.
## 59 -1.0423722
## 149 -0.4096043
## 221 0.4402032
## 309 1.9072573
## Gross.total.foreign.liabilities....billion...30.9.1988.30.6.2025.
## 59 -1.1249987
## 149 -0.3239179
## 221 0.6172666
## 309 1.6226684
print(differenced_clusinfo)
## size max_diss av_diss diameter separation
## [1,] 172 24.99124 5.421401 36.42341 2.152475
## [2,] 150 14.59829 5.446630 21.35747 2.152475
## [3,] 1 0.00000 0.000000 0.00000 30.159302
print(differenced_meloids)
## Number.of.purchases...000s....Credit.card
## 36 0.005080301
## 14 -0.170871848
## 261 -11.941937115
## Value.of.purchases....million....Credit.card
## 36 0.07436823
## 14 -0.75470925
## 261 -10.63036640
## Total.number.of.transactions...000s.
## 36 0.01611643
## 14 -0.15096861
## 261 -11.94869099
## Total.value.of.transactions....million. Repayments....million.
## 36 0.1546605 0.009866112
## 14 -0.7394499 -0.008353377
## 261 -10.7530707 -6.580068011
## Credit..Owner.occupier.housing..Seasonally.adjusted....billion.
## 36 -0.23924167
## 14 -0.31951355
## 261 -0.06531927
## Credit..Investor.housing..Seasonally.adjusted....billion.
## 36 -0.2823974
## 14 -0.1562364
## 261 -0.2193169
## Credit..Other.personal..Seasonally.adjusted....billion.
## 36 0.3733317
## 14 0.1061936
## 261 -2.6186147
## Short.term.non.government.securities.issued.in.Australia...Total....billion.
## 36 0.1278356
## 14 0.3010669
## 261 1.9343902
## Long.term.non.government.securities.issued.in.Australia...Total.....billion.
## 36 0.08650884
## 14 -0.09702059
## 261 -1.63407957
## Residential.mortgage.securities....billion.
## 36 0.2640051
## 14 0.3974329
## 261 -0.7500456
## Consumer.price.index..31.03.1985.30.06.2025.
## 36 -0.6115239
## 14 -0.1412329
## 261 -4.3738516
## Real.GDP....million...31.03.1985.30.06.2025.
## 36 0.15842204
## 14 0.06455615
## 261 -7.73666475
## Household.consumption....million...31.03.1985.30.06.2025.
## 36 -0.24784894
## 14 0.02802824
## 261 -7.77715097
## Business.investment....million...31.03.1985.30.06.2025.
## 36 -0.05278234
## 14 0.34028335
## 261 -1.99645242
## Public.demand....million...31.03.1985.30.06.2025.
## 36 -0.01249723
## 14 -0.86483557
## 261 0.20290349
## Changes.in.inventories....million...31.03.1985.30.06.2025.
## 36 0.5414762
## 14 -0.2058408
## 261 0.1220023
## Gross.national.expenditure....million...31.03.1985.30.06.2025.
## 36 0.2581226
## 14 0.1141856
## 261 -6.8019051
## Real.household.disposable.income....million...31.03.1985.30.06.2025.
## 36 0.4195491
## 14 -0.5005027
## 261 2.4862615
## Household.saving.ratio..per.cent...31.03.1985.30.06.2025.
## 36 0.7066791
## 14 -0.6058305
## 261 5.7180795
## Retail.sales..seasonally.adjusted....million.
## 36 -0.03384066
## 14 0.03414620
## 261 -11.37448660
## Private.dwelling.approvals..000.s. Business.conditions..Percentage.
## 36 0.4574401 -0.02951675
## 14 -0.5540348 0.13286128
## 261 -0.6318406 -3.55543971
## Year.ended.wage.growth..Percentage.
## 36 -0.06068396
## 14 -0.06068396
## 261 -0.91289781
## Non.farm.labour.productivity.per.hour..Index. Labour.force...000s.
## 36 0.9264794 -0.05800672
## 14 -0.2737325 0.18451326
## 261 1.7266206 -9.56839784
## Employment...000s. Unemployment...000s. Hours.worked...000s.
## 36 0.01554777 -0.1772540 0.1228085
## 14 -0.10238052 0.6834516 -0.2193997
## 261 -10.86832655 5.6037419 -10.1118024
## Job.vacancies...000s..Quarterly.
## 36 -0.2994473
## 14 0.2243692
## 261 -5.0487172
## Exports....million...31.03.1985.30.06.2025.
## 36 -0.7500187
## 14 0.6509087
## 261 -4.6955843
## Imports....million...31.03.1985.30.06.2025.
## 36 -0.3851810
## 14 0.2904433
## 261 -5.2226367
## Trade.balance.as.a.per.cent.of.output....million...31.03.1985.30.06.2025.
## 36 -0.02081454
## 14 -0.02081454
## 261 1.57992288
## Commodity.prices...A...Index..2023.24.100.
## 36 -0.03512379
## 14 0.00524982
## 261 -1.40782665
## Rural.commodity.prices...A...Index..2023.24.100.
## 36 0.1456369
## 14 -0.3590919
## 261 -1.4694951
## Non.rural.commodity.prices...A...Index..2023.24.100.
## 36 -0.03423893
## 14 0.04073852
## 261 -1.27136674
## Base.metals.prices...A...Index..2023.24.100.
## 36 -0.4905774
## 14 0.4697651
## 261 -1.5195158
## Bulk.commodities.prices...A...Index..2023.24.100.
## 36 0.16592437
## 14 -0.09161321
## 261 -0.60668838
## Gross.total.foreign.assets....billion...30.9.1988.30.6.2025.
## 36 -0.3560172
## 14 -0.5246928
## 261 -4.0712066
## Gross.total.foreign.liabilities....billion...30.9.1988.30.6.2025.
## 36 -0.6309980
## 14 -0.4009509
## 261 -2.5464924
print(d_diff_clusinfo)
## size max_diss av_diss diameter separation
## [1,] 230 20.21291 4.828996 38.55805 2.102388
## [2,] 89 13.93198 5.369843 20.81254 2.102388
## [3,] 1 0.00000 0.000000 0.00000 24.176453
## [4,] 1 0.00000 0.000000 0.00000 23.854291
## [5,] 1 0.00000 0.000000 0.00000 21.761575
print(d_diff_meloids)
## Number.of.purchases...000s....Credit.card
## 9 0.36763022
## 10 -0.01577119
## 260 -6.78734494
## 261 12.33693777
## 263 -1.55141122
## Value.of.purchases....million....Credit.card
## 9 0.06507044
## 10 -0.30905861
## 260 -3.49751638
## 261 10.52454382
## 263 -2.84929745
## Total.number.of.transactions...000s.
## 9 0.4150117
## 10 -0.0204542
## 260 -6.7784375
## 261 12.3252534
## 263 -1.5400809
## Total.value.of.transactions....million. Repayments....million.
## 9 0.1461369 0.22960556
## 10 -0.3693858 -0.09605127
## 260 -3.6647689 -1.70287068
## 261 10.6016845 -0.08022374
## 263 -2.8218601 0.74757770
## Credit..Owner.occupier.housing..Seasonally.adjusted....billion.
## 9 -0.041803090
## 10 -0.002067016
## 260 0.395293726
## 261 0.286019522
## 263 0.156877280
## Credit..Investor.housing..Seasonally.adjusted....billion.
## 9 -0.07609520
## 10 0.02848930
## 260 0.20777701
## 261 -0.56913639
## 263 -0.09103584
## Credit..Other.personal..Seasonally.adjusted....billion.
## 9 0.12017418
## 10 0.08028131
## 260 -0.91704052
## 261 0.87813877
## 263 -0.51811179
## Short.term.non.government.securities.issued.in.Australia...Total....billion.
## 9 0.2588097
## 10 -0.4368541
## 260 3.3750023
## 261 -1.6947668
## 263 0.2969282
## Long.term.non.government.securities.issued.in.Australia...Total.....billion.
## 9 -0.05713353
## 10 0.24621170
## 260 0.94212133
## 261 1.83431317
## 263 -0.37832259
## Residential.mortgage.securities....billion.
## 9 0.0002785367
## 10 0.0002785367
## 260 -0.4705878281
## 261 0.5384115251
## 263 -0.0669880868
## Consumer.price.index..31.03.1985.30.06.2025.
## 9 -0.003974404
## 10 -0.003974404
## 260 -5.123007207
## 261 -0.643853505
## 263 8.314453901
## Real.GDP....million...31.03.1985.30.06.2025.
## 9 0.001193556
## 10 0.001319337
## 260 -8.233651743
## 261 0.001193556
## 263 12.455061429
## Household.consumption....million...31.03.1985.30.06.2025.
## 9 9.517124e-05
## 10 -4.163742e-05
## 260 -7.705926e+00
## 261 -4.163742e-05
## 263 1.304610e+01
## Business.investment....million...31.03.1985.30.06.2025.
## 9 0.0008644734
## 10 0.0013477380
## 260 -2.1213921639
## 261 0.0013477380
## 263 2.1236043753
## Public.demand....million...31.03.1985.30.06.2025.
## 9 0.005951434
## 10 0.005425827
## 260 -1.066023214
## 261 0.005688630
## 263 1.796430296
## Changes.in.inventories....million...31.03.1985.30.06.2025.
## 9 3.548164e-05
## 10 3.548164e-05
## 260 5.061906e-01
## 261 1.544930e-04
## 263 1.445309e+00
## Gross.national.expenditure....million...31.03.1985.30.06.2025.
## 9 0.001629057
## 10 0.001732909
## 260 -7.010092497
## 261 0.001732909
## 263 12.494761456
## Real.household.disposable.income....million...31.03.1985.30.06.2025.
## 9 0.002633445
## 10 0.002773512
## 260 2.033403366
## 261 0.002703478
## 263 -0.792320150
## Household.saving.ratio..per.cent...31.03.1985.30.06.2025.
## 9 0.1362185
## 10 -0.1328757
## 260 5.3835544
## 261 0.1362185
## 263 -7.9366061
## Retail.sales..seasonally.adjusted....million.
## 9 -0.05640817
## 10 -0.02519729
## 260 -9.34554531
## 261 11.80502539
## 263 0.54310075
## Private.dwelling.approvals..000.s. Business.conditions..Percentage.
## 9 -0.37909935 0.09066099
## 10 0.37037105 -0.76007476
## 260 0.04247775 1.42978209
## 261 -0.28541555 4.40735724
## 263 1.30720905 -1.16968828
## Year.ended.wage.growth..Percentage.
## 9 0.03278012
## 10 0.03278012
## 260 -0.92678342
## 261 -0.92678342
## 263 0.03278012
## Non.farm.labour.productivity.per.hour..Index. Labour.force...000s.
## 9 -0.509218513 -0.1028078
## 10 0.518796290 0.3843657
## 260 0.518796290 -6.8314295
## 261 0.004788889 3.5132341
## 263 -4.107270322 -2.4705600
## Employment...000s. Unemployment...000s. Hours.worked...000s.
## 9 -0.1938237 0.217753 -0.04859768
## 10 0.8952141 -1.233830 -0.19955825
## 260 -8.0808394 3.628139 -6.16757055
## 261 4.0375418 -1.587549 6.97311529
## 263 -1.4204092 -2.224911 -1.57455996
## Job.vacancies...000s..Quarterly.
## 9 -0.004345886
## 10 -0.004345889
## 260 -5.733413997
## 261 -0.004345887
## 263 10.395288203
## Exports....million...31.03.1985.30.06.2025.
## 9 -0.004090622
## 10 -0.003855243
## 260 -2.925377775
## 261 -0.003972932
## 263 2.804096985
## Imports....million...31.03.1985.30.06.2025.
## 9 -0.002390786
## 10 -0.002390786
## 260 -3.665718044
## 261 -0.002277123
## 263 8.936786966
## Trade.balance.as.a.per.cent.of.output....million...31.03.1985.30.06.2025.
## 9 -0.001173804
## 10 -0.001173804
## 260 1.510685133
## 261 0.376790931
## 263 -4.536750614
## Commodity.prices...A...Index..2023.24.100.
## 9 0.04424128
## 10 0.45242734
## 260 -3.05797273
## 261 0.57488315
## 263 0.94225060
## Rural.commodity.prices...A...Index..2023.24.100.
## 9 -0.05711259
## 10 0.63633588
## 260 -1.55496128
## 261 1.63490167
## 263 -0.69508518
## Non.rural.commodity.prices...A...Index..2023.24.100.
## 9 0.04171982
## 10 0.34445294
## 260 -2.83424484
## 261 0.38229458
## 263 0.94991919
## Base.metals.prices...A...Index..2023.24.100.
## 9 -0.3138043
## 10 -0.1429278
## 260 -1.1112281
## 261 0.9108108
## 263 0.7399343
## Bulk.commodities.prices...A...Index..2023.24.100.
## 9 0.2228662
## 10 0.4412395
## 260 -2.4522064
## 261 0.1682729
## 263 0.9052827
## Gross.total.foreign.assets....billion...30.9.1988.30.6.2025.
## 9 -0.003908744
## 10 -0.003908744
## 260 -8.199775368
## 261 -0.008863923
## 263 3.628237710
## Gross.total.foreign.liabilities....billion...30.9.1988.30.6.2025.
## 9 0.0006684932
## 10 -0.0100942473
## 260 -5.5529056234
## 261 0.0006684932
## 263 1.8626226059