lemaslab / ehr-preeclampsia-model

Use mom-baby EHR to validate ShinyApp model. The goal of the project is to predict infant delivery for mothers with preeclampsia.
2 stars 0 forks source link

fuzzy_joinleft() needs to be a function #28

Open dlemas opened 2 years ago

dlemas commented 2 years ago

Please create: ufrc_fuzzy_joinleft() in the /utils.R file as a function. This will help reduce the code space devoted to each data processing script.

START LOOP

chunks=length(delivery_ids) pages <- list()

for(i in 1:chunks){

subset data

delivery_subset=delivery_final %>% filter(mom_id==delivery_ids[i]) %>% select(mom_id,everything())

gravid_subset=gravid_final %>% filter(mom_id==delivery_ids[i]) %>% select(mom_id,everything())

fuzzy=fuzzy_left_join(delivery_subset,gravid_subset, by = c("mom_id" = "mom_id", "part_dob" = "pregnancy_start_date", "gest_start_date"="pregnancy_start_date"), match_fun = list(==, >=, <=))

pages[[i]] <- fuzzy } # END LOOP

data_ready=bind_rows(pages)

xkcococo commented 2 years ago

@dlemas I was wondering if this works:

ufrc_fuzzy_joinleft<-function(delivery_final,gravid_final,
                              delivery_ids){

  chunks=length(delivery_ids) 
  pages <- list()

  ## start loop

  for(i in 1:chunks){
    # subset data
    delivery_subset=delivery_final %>%
      filter(mom_id==delivery_ids[i]) %>%
      select(mom_id,everything())

    gravid_subset=gravid_final %>%
      filter(mom_id==delivery_ids[i]) %>%
      select(mom_id,everything())

    fuzzy=fuzzy_left_join(delivery_subset,gravid_subset,
                          by = c("mom_id" = "mom_id",
                                 "part_dob" = "pregnancy_start_date",
                                 "gest_start_date"="pregnancy_start_date"),
                          match_fun = list(`==`, `>=`, `<=`)) 

    pages[[i]] <- fuzzy
  }   ## end loop

  data_ready=bind_rows(pages)
  return(data_ready)
}

The only variables we need to set when use this function are the final data for delivery and gravid, and ID (delivery_ids).