forked from xmarquez/GPTDemocracyIndex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_targets.R
163 lines (157 loc) · 5.07 KB
/
_targets.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
# Created by use_targets().
# Follow the comments below to fill in this target script.
# Then follow the manual to check and run the pipeline:
# https://books.ropensci.org/targets/walkthrough.html#inspect-the-pipeline # nolint
# Load packages required to define the pipeline:
library(targets)
# library(tarchetypes) # Load other packages as needed. # nolint
# Set target options:
tar_option_set(
packages = c("tidyverse", "democracyData", "httr"), # packages that your targets need to run
format = "rds" # default storage format
# Set other options as needed.
)
# tar_make_clustermq() configuration (okay to leave alone):
options(clustermq.scheduler = "multiprocess")
# tar_make_future() configuration (okay to leave alone):
future::plan(future.callr::callr)
# Run the R scripts in the R/ folder with your custom functions:
tar_source()
# source("other_functions.R") # Source other scripts as needed. # nolint
# Replace the target list below with your own:
list(
tar_target(
name = fh_data,
command = democracyData::download_fh() |>
filter(year < 2022)
# format = "feather" # efficient storage of large data frames # nolint
),
tar_target(
name = sample_fh,
command = fh_data |>
filter(year %% 5 == 0)
),
tar_target(
name = prompts,
command = create_prompt(sample_fh),
iteration = "list"
),
tar_target(
name = pacl_prompts,
command = create_prompt_pacl(sample_fh),
iteration = "list"),
tar_target(
name = openai_completions,
command = submit_openai(prompts),
pattern = map(prompts)
),
tar_target(
name = openai_completions_pacl,
command = submit_openai(pacl_prompts),
pattern = map(pacl_prompts)
),
tar_target(
name = completions_tibble_pacl,
command = format_results_pacl(openai_completions_pacl)
),
tar_target(
name = completions_tibble,
command = format_results(openai_completions)
),
tar_target(
name = claude_prompts,
command = create_prompt_claude(sample_fh),
iteration = "list"
),
tar_target(
name = claude_prompts_pacl,
command = create_prompt_claude_pacl(fh_data),
iteration = "list"
),
tar_target(
name = claude_completions_pacl,
command = submit_claude(claude_prompts_pacl),
pattern = map(claude_prompts_pacl)
),
tar_target(
name = claude_completions,
command = submit_claude(claude_prompts),
pattern = map(claude_prompts)
),
tar_target(
name = claude_prompts2,
command = create_prompt_claude(fh_data),
iteration = "list"
),
tar_target(
name = claude_completions2,
command = submit_claude(claude_prompts2),
pattern = map(claude_prompts2)
),
tar_target(
name = claude_tibble,
command = format_results_claude(claude_completions)
),
tar_target(
name = claude_tibble2,
command = format_results_claude(claude_completions2)
),
tar_target(
name = correlations_all,
command = all_dem |>
pivot_wider(id_cols = c(extended_country_name:year),
values_from = value_rescaled,
names_from = measure,
names_sort = TRUE) |>
select(-extended_country_name:-year) |>
corrr::correlate()
),
tar_target(
name = correlations_ordinal,
command = all_dem |>
filter(index_type == "ordinal") |>
pivot_wider(id_cols = extended_country_name:year,
values_from = value_rescaled,
names_from = measure,
names_sort = TRUE) |>
select(-extended_country_name:-year) |>
corrr::correlate()
),
tar_target(
name = all_dem,
command = generate_democracy_scores_dataset(verbose = FALSE) |>
filter(extended_country_name %in% combined_ai_scores$extended_country_name,
year %in% combined_ai_scores$year) |>
bind_rows(combined_ai_scores) |>
group_by(measure) |>
mutate(value_rescaled = scales::rescale(value, to = c(0,1))) %>%
group_by(measure, extended_country_name) |>
mutate(coverage = n()) |>
ungroup()
),
tar_target(
name = combined_ai_scores,
command = completions_tibble |>
mutate(dataset = "openai",
index_type = "ordinal",
measure = "openai_score") |>
rename(value = score) |>
bind_rows(claude_tibble |>
mutate(dataset = "anthropic small sample",
index_type = "ordinal",
measure = "claude_score") |>
rename(value = score)) |>
bind_rows(claude_tibble2 |>
mutate(dataset = "anthropic larger sample",
index_type = "ordinal",
measure = "claude_score_2") |>
rename(value = score)) |>
country_year_coder(fh_country, year,
include_in_output = c(
"extended_country_name", "GWn", "cown", "in_GW_system"
),
verbose = FALSE) |>
select(extended_country_name, GWn, cown, in_GW_system,
year, measure, value, confidence, index_type, dataset, justification)
)
)