@@ -66,8 +66,10 @@ cdi <- function(model, weight_prevalence = TRUE) {
6666 cols = " name" ,
6767 patterns = c(" pi\\ [" , item = " [0-9]*" , " ," , class = " [0-9]*" , " \\ ]" )
6868 ) | >
69- dplyr :: mutate(item = as.integer(.data $ item ),
70- class = as.integer(.data $ class ))
69+ dplyr :: mutate(
70+ item = as.integer(.data $ item ),
71+ class = as.integer(.data $ class )
72+ )
7173
7274 hamming <- profile_hamming(
7375 dplyr :: select(measr_extract(model , " classes" ), - " class" )
@@ -76,29 +78,42 @@ cdi <- function(model, weight_prevalence = TRUE) {
7678 dplyr :: select(- c(" profile_1" , " profile_2" , " hamming" )) | >
7779 colnames()
7880
79- item_discrim <- tidyr :: crossing(item = unique(pi_matrix $ item ),
80- profile_1 = unique(pi_matrix $ class ),
81- profile_2 = unique(pi_matrix $ class )) | >
82- dplyr :: left_join(pi_matrix , by = c(" item" , " profile_1" = " class" ),
83- relationship = " many-to-one" ) | >
81+ item_discrim <- tidyr :: crossing(
82+ item = unique(pi_matrix $ item ),
83+ profile_1 = unique(pi_matrix $ class ),
84+ profile_2 = unique(pi_matrix $ class )
85+ ) | >
86+ dplyr :: left_join(
87+ pi_matrix ,
88+ by = c(" item" , " profile_1" = " class" ),
89+ relationship = " many-to-one"
90+ ) | >
8491 dplyr :: rename(" prob_1" = " value" ) | >
85- dplyr :: left_join(pi_matrix , by = c(" item" , " profile_2" = " class" ),
86- relationship = " many-to-one" ) | >
92+ dplyr :: left_join(
93+ pi_matrix ,
94+ by = c(" item" , " profile_2" = " class" ),
95+ relationship = " many-to-one"
96+ ) | >
8797 dplyr :: rename(" prob_2" = " value" ) | >
88- dplyr :: mutate(kli = (.data $ prob_1 * log(.data $ prob_1 / .data $ prob_2 )) +
89- ((1 - .data $ prob_1 ) *
90- log((1 - .data $ prob_1 ) / (1 - .data $ prob_2 )))) | >
91- dplyr :: left_join(hamming , by = c(" profile_1" , " profile_2" ),
92- relationship = " many-to-one" ) | >
93- dplyr :: mutate(dplyr :: across(dplyr :: where(is.logical ),
94- \(x ) {
95- dplyr :: case_when(
96- x & .data $ hamming == 1L ~ TRUE ,
97- .default = NA
98- )
99- }),
100- dplyr :: across(dplyr :: where(is.logical ),
101- \(x ) as.integer(x ) * .data $ kli )) | >
98+ dplyr :: mutate(
99+ kli = (.data $ prob_1 * log(.data $ prob_1 / .data $ prob_2 )) +
100+ ((1 - .data $ prob_1 ) *
101+ log((1 - .data $ prob_1 ) / (1 - .data $ prob_2 )))
102+ ) | >
103+ dplyr :: left_join(
104+ hamming ,
105+ by = c(" profile_1" , " profile_2" ),
106+ relationship = " many-to-one"
107+ ) | >
108+ dplyr :: mutate(
109+ dplyr :: across(dplyr :: where(is.logical ), \(x ) {
110+ dplyr :: case_when(
111+ x & .data $ hamming == 1L ~ TRUE ,
112+ .default = NA
113+ )
114+ }),
115+ dplyr :: across(dplyr :: where(is.logical ), \(x ) as.integer(x ) * .data $ kli )
116+ ) | >
102117 dplyr :: filter(.data $ hamming > 0 ) | >
103118 dplyr :: mutate(weight = 1 / .data $ hamming )
104119
@@ -135,19 +150,23 @@ cdi <- function(model, weight_prevalence = TRUE) {
135150 test_discrim <- item_discrim | >
136151 dplyr :: summarize(dplyr :: across(- " item" , sum ))
137152
138- list (item_discrimination = item_discrim ,
139- test_discrimination = test_discrim )
153+ list (item_discrimination = item_discrim , test_discrimination = test_discrim )
140154}
141155
142156profile_hamming <- function (profiles ) {
143- profile_combos <- tidyr :: crossing(profile_1 = seq_len(nrow(profiles )),
144- profile_2 = seq_len(nrow(profiles )))
145-
157+ profile_combos <- tidyr :: crossing(
158+ profile_1 = seq_len(nrow(profiles )),
159+ profile_2 = seq_len(nrow(profiles ))
160+ )
146161
147- hamming <- mapply(hamming_distance , profile_combos $ profile_1 ,
148- profile_combos $ profile_2 ,
149- MoreArgs = list (profiles = profiles ),
150- SIMPLIFY = FALSE , USE.NAMES = FALSE ) | >
162+ hamming <- mapply(
163+ hamming_distance ,
164+ profile_combos $ profile_1 ,
165+ profile_combos $ profile_2 ,
166+ MoreArgs = list (profiles = profiles ),
167+ SIMPLIFY = FALSE ,
168+ USE.NAMES = FALSE
169+ ) | >
151170 dplyr :: bind_rows()
152171
153172 dplyr :: bind_cols(profile_combos , hamming )
@@ -158,13 +177,25 @@ hamming_distance <- function(prof1, prof2, profiles) {
158177 pattern2 <- profiles [prof2 , ]
159178
160179 pattern1 | >
161- tidyr :: pivot_longer(cols = dplyr :: everything(),
162- names_to = " att" , values_to = " patt1" ) | >
163- dplyr :: left_join(tidyr :: pivot_longer(pattern2 , cols = dplyr :: everything(),
164- names_to = " att" , values_to = " patt2" ),
165- by = " att" , relationship = " one-to-one" ) | >
166- dplyr :: mutate(mismatch = .data $ patt1 != .data $ patt2 ,
167- hamming = sum(.data $ mismatch )) | >
180+ tidyr :: pivot_longer(
181+ cols = dplyr :: everything(),
182+ names_to = " att" ,
183+ values_to = " patt1"
184+ ) | >
185+ dplyr :: left_join(
186+ tidyr :: pivot_longer(
187+ pattern2 ,
188+ cols = dplyr :: everything(),
189+ names_to = " att" ,
190+ values_to = " patt2"
191+ ),
192+ by = " att" ,
193+ relationship = " one-to-one"
194+ ) | >
195+ dplyr :: mutate(
196+ mismatch = .data $ patt1 != .data $ patt2 ,
197+ hamming = sum(.data $ mismatch )
198+ ) | >
168199 dplyr :: select(" att" , " mismatch" , " hamming" ) | >
169200 tidyr :: pivot_wider(names_from = " att" , values_from = " mismatch" )
170201}
0 commit comments