## -----------------------------------------------------------------------------
#| label: load_library
#| echo: false
#| message: false

library(mwana)
library(dplyr)


## -----------------------------------------------------------------------------
#| label: inspect_anthro.02
#| echo: true
#| eval: false

# tail(anthro.02)


## -----------------------------------------------------------------------------
#| label: view_anthro.02
#| echo: false

tail(anthro.02)


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

# anthro.02 |>
#   mw_estimate_prevalence_wfhz(
#     wt = NULL,
#     oedema = oedema
#   )


## -----------------------------------------------------------------------------
#| label: view_unwt_wast_wfhz
#| echo: false

mw_estimate_prevalence_wfhz(
  df = anthro.02,
  wt = NULL,
  oedema = oedema
)


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

# anthro.02 |>
#   mw_estimate_prevalence_wfhz(
#     wt = NULL,
#     oedema = NULL # Setting oedema to NULL
#   )


## -----------------------------------------------------------------------------
#| label: view_unwt_wast_wfhz_noedema
#| echo: false

mw_estimate_prevalence_wfhz(
  df = anthro.02,
  wt = NULL,
  oedema = NULL
)


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

# anthro.02 |>
#   mw_estimate_prevalence_wfhz(
#     wt = NULL,
#     oedema = oedema,
#     province # province is the variable name holding data on where the survey was conducted.
#   )


## -----------------------------------------------------------------------------
#| label: view_unwt_wast_wfhz_province
#| echo: false

anthro.02 |>
  mw_estimate_prevalence_wfhz(
    wt = NULL,
    oedema = oedema,
    province
  )


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

# anthro.02 |>
#   mw_estimate_prevalence_wfhz(
#     wt = wtfactor, # Passing the wtfactor to wt
#     oedema = oedema,
#     province
#   )


## -----------------------------------------------------------------------------
#| label: view_wt_wasting_wfhz
#| echo: false

anthro.02 |>
  mw_estimate_prevalence_wfhz(
    wt = wtfactor,
    oedema = oedema,
    province
  )


## -----------------------------------------------------------------------------
#| label: anthro.3
#| echo: false

head(anthro.03)


## -----------------------------------------------------------------------------
#| label: anthro.3_prev
#| echo: true
#| eval: false

# anthro.03 |>
#   mw_wrangle_wfhz(
#     sex = sex,
#     .recode_sex = TRUE,
#     height = height,
#     weight = weight
#   ) |>
#   mw_estimate_prevalence_wfhz(
#     wt = NULL,
#     oedema = oedema,
#     district
#   )


## -----------------------------------------------------------------------------
#| label: view_anthro.3_prev
#| echo: false

anthro.03 |>
  mw_wrangle_wfhz(
    sex = sex,
    .recode_sex = TRUE,
    height = height,
    weight = weight
  ) |>
  mw_estimate_prevalence_wfhz(
    wt = NULL,
    oedema = oedema,
    district
  )


## flowchart LR
## 
## A[(Input dataset)]
## B[Age‑ratio test]
## C[Classification]
## 
## %% Observed proportion node
## D[Observed proportion<br>aged 24–59 months]
## 
## %% Single decision block
## P{Is proportion > 66%?}
## 
## E{Excellent<br>Good<br>Acceptable}
## F{Problematic}
## 
## I[Estimate unweighted prevalence]
## J[Estimate age‑weighted prevalence]
## 
## K[(Summarised results)]
## 
## A --> B --> C
## B --> D
## 
## %% Decision from D
## D --> P
## 
## %% Unweighted prevalence
## C --> E --> I --> K
## C --> F --> P -->|Yes| I
## 
## %% Age-weighted prevalence
## P -->|No| J --> K
## 

## -----------------------------------------------------------------------------
#| label: anthro.04
#| echo: false

tail(anthro.04)


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

# anthro.04 |>
#   mw_wrangle_age(age = age) |>
#   mw_wrangle_muac(
#     muac = muac,
#     .recode_muac = TRUE,
#     .to = "cm",
#     age = age,
#     sex = sex,
#     .recode_sex = FALSE
#   ) |>
#   mutate(muac = recode_muac(muac, "mm")) |>
#   mw_estimate_prevalence_muac(
#     muac = muac,
#     age = age,
#     wt = NULL,
#     oedema = oedema,
#     analysis_unit
#   )


## -----------------------------------------------------------------------------
#| label: view_prev_muac
#| echo: false

anthro.04 |>
  mw_wrangle_age(age = age) |>
  mw_wrangle_muac(
    muac = muac,
    .recode_muac = TRUE,
    .to = "cm",
    age = age,
    sex = sex,
    .recode_sex = FALSE
  ) |>
  mutate(muac = recode_muac(muac, "mm")) |>
  mw_estimate_prevalence_muac(
    muac = muac,
    age = age,
    wt = NULL,
    oedema = oedema,
    analysis_unit
  )


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

# anthro.04 |>
#   mw_wrangle_age(age = age) |>
#   mw_wrangle_muac(
#     muac = muac,
#     .recode_muac = TRUE,
#     .to = "cm",
#     age = age,
#     sex = sex,
#     .recode_sex = FALSE
#   ) |>
#   mutate(muac = recode_muac(muac, "mm")) |>
#   mw_estimate_age_weighted_prev_muac(
#     muac = muac,
#     oedema = oedema,
#     has_age = TRUE,
#     age = age,
#     age_cat = FALSE,
#     raw_muac = FALSE,
#     analysis_unit
#   )


## -----------------------------------------------------------------------------
#| label: smart_wt_view
#| echo: false

anthro.04 |>
  mw_wrangle_age(age = age) |>
  mw_wrangle_muac(
    muac = muac,
    .recode_muac = TRUE,
    .to = "cm",
    age = age,
    sex = sex,
    .recode_sex = FALSE
  ) |>
  mutate(muac = recode_muac(muac, "mm")) |>
  mw_estimate_age_weighted_prev_muac(
    muac = muac,
    oedema = oedema,
    has_age = TRUE,
    age = age,
    age_cat = FALSE,
    raw_muac = FALSE,
    analysis_unit
  )


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

# anthro.02 |>
#   mw_wrangle_age(
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_muac(
#     sex = sex,
#     .recode_sex = FALSE,
#     muac = muac,
#     .recode_muac = TRUE,
#     .to = "cm",
#     age = age
#   ) |>
#   mutate(
#     muac = recode_muac(muac, .to = "mm")
#   ) |>
#   mw_estimate_prevalence_muac(
#     muac = muac,
#     age = age,
#     wt = wtfactor,
#     oedema = oedema,
#     province
#   )


## -----------------------------------------------------------------------------
#| label: view_wt_muac_prev
#| echo: false
#| message: false

anthro.02 |>
  mw_wrangle_age(
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_muac(
    sex = sex,
    .recode_sex = FALSE,
    muac = muac,
    .recode_muac = TRUE,
    .to = "cm",
    age = age
  ) |>
  mutate(
    muac = recode_muac(muac, .to = "mm")
  ) |>
  mw_estimate_prevalence_muac(
    muac = muac,
    age = age,
    wt = wtfactor,
    oedema = oedema,
    province
  )


## -----------------------------------------------------------------------------
#| label: non-survey
#| echo: true
#| eval: false

# anthro.04 |>
#   mw_wrangle_age(age = age) |>
#   mw_wrangle_muac(
#     muac = muac,
#     .recode_muac = TRUE,
#     .to = "cm",
#     age = age,
#     sex = sex,
#     .recode_sex = FALSE
#   ) |>
#   mutate(muac = recode_muac(muac, "mm")) |>
#   mw_estimate_prevalence_screening(
#     muac = muac,
#     age = age,
#     oedema = oedema,
#     analysis_unit
#   )


## -----------------------------------------------------------------------------
#| label: non-survey-view
#| echo: false

anthro.04 |>
  mw_wrangle_age(age = age) |>
  mw_wrangle_muac(
    muac = muac,
    .recode_muac = TRUE,
    .to = "cm",
    age = age,
    sex = sex,
    .recode_sex = FALSE
  ) |>
  mutate(muac = recode_muac(muac, "mm")) |>
  mw_estimate_prevalence_screening(
    muac = muac,
    age = age,
    oedema = oedema,
    analysis_unit
  )


## -----------------------------------------------------------------------------
#| label: non-survey2
#| echo: true
#| eval: false

# anthro.04 |>
#   mutate(age_cat = ifelse(age < 24, "6-23", "24-59")) |>
#   mw_wrangle_muac(sex = sex, .recode_sex = TRUE, muac = muac) |>
#   mw_estimate_prevalence_screening2(
#     age_cat = age_cat,
#     muac = muac,
#     oedema = oedema,
#     analysis_unit
#   )


## -----------------------------------------------------------------------------
#| label: non-survey2-view
#| echo: true
#| eval: false

# anthro.04 |>
#   mutate(age_cat = ifelse(age < 24, "6-23", "24-59")) |>
#   mw_wrangle_muac(sex = sex, .recode_sex = TRUE, muac = muac) |>
#   mw_estimate_prevalence_screening2(
#     age_cat = age_cat,
#     muac = muac,
#     oedema = oedema,
#     analysis_unit
#   )


## -----------------------------------------------------------------------------
#| label: view_anthro.01
#| echo: false
head(anthro.01)


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

# ## Apply the wrangling workflow ----
# anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_muac(
#     sex = sex,
#     .recode_sex = TRUE,
#     muac = muac,
#     .recode_muac = TRUE,
#     .to = "cm",
#     age = age
#   ) |>
#   mutate(
#     muac = recode_muac(muac, .to = "mm")
#   ) |>
#   mw_wrangle_wfhz(
#     sex = sex,
#     weight = weight,
#     height = height,
#     .recode_sex = FALSE
#   )


## -----------------------------------------------------------------------------
#| label: view_combined_wrangling
#| echo: false
#| message: false

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_muac(
    sex = sex,
    .recode_sex = TRUE,
    muac = muac,
    .recode_muac = TRUE,
    .to = "cm",
    age = age
  ) |>
  mutate(
    muac = recode_muac(muac, .to = "mm")
  ) |>
  mw_wrangle_wfhz(
    sex = sex,
    weight = weight,
    height = height,
    .recode_sex = FALSE
  ) |>
  select(area, wfhz, flag_wfhz, mfaz, flag_mfaz)


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

# ## Apply the workflow ----
# anthro.01 |>
#   mw_wrangle_age(
#     dos = dos,
#     dob = dob,
#     age = age,
#     .decimals = 2
#   ) |>
#   mw_wrangle_muac(
#     sex = sex,
#     .recode_sex = TRUE,
#     muac = muac,
#     .recode_muac = TRUE,
#     .to = "cm",
#     age = age
#   ) |>
#   mutate(
#     muac = recode_muac(muac, .to = "mm")
#   ) |>
#   mw_wrangle_wfhz(
#     sex = sex,
#     weight = weight,
#     height = height,
#     .recode_sex = FALSE
#   ) |>
#   mw_estimate_prevalence_combined(
#     wt = NULL,
#     oedema = oedema,
#     area
#   )


## -----------------------------------------------------------------------------
#| label: view_cwasting
#| echo: false

anthro.01 |>
  mw_wrangle_age(
    dos = dos,
    dob = dob,
    age = age,
    .decimals = 2
  ) |>
  mw_wrangle_muac(
    sex = sex,
    .recode_sex = TRUE,
    muac = muac,
    .recode_muac = TRUE,
    .to = "cm",
    age = age
  ) |>
  mutate(
    muac = recode_muac(muac, .to = "mm")
  ) |>
  mw_wrangle_wfhz(
    sex = sex,
    weight = weight,
    height = height,
    .recode_sex = FALSE
  ) |>
  mw_estimate_prevalence_combined(
    wt = NULL,
    oedema = oedema,
    area
  )

