## -----------------------------------------------------------------------------
#| label: setup
#| collapse: true
#| echo: false
#| message: false

library(mwana)
library(dplyr)


## -----------------------------------------------------------------------------
#| label: data
#| echo: true

head(anthro.01)


## -----------------------------------------------------------------------------
#| label: wrangle_age
#| echo: true
#| eval: false

# age_mo <- mw_wrangle_age(
#   df = anthro.01,
#   dos = dos,
#   dob = dob,
#   age = age,
#   .decimals = 2
# )


## -----------------------------------------------------------------------------
#| label: view_age
#| echo: false
#| eval: true

age_mo <- anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  )
head(age_mo)


## -----------------------------------------------------------------------------
#| label: wrangle_wfhz_data
#| echo: true
#| eval: false

# wrangled_df <- anthro.01 |>
#   mw_wrangle_wfhz(
#     sex = sex,
#     weight = weight,
#     height = height,
#     .recode_sex = TRUE
#   )


## -----------------------------------------------------------------------------
#| label: view_df
#| echo: false
#| eval: true

wrangled_df <- anthro.01 |>
  mw_wrangle_wfhz(
    sex = sex,
    weight = weight,
    height = height,
    .recode_sex = TRUE
  )

x <- wrangled_df |>
  dplyr::select(area, wfhz, flag_wfhz)

head(x)


## -----------------------------------------------------------------------------
#| label: pl_wfhz
#| echo: true
#| eval: false

# x <- wrangled_df |>
#   mw_plausibility_check_wfhz(
#     sex = sex,
#     age = age,
#     weight = weight,
#     height = height,
#     flags = flag_wfhz
#   )


## -----------------------------------------------------------------------------
#| label: pipe_workflow
#| echo: true
#| eval: false

# x <- anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_wfhz(
#     sex = sex,
#     weight = weight,
#     height = height,
#     .recode_sex = TRUE
#   ) |>
#   mw_plausibility_check_wfhz(
#     sex = sex,
#     age = age,
#     weight = weight,
#     height = height,
#     flags = flag_wfhz
#   )


## -----------------------------------------------------------------------------
#| label: view_pl_wfhz
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_wfhz(
    sex = sex,
    weight = weight,
    height = height,
    .recode_sex = TRUE
  ) |>
  mw_plausibility_check_wfhz(
    sex = sex,
    age = age,
    weight = weight,
    height = height,
    flags = flag_wfhz
  )


## -----------------------------------------------------------------------------
#| label: neat_table
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_wfhz(
#     sex = sex,
#     weight = weight,
#     height = height,
#     .recode_sex = TRUE
#   ) |>
#   mw_plausibility_check_wfhz(
#     sex = sex,
#     age = age,
#     weight = weight,
#     height = height,
#     flags = flag_wfhz
#   ) |>
#   mw_neat_output_wfhz()


## -----------------------------------------------------------------------------
#| label: view_neat_table
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_wfhz(
    sex = sex,
    weight = weight,
    height = height,
    .recode_sex = TRUE
  ) |>
  mw_plausibility_check_wfhz(
    sex = sex,
    age = age,
    weight = weight,
    height = height,
    flags = flag_wfhz
  ) |>
  mw_neat_output_wfhz()


## -----------------------------------------------------------------------------
#| label: pl_group_by
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_wfhz(
#     sex = sex,
#     weight = weight,
#     height = height,
#     .recode_sex = TRUE
#   ) |>
#   mw_plausibility_check_wfhz(
#     sex = sex,
#     age = age,
#     weight = weight,
#     height = height,
#     flags = flag_wfhz,
#     area,
#     team
#   ) |>
#   mw_neat_output_wfhz()


## -----------------------------------------------------------------------------
#| label: pl_group_by-view
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_wfhz(
    sex = sex,
    weight = weight,
    height = height,
    .recode_sex = TRUE
  ) |>
  mw_plausibility_check_wfhz(
    sex = sex,
    age = age,
    weight = weight,
    height = height,
    flags = flag_wfhz,
    area,
    team
  ) |>
  mw_neat_output_wfhz()


## -----------------------------------------------------------------------------
#| label: wrangle_mfaz_data
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_muac(
#     sex = sex,
#     muac = muac,
#     age = age,
#     .recode_sex = TRUE,
#     .recode_muac = TRUE,
#     .to = "cm"
#   )


## -----------------------------------------------------------------------------
#| label: wrangle_mfaz_data-view
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_muac(
    sex = sex,
    muac = muac,
    age = age,
    .recode_sex = TRUE,
    .recode_muac = TRUE,
    .to = "cm"
  )


## -----------------------------------------------------------------------------
#| label: pl_mfaz
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_muac(
#     sex = sex,
#     muac = muac,
#     age = age,
#     .recode_sex = TRUE,
#     .recode_muac = TRUE,
#     .to = "cm"
#   ) |>
#   mw_plausibility_check_mfaz(
#     sex = sex,
#     muac = muac,
#     age = age,
#     flags = flag_mfaz
#   )


## -----------------------------------------------------------------------------
#| label: pl_mfaz-view
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_muac(
    sex = sex,
    muac = muac,
    age = age,
    .recode_sex = TRUE,
    .recode_muac = TRUE,
    .to = "cm"
  ) |>
  mw_plausibility_check_mfaz(
    sex = sex,
    muac = muac,
    age = age,
    flags = flag_mfaz
  )


## -----------------------------------------------------------------------------
#| label: pretty_mfaz
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_muac(
#     sex = sex,
#     muac = muac,
#     age = age,
#     .recode_sex = TRUE,
#     .recode_muac = TRUE,
#     .to = "cm"
#   ) |>
#   mw_plausibility_check_mfaz(
#     sex = sex,
#     muac = muac,
#     age = age,
#     flags = flag_mfaz
#   ) |>
#   mw_neat_output_mfaz()


## -----------------------------------------------------------------------------
#| label: pretty_mfaz-view
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_muac(
    sex = sex,
    muac = muac,
    age = age,
    .recode_sex = TRUE,
    .recode_muac = TRUE,
    .to = "cm"
  ) |>
  mw_plausibility_check_mfaz(
    sex = sex,
    muac = muac,
    age = age,
    flags = flag_mfaz
  ) |>
  mw_neat_output_mfaz()


## -----------------------------------------------------------------------------
#| label: grouped_mfaz
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_muac(
#     sex = sex,
#     muac = muac,
#     age = age,
#     .recode_sex = TRUE,
#     .recode_muac = TRUE,
#     .to = "cm"
#   ) |>
#   mw_plausibility_check_mfaz(
#     sex = sex,
#     muac = muac,
#     age = age,
#     flags = flag_mfaz,
#     area
#   ) |>
#   mw_neat_output_mfaz()


## -----------------------------------------------------------------------------
#| label: grouped_mfaz-view
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_muac(
    sex = sex,
    muac = muac,
    age = age,
    .recode_sex = TRUE,
    .recode_muac = TRUE,
    .to = "cm"
  ) |>
  mw_plausibility_check_mfaz(
    sex = sex,
    muac = muac,
    age = age,
    flags = flag_mfaz,
    area
  ) |>
  mw_neat_output_mfaz()


## -----------------------------------------------------------------------------
#| label: wrangle_muac
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_muac(
#     sex = sex,
#     muac = muac,
#     age = NULL,
#     .recode_sex = TRUE,
#     .recode_muac = FALSE,
#     .to = "none"
#   )


## -----------------------------------------------------------------------------
#| label: wrangle_muac-view
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_muac(
    sex = sex,
    muac = muac,
    age = NULL,
    .recode_sex = TRUE,
    .recode_muac = FALSE,
    .to = "none"
  )


## -----------------------------------------------------------------------------
#| label: pl_muac
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_muac(
#     sex = sex,
#     muac = muac,
#     age = NULL,
#     .recode_sex = TRUE,
#     .recode_muac = FALSE,
#     .to = "none"
#   ) |>
#   mw_plausibility_check_muac(
#     sex = sex,
#     flags = flag_muac,
#     muac = muac
#   )


## -----------------------------------------------------------------------------
#| label: pl_muac-view
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_muac(
    sex = sex,
    muac = muac,
    age = NULL,
    .recode_sex = TRUE,
    .recode_muac = FALSE,
    .to = "none"
  ) |>
  mw_plausibility_check_muac(
    sex = sex,
    flags = flag_muac,
    muac = muac
  )


## -----------------------------------------------------------------------------
#| label: neat_table_muac
#| echo: true
#| eval: false

# anthro.01 |>
#   mw_wrangle_muac(
#     sex = sex,
#     muac = muac,
#     age = NULL,
#     .recode_sex = TRUE,
#     .recode_muac = FALSE,
#     .to = "none"
#   ) |>
#   mw_plausibility_check_muac(
#     sex = sex,
#     flags = flag_muac,
#     muac = muac
#   ) |>
#   mw_neat_output_muac()


## -----------------------------------------------------------------------------
#| label: neat_table_muac-view
#| echo: false
#| eval: true

anthro.01 |>
  mw_wrangle_muac(
    sex = sex,
    muac = muac,
    age = NULL,
    .recode_sex = TRUE,
    .recode_muac = FALSE,
    .to = "none"
  ) |>
  mw_plausibility_check_muac(
    sex = sex,
    flags = flag_muac,
    muac = muac
  ) |>
  mw_neat_output_muac()


## -----------------------------------------------------------------------------
#| label: by_area
#| echo: true
#| eval: false

# ## Check plausibility ----
# anthro.01 |>
#   mw_wrangle_muac(
#     sex = sex,
#     muac = muac,
#     age = NULL,
#     .recode_sex = TRUE,
#     .recode_muac = FALSE,
#     .to = "none"
#   ) |>
#   mw_plausibility_check_muac(
#     sex = sex,
#     flags = flag_muac,
#     muac = muac,
#     area
#   ) |>
#   mw_neat_output_muac()


## -----------------------------------------------------------------------------
#| label: by_area-view
#| echo: false
#| eval: true

## Check plausibility ----
anthro.01 |>
  mw_wrangle_muac(
    sex = sex,
    muac = muac,
    age = NULL,
    .recode_sex = TRUE,
    .recode_muac = FALSE,
    .to = "none"
  ) |>
  mw_plausibility_check_muac(
    sex = sex,
    flags = flag_muac,
    muac = muac,
    area
  ) |>
  mw_neat_output_muac()

