R/attach_MgDS.R

Defines functions attach_MgDS

Documented in attach_MgDS

#' Magnesium Depletion Score
#'
#' @param data data
#' @param years years
#' @param component logical. whether to keep components
#'
#' @return Magnesium Depletion Score
#' @details
#' MDS was calculated by aggregating 4 factors:
#' - 1) diuretic use (current use for 1 point),
#' - 2) PPI use (current use for 1 point),
#' - 3) kidney function 60 mL/(min · 1.73 m2) ≤ eGFR  <90 mL/(min · 1.73 m2) for 1 point;
#'     eGFR <60 mL/(min · 1.73 m2) for 2 points
#' - 4) alcohol drinking (heavy drinker for 1 point)
#'     Heavy drinkers were defined as >1 drink/d for women and >2 drinks/d for men
#'     US Department of Health and Human Services; US Department of Agriculture. 2015–2020 Dietary guidelines for Americans [Internet]. 8thed. Washington (DC): US Deptartment of Health and Human Services; December 2015. Available from: http://www.health.gov/DietaryGuidel ines Accessed 16 December 2015
#' @export
#' @references
#' Lei Fan, Xiangzhu Zhu, Andrea Rosanoff, Rebecca B Costello, Chang Yu, Reid Ness, Douglas L Seidner, Harvey J Murff, Christianne L Roumie, Martha J Shrubsole, Qi Dai, Magnesium Depletion Score (MDS) Predicts Risk of Systemic Inflammation and Cardiovascular Mortality among US Adults, The Journal of Nutrition, Volume 151, Issue 8, August 2021, Pages 2226–2235, https://doi.org/10.1093/jn/nxab138
#'
attach_MgDS <- function(data,years,component=FALSE){
    if (!missing(data)) years <- unique(data$Year)
    years <- prepare_years(years)

    data0 <- attach_eGFR(method = 'CKD_EPI_Scr',years = years)
    data0$CKD_EPI_Scr <- ifelse(data0$CKD_EPI_Scr <60,2,
                               ifelse(data0$CKD_EPI_Scr <90,1,0))
    data0 <- attach_Drug(data = data0,'diuretic',drugname = 'diuretic',goal01 = 1,cat = FALSE,years = years)
    data0 <- attach_Drug(data = data0,'proton pump inhibitor',drugname = 'ppi',goal01 = 1,cat = FALSE,years = years)


    demo <- nhs_read(nhs_tsv('demo',years = years,cat = FALSE),"riagendr",cat = FALSE,Year = FALSE,lower_cd = TRUE)
    fped <- fped_read(years = years,day = c(1,2),cat = F,fun = 'mean',dietary = 'tot')[,c('seqn','a_drinks')]
    data0 <- dplyr::inner_join(data0,fped,'seqn')
    data0 <- dplyr::inner_join(data0,demo,'seqn')
    ck1 <- (data0$riagendr == 'female' & data0$a_drinks >1) |
        (data0$riagendr == 'male' & data0$a_drinks >2)
    data0$a_drinks <- ifelse(ck1,1,0)
    colnames(data0)[colnames(data0) == 'CKD_EPI_Scr'] <- 'eGFR'
    data0 <- data0[,c("seqn","eGFR","diuretic","ppi",'a_drinks')]
    data0$MgDS <- rowSums(data0[,-1],na.rm = TRUE)
    if (!component) data0 <- data0[,c("seqn",'MgDS')]
    if (missing(data)){
        data <- data0
    }else{
        data <- dplyr::left_join(data,data0,'seqn')
    }
    return(data)
}
yikeshu0611/nhanesR documentation built on Jan. 29, 2022, 6:08 a.m.