-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data.R
259 lines (173 loc) · 5.93 KB
/
get_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# Working libraries -------------------------------------------------------
library(fpp3) # time series
library(rjson)
library(blsAPI) # Official API
library(readxl)
library(dplyr)
# Working directory -------------------------------------------------------
setwd('C:/Users/Julian/Desktop/Cursos/Fisica/Econofísica/WorkBench')
getwd()
list.files()
# Get data ----------------------------------------------------------------
# Key Api
api_key <- '3e54893b7aa243398b4248404e48f378'
# get list of ID series
item_list <- read_excel("item_list.xlsx",
sheet = "Sheet2")
nrow(item_list) # 181 items
sample_n(item_list,5)
item_list$item_code # item code's vector
# The API allows us to make a maximum of 50 queries at a time
payload1 <- list(
'seriesid'= item_list$item_code[1:49],
'startyear'=2012,
'endyear'=2024,
'catalog'=FALSE,
'calculations'=FALSE,
'annualaverage'=FALSE,
'registrationKey'= api_key)
df1 <- blsAPI(payload = payload1, api_version = 2,return_data_frame = TRUE)
length(table(df1$seriesID)) # 49 items
nrow(df1) #7350 records
head(df1)
# second part
payload2 <- list(
'seriesid'= item_list$item_code[50:98],
'startyear'=2012,
'endyear'=2024,
'catalog'=FALSE,
'calculations'=FALSE,
'annualaverage'=FALSE,
'registrationKey'= api_key)
df2 <- blsAPI(payload = payload2, api_version = 2,return_data_frame = TRUE)
length(table(df2$seriesID)) # 49 items
nrow(df2) # 7349 records
# third part
payload3 <- list(
'seriesid'= item_list$item_code[99:147],
'startyear'=2012,
'endyear'=2024,
'catalog'=FALSE,
'calculations'=FALSE,
'annualaverage'=FALSE,
'registrationKey'= api_key)
df3 <- blsAPI(payload = payload3, api_version = 2,return_data_frame = TRUE)
length(table(df3$seriesID)) # 49 items
nrow(df3) # 7244 records
# fourth part
nrow(item_list)
payload4 <- list(
'seriesid'= item_list$item_code[148:181],
'startyear'=2012,
'endyear'=2024,
'catalog'=FALSE,
'calculations'=FALSE,
'annualaverage'=FALSE,
'registrationKey'= api_key)
df4 <- blsAPI(payload = payload4, api_version = 2,return_data_frame = TRUE)
length(table(df4$seriesID)) # 34 items
nrow(df4) # 5062 records
df <- rbind(df1,df2,df3,df4)
# Removing objects
rm(df1,df2,df3,df4)
rm(payload1,payload2,payload3,payload4)
dim(df)
# number of items
length(levels(as.factor(df$seriesID))) #181 items
# Checking data consistency
which(!item_list$item_code %in% levels(as.factor(df$seriesID))) # 0
table(df$year) # different records by years
# Data cleansing ----------------------------------------------------------
# Missing values
sum(is.na(df)) # 0 NA
freq_table <- table(df$seriesID)
filtered_values <- freq_table[freq_table < 150]
filtered_values
length(filtered_values) # 12 items have lees records
# What kind of items are they?
items_to_remove <- names(filtered_values)
item_list[item_list$item_code %in% items_to_remove, 4]
# We chose to remove those items
df <- df[!df$seriesID %in% items_to_remove,] # from 27005 to 25350
# Change type
df$date <-parse_date_time(paste(df$year, df$periodName, "01"),
orders = "ymd")
df$date <- as.Date(df$date)
df$seriesID <- as.factor(df$seriesID)
df$value <- as.numeric(df$value)
head(df)
# Creating a tsibble
ts_df <- as_tsibble(df, index = date, key = seriesID) # 169 items
levels(ts_df$seriesID) # 181 series
str(ts_df)
# data loading ------------------------------------------------------------
write.csv(df,file = 'df_base.csv')
write.csv(ts_df,file = 'ts_dataframe.csv')
# list.files()
# stationarity condition --------------------------------------------------
# Change format
# Pivotar el tsibble
pivoted_df <- ts_df %>%
pivot_wider(names_from = seriesID, values_from = value)
# Seleccionar solo las columnas que contienen las variables de seriesID
series_data <- pivoted_df %>%
select(-date,-year, -period, -periodName)
rownames(series_data) <- pivoted_df$date
# Calcular la diferencia logarítmica para cada serie
log_diff_data <- long_data %>%
group_by(seriesID) %>%
arrange(date) %>% # Asegúrate de que los datos estén ordenados por fecha
mutate(
log_value = log(value),
log_diff = log_value - lag(log_value)
) %>%
select(-log_value) # Opcional: elimina la columna log_value si no es necesaria
# Convertir de nuevo a formato ancho
ts_data_diff <- log_diff_data %>%
pivot_wider(
names_from = seriesID,
values_from = log_diff
)
# Mostrar el resultado
print(ts_data_diff)
# logarithmic difference
series_data
# Correlation graph -------------------------------------------------------
library(igraph)
library(corrr)
# Correlation matrix
# Calcular la matriz de correlación
correlation_matrix <- series_data %>%
correlate(method = "pearson",diagonal = 1)
# Set threshold
# threshold <- 0.5 #reino unido
threshold <- 0.82 # uruguay
# Create the adjacency matrix by applying the threshold
correlation_df <- as.data.frame(correlation_matrix)
# Eliminar la columna `term` y convertir a matriz numérica
correlation_matrix_num <- correlation_df %>%
select(-term) %>%
as.matrix()
# Asegurarse de que los nombres de las filas y columnas sean correctos
rownames(correlation_matrix_num) <- correlation_df$term
colnames(correlation_matrix_num) <- correlation_df$term
# Mostrar la matriz de correlación numérica
correlation_matrix_num
# Convert the matrix to data.frame format for display
adjacency_df <- as.data.frame(adjacency_matrix)
# Crear la matriz de adyacencia aplicando el umbral
adjacency_matrix <- abs(correlation_matrix_num) > threshold
# Convertir la matriz de adyacencia a 1s y 0s
adjacency_matrix <- adjacency_matrix * 1
# Mostrar la matriz de adyacencia
adjacency_matrix
# Crear el grafo a partir de la matriz de adyacencia
graph <- graph_from_adjacency_matrix(adjacency_matrix, mode = "undirected", diag = FALSE)
# Verificar el grafo
print(graph)
# Graficar el grafo
windows()
plot(graph, vertex.label = NA,
main = "Grafo basado en la matriz de correlación")
ecount(graph) # 6101
vcount(graph) #169