library(pacman)
p_load(googlesheets, kirkegaard)
options(digits = 2)
googlesheets::gs_auth()

d = gs_url("https://docs.google.com/spreadsheets/d/1knKg2WG7ZW5cIeFJeNfdXMSAMvp_t5ny0baaOV9OD1M/edit#gid=0") %>%  gs_read()
## Sheet-identifying info appears to be a browser URL.
## googlesheets will attempt to extract sheet key from the URL.
## Putative key: 1knKg2WG7ZW5cIeFJeNfdXMSAMvp_t5ny0baaOV9OD1M
## Sheet successfully identified: "Moscow districts"
## Accessing worksheet titled 'Sheet1'.
## Parsed with column specification:
## cols(
##   District = col_character(),
##   `Mean score` = col_double(),
##   Population = col_double(),
##   `% tertiary` = col_double(),
##   `% PhD` = col_integer(),
##   Zhirinovskiy = col_double(),
##   Zjuganov = col_double(),
##   Mironov = col_double(),
##   Prokhorov = col_double(),
##   Putin = col_double()
## )
#rename and recode
d %<>% mutate(
  tertiary_pct = `% tertiary`/100,
  PHD_pct = `% PhD`/100,
  mean_score = `Mean score`,
  Zhirinovskiy = Zhirinovskiy/100,
  Zjuganov =  Zjuganov/100,
  Mironov = Mironov/100,
  Prokhorov = Prokhorov/100,
  Putin = Putin/100
)
#cors
wtd.cors(d[-1])
##              Mean score Population % tertiary % PhD Zhirinovskiy Zjuganov
## Mean score        1.000     0.3129      0.436  0.43        -0.40    0.188
## Population        0.313     1.0000     -0.172 -0.16         0.12    0.196
## % tertiary        0.436    -0.1718      1.000  0.71        -0.78    0.085
## % PhD             0.433    -0.1554      0.706  1.00        -0.82    0.167
## Zhirinovskiy     -0.404     0.1150     -0.782 -0.82         1.00   -0.293
## Zjuganov          0.188     0.1958      0.085  0.17        -0.29    1.000
## Mironov           0.052     0.0098      0.098  0.23        -0.23    0.460
## Prokhorov         0.469    -0.1491      0.832  0.83        -0.91    0.105
## Putin            -0.495     0.0799     -0.778 -0.81         0.88   -0.367
## tertiary_pct      0.436    -0.1718      1.000  0.71        -0.78    0.085
## PHD_pct           0.433    -0.1554      0.706  1.00        -0.82    0.167
## mean_score        1.000     0.3129      0.436  0.43        -0.40    0.188
##              Mironov Prokhorov Putin tertiary_pct PHD_pct mean_score
## Mean score    0.0523      0.47 -0.50        0.436    0.43      1.000
## Population    0.0098     -0.15  0.08       -0.172   -0.16      0.313
## % tertiary    0.0984      0.83 -0.78        1.000    0.71      0.436
## % PhD         0.2253      0.83 -0.81        0.706    1.00      0.433
## Zhirinovskiy -0.2347     -0.91  0.88       -0.782   -0.82     -0.404
## Zjuganov      0.4603      0.10 -0.37        0.085    0.17      0.188
## Mironov       1.0000      0.17 -0.35        0.098    0.23      0.052
## Prokhorov     0.1749      1.00 -0.95        0.832    0.83      0.469
## Putin        -0.3542     -0.95  1.00       -0.778   -0.81     -0.495
## tertiary_pct  0.0984      0.83 -0.78        1.000    0.71      0.436
## PHD_pct       0.2253      0.83 -0.81        0.706    1.00      0.433
## mean_score    0.0523      0.47 -0.50        0.436    0.43      1.000
#plot Putin votes
GG_scatter(d, "tertiary_pct", "Putin", case_names = "District") +
  scale_x_continuous("% of population with tertiary degree", labels = scales::percent) +
  scale_y_continuous("% voted for Putin in Russian 2012 election", labels = scales::percent)

GG_save("figs/vote_Putin_tertiary.png")

GG_scatter(d, "PHD_pct", "Putin", case_names = "District") +
  scale_x_continuous("% of population with a Ph.D. degree", labels = scales::percent) +
  scale_y_continuous("% voted for Putin in Russian 2012 election", labels = scales::percent)