Consider migration to `dm`

Aperta
#8 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
20/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
r
Ambito
data

Direzione di ricerca

Inizia con CDISCData, CDISCFilteredData, MAE e l’entry point teal_module, quindi esamina l’issue bloccante collegata e il lavoro su data_merge. Confronta il modo in cui dm memorizza relazioni, metadati, filtri, join e colonne duplicate, quindi documenta se può sostituire l’attuale modello dei dati; il lavoro è completato quando sono stati concordati l’ambito della migrazione e i problemi di compatibilità irrisolti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

refactor research

Blocked by https://github.com/insightsengineering/teal/issues/628 - we need more insights about how chevron and teal fits together.

  • Research CDISCData, CDISCFilteredData to store dm object to manage relationships between data.frames object.
  • Research how MAE can be stored
  • Can dm be primary data object passed to the teal_module?

Some initial research
  1. Easy initialization of the relational data
library(dm)

ADSL <- synthetic_cdisc_data("latest")$adsl
ADTTE <- synthetic_cdisc_data("latest")$adtte
ADRS <- synthetic_cdisc_data("latest")$adrs
IRIS <- iris
IRIS$id <- seq_len(nrow(IRIS))

dm <- dm(ADSL, ADTTE, ADRS, IRIS) |>
  dm_add_pk(ADSL, teal::get_cdisc_keys("ADSL")) |>
  dm_add_pk(ADTTE, teal::get_cdisc_keys("ADTTE")) |>
  dm_add_pk(ADRS, teal::get_cdisc_keys("ADRS")) |>
  dm_add_pk(IRIS, "id") |>
  dm_add_fk(table = ADTTE, columns = teal::get_cdisc_keys("ADSL"), ref_table = ADSL) |>
  dm_add_fk(table = ADRS, columns = teal::get_cdisc_keys("ADSL"), ref_table = ADSL)
  1. Get keys
# get primary and foreign keys
dm |> dm_get_all_pks()
dm |> dm_get_all_fks()
  1. Check metadata
    dm throws an error if the primary keys are duplicated
# check if keys are unique
dm |> dm_examine_constraints()
dm(ADSL, ADTTE, ADRS, IRIS) |>
  dm_add_pk(ADSL, teal::get_cdisc_keys("ADSL")) |>
  dm_add_pk(ADTTE, teal::get_cdisc_keys("ADSL")) |>
  dm_examine_constraints() 
  1. dm holds data in list, so the data is copied by reference
# same objects
identical(dm[["ADSL"]], ADSL) 
  1. Filters can be added to metadata with the dm_filter
# apply filter
dm |>
  dm_filter(ADSL, SEX == "M") |>
  dm_filter(ADRS, PARAMCD %in% c("BESRSPI", "INVET")) |>
  dm_filter(ADRS, AVISIT %in% c("BASELINE", "SCREENING", "CYCLE2", "DAY1"))
# filter and select
dm |>
  dm_filter(ADSL, SEX == "M") |>
  dm_filter(ADRS, PARAMCD %in% c("BESRSPI", "INVET")) |>
  dm_apply_filters() |>
  dm_select(ADSL, USUBJID, STUDYID, AGE) |>
  dm_flatten_to_tbl(ADRS) |>
  nrow()
  1. Joining is performed automatically by dm
# join tables
joined <- dm |>
  dm_filter(ADSL, SEX == "M") |>
    dm_filter(ADRS, PARAMCD %in% c("BESRSPI", "INVET")) |>
    dm_apply_filters() |>
    dm_select(ADSL, USUBJID, STUDYID, AGE) |>
    dm_select(ADRS, USUBJID, STUDYID, PARAMCD, AVISIT, AGE, SEX, AVAL) |>
    dm_join_to_tbl(ADSL, ADRS, join = left_join)
  1. dm object is mutable
library(dm)
library(scda)


ADSL <- synthetic_cdisc_data("latest")$adsl
ADTTE <- synthetic_cdisc_data("latest")$adtte
ADRS <- synthetic_cdisc_data("latest")$adrs
IRIS <- iris
IRIS$id <- seq_len(nrow(IRIS))



dm <- dm(ADSL, ADTTE, ADRS, IRIS) 
dm2 <- dm |>
  dm_add_pk(ADSL, teal.data::get_cdisc_keys("ADSL")) |>
  dm_add_pk(ADTTE, teal.data::get_cdisc_keys("ADTTE")) |>
  dm_add_pk(ADRS, teal.data::get_cdisc_keys("ADRS")) |>
  dm_add_pk(IRIS, "id") |>
  dm_add_fk(table = ADTTE, columns = teal.data::get_cdisc_keys("ADSL"), ref_table = ADSL) |>
  dm_add_fk(table = ADRS, columns = teal.data::get_cdisc_keys("ADSL"), ref_table = ADSL)

lobstr::obj_addr(dm)
lobstr::obj_addr(dm2)
  1. dm operations preserve attributes
dm_filtered <- dm |>
  dm_filter(ADSL, SEX == "M") |>
  dm_filter(ADRS, PARAMCD %in% c("BESRSPI", "INVET")) |>
  dm_apply_filters() |>
  dm_select(ADSL, USUBJID, STUDYID, AGE) |>
  dm_select(ADRS, USUBJID, STUDYID, PARAMCD, AVISIT, AGE, SEX, AVAL) |>
  dm_select_tbl(ADSL, ADRS) |>
  dm_flatten_to_tbl(ADRS, ADSL)


attr(joined, "label")
formatters::var_labels(joined)

Issues:

  • dm renames duplicated the columns automatically so we might have a problem with difference between input column names and output column names. input$select_from_adsl and input$select_from_adrs won't be found in colnames(data)

  • won't support same dataset selectors by default. Following will not possible
    Not needed since we are going to fix data_merge https://github.com/insightsengineering/NEST-roadmap/issues/36

    ANL1 <- ADRS %>% filter(PARAMCD == "PARAM1") %>% select(<keys without PARAMCD>, PARAM1 = AVAL) 
    ANL2 <- ADRS %>% filter(PARAMCD == "PARAM2") %>% select(<keys without, PARAMCD>, PARAM2 = AVAL)
    ANL <- merge(ANL1, ANL2, by = c(<keys without PARAMCD>))
    

    Maybe we should reconsider this problematic option and just to do this:

    ANL <- ADRS %>% filter(PARAMCD %in% c("PARAM1", "PARAM2")_) %>% select(<keys>, AVAL)
    
Lingua principale
R
Stelle
11
Fork
10
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pharmaverse/teal.data

Tutte le issue di pharmaverse/teal.data

Issue simili

Altre issue su R

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.