-
Notifications
You must be signed in to change notification settings - Fork 0
/
1b_pairwise_matching.R
56 lines (47 loc) · 1.7 KB
/
1b_pairwise_matching.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
# Finding pairwise matches and constructing the treatment assignment
library(nbpMatching)
participants = read_csv(paste(data_path,
"participants_merged.csv",
sep = ""))
# create and select relevant covariates for matching
participant_matchingvars = participants %>%
select(PSTNR,
MALE,
PEALTER,
MIG,
BILDUNG,
EINSCHRAENKUNG,
BENEFIT_LEVEL,
UE_DAYS) %>%
as.data.frame()
# create matrix of pairwise participant Mahalanobis distances
participant_distances = gendistance(participant_matchingvars, idcol = 1)
participant_distmatrix = distancematrix(participant_distances)
# pairwise matching based on distances
participant_match = nonbimatch(participant_distmatrix)
# random treatment assignment
participant_assignment = assign.grp(participant_match$matches, seed = 1929)
# merge assignment with original covariates
participant_assignment_full =
participant_assignment %>%
as_tibble() %>%
rename(PSTNR = Group1.ID,
match = Group2.ID,
match_row = Group2.Row) %>%
mutate(
treatment_wave = if_else(treatment.grp == "A", 1, 2),
PSTNR = as.integer(PSTNR),
match = as.integer(match),
match_row = as.integer(match_row)
) %>%
select(PSTNR, treatment_wave, match, match_row) %>%
left_join(participant_matchingvars, by = "PSTNR")
# exporting files for AMS; one for each wave
participant_assignment_full %>%
filter(treatment_wave == 1) %>%
select(PSTNR) %>%
write_csv(paste0(data_out, "TeilnehmerInnen_Welle_1.csv"))
participant_assignment_full %>%
filter(treatment_wave == 2) %>%
select(PSTNR) %>%
write_csv(paste0(data_out, "TeilnehmerInnen_Welle_2.csv"))