-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_correlation.R
50 lines (36 loc) · 1.17 KB
/
simple_correlation.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
library(DBI)
library(dplyr)
library(tidyr)
library(readr)
library(xtable)
# Prevent scientific notation
options(scipen=999)
# Define output file
output_file <- "results_files/simple_correlation.txt"
tpd_db <- file.path(getwd(), "elaborations", "cpi.db")
con_tpd <- dbConnect(RSQLite::SQLite(),
dbname = tpd_db)
categories_pretty <- read_csv("coicop1999_categories_pretty.csv")
results_df <- data.frame()
for (table in dbListTables(con_tpd)) {
df <- dbReadTable(con_tpd, table ) |>
select(date, index, official) %>%
mutate(date=as.Date(date)) |>
drop_na()
x <- cor.test(df$index, df$official)
new_res <- data.frame(
category=table,
correlation=x$estimate,
pvalue=x$p.value
)
results_df <- bind_rows(results_df, new_res)
}
results_df <- merge(results_df, categories_pretty, by="category") |>
select(pretty, correlation, pvalue) |>
arrange(pretty)
print(xtable(results_df), file = output_file, compress = FALSE, include.rownames=FALSE)
sum(results_df$correlation > 0.7)
sum(results_df$correlation > 0.9)
sum(results_df$pvalue > 0.05)
# Disconnect from DB
dbDisconnect(conn = con_tpd)