Correlation Plots

Author

Ben Prystawski

Imports and loading data

library(plyr)
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✔ ggplot2 3.3.6     ✔ purrr   0.3.4
✔ tibble  3.1.7     ✔ dplyr   1.0.9
✔ tidyr   1.2.0     ✔ stringr 1.4.0
✔ readr   2.1.2     ✔ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::arrange()   masks plyr::arrange()
✖ purrr::compact()   masks plyr::compact()
✖ dplyr::count()     masks plyr::count()
✖ dplyr::failwith()  masks plyr::failwith()
✖ dplyr::filter()    masks stats::filter()
✖ dplyr::id()        masks plyr::id()
✖ dplyr::lag()       masks stats::lag()
✖ dplyr::mutate()    masks plyr::mutate()
✖ dplyr::rename()    masks plyr::rename()
✖ dplyr::summarise() masks plyr::summarise()
✖ dplyr::summarize() masks plyr::summarize()
library(here)
here() starts at /Users/ben/Documents/autoregressive-inference

Attaching package: 'here'
The following object is masked from 'package:plyr':

    here
library(ggthemes)
MODEL_NAME <- "local-joint-zipf-net-2"
df_true <- read_csv(here(sprintf("data/evaluation/true-probabilities-%s.csv", 
                                 MODEL_NAME))) |>
  mutate(type = "true")
New names:
Rows: 190 Columns: 5
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," chr
(2): target_var, condition_var dbl (3): ...1, condition_val, prob
ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
Specify the column types or set `show_col_types = FALSE` to quiet this message.
• `` -> `...1`
df_fixed <- read_csv(here(sprintf("data/evaluation/fixed-gen-probabilities-%s.csv", 
                                 MODEL_NAME))) |>
  mutate(type = "fixed")
New names:
Rows: 180 Columns: 5
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," chr
(2): target_var, condition_var dbl (3): ...1, condition_val, prob
ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
Specify the column types or set `show_col_types = FALSE` to quiet this message.
• `` -> `...1`
df_free <- read_csv(here(sprintf("data/evaluation/free-gen-probabilities-%s.csv", 
                                 MODEL_NAME))) |>
  mutate(type = "free")
New names:
Rows: 190 Columns: 6
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," chr
(2): target_var, condition_var dbl (4): ...1, condition_val, prob, n_samples
ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
Specify the column types or set `show_col_types = FALSE` to quiet this message.
• `` -> `...1`
df_all <- rbind.fill(df_true, df_fixed, df_free) |>
  filter(is.na(n_samples) | n_samples >= 100) |>
  select(-n_samples, -"...1") |>
  pivot_wider(
    id_cols=c(target_var, condition_var, condition_val),
    names_from=type,
    values_from=prob
  ) |>
  filter(!is.na(fixed) & !is.na(free))

Compute correlation strengths

fixed_corr <- cor.test(df_all$true, df_all$fixed)[["estimate"]]
free_corr <- cor.test(df_all$true, df_all$free)[["estimate"]]
print(sprintf("correlation strength with fixed generation: %s", fixed_corr))
[1] "correlation strength with fixed generation: 0.99774202115793"
print(sprintf("correlation strength with free generation: %s", free_corr))
[1] "correlation strength with free generation: 0.994639841473858"

Plot the Correlations

ggplot(
  data = df_all,
  mapping = aes(x = true, y = fixed)
) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(
    title = "Correlation between true and fixed generation probabilities",
    x = "true probability",
    y = "fixed generation probability"
  ) +
  theme_tufte()
`geom_smooth()` using formula 'y ~ x'

ggsave(here(sprintf("figures/%s-fixed-true-corrs.png", MODEL_NAME)))
Saving 7 x 5 in image
`geom_smooth()` using formula 'y ~ x'
ggplot(
  data = df_all,
  mapping = aes(x = true, y = free)
) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(
    title = "Correlation between true and free generation probabilities",
    x = "true probability",
    y = "free generation probability"
  ) +
  theme_tufte()
`geom_smooth()` using formula 'y ~ x'

ggsave(here(sprintf("figures/%s-free-true-corrs.png", MODEL_NAME)))
Saving 7 x 5 in image
`geom_smooth()` using formula 'y ~ x'