R/attach_DM.R

Defines functions attach_DM

Documented in attach_DM

#' attach Diabetes Nellitus
#'
#' @param data data
#' @param years years
#'
#' @return
#' The diagnostic criteria for diabetes are:
#' 1) doctor told you have diabetes,
#' 2) Self-reported diabetes for a long time,
#' 3) glycohemoglobin HbA1c(%) > 6.5,
#' 4) fasting glucose (mmol/l) >= 7.0,
#' 5) random blood glucose (mmol/l) >= 11.1,
#' 6) two-hour OGTT blood glucose (mmol/l) >= 11.1,
#' 7) Use of diabetes medication or insulin,
#' 8) Diabetes at birth is considered type 1 diabetes.
#'
#' - DM: diabetes mellitus
#' - GDM: Gestational Diabetes Mellitus
#' - IFG: Impaired Fasting Glycaemia
#' - IGT: Impaired Glucose Tolerance
#' @export
#'
attach_DM <- function(data,years){
    years <- data_years(data,years)
    diq <- nhs_tsv('diq',items = 'question',cat = FALSE,years=years)
    pfq <- nhs_tsv('pfq',items = 'question',cat = FALSE,years=years)
    ghb <- nhs_tsv('lab10\\.|l10_b\\.|l10_c\\.|ghb',items='lab',cat = FALSE,years=years)
    gluam <- nhs_tsv('lab10am|l10am_b|l10am_c|glu',items = 'Laboratory',cat = FALSE,years=years)
    biopro <- nhs_tsv('lab18\\.|l40_b\\.|l40_c\\.|biopro',cat = FALSE,years=years)
    l10_2_b	<- nhs_tsv('l10_2_b',items = 'Laboratory',cat = FALSE,years=years)
    l40_2_b	<- nhs_tsv('l40_2_b',items = 'Laboratory',cat = FALSE,years=years)
    ogtt <- nhs_tsv('ogtt',items = 'Laboratory',cat = FALSE,years=years)
    d <- nhs_read(diq,'diq010',
                  pfq,'pfq069fq,pfd069f:DMday',
                  ghb,'lbxgh',
                  gluam,'lbxglusi,lbdglusi:fglu',
                  biopro,'lbdsglsi:glu1',
                  l10_2_b,'lb2glusi:glu2',
                  l40_2_b,'lb2sglsi:glu3',
                  ogtt,'lbdgltsi',cat = FALSE,
                  lower_cd = TRUE)
    d <- attach_Pregnant(d)
    noPreg <- d$pregnant == 'no' | is.na(d$pregnant)
    Preg <- !noPreg

    d$DM <- 'no'
    d$DM_NA <- 0
    NAs <- 0
    d <- to_NA(d)

    # 1. doctor told you have DM
    if ('diq010' %in% colnames(d)){
        d$DM[d$diq010=='yes' & Preg] <- 'GDM'
        d$DM[d$diq010=='yes' & noPreg] <- 'DM'
        d$DM_NA[is.na(d$diq010)] <- d$DM_NA[is.na(d$diq010)]+1
        NAs <- NAs + 1
    }

    # 2. have DM for long time
    if ('DMday' %in% colnames(d)){
        d$DM_NA[is.na(d$DMday)] <- d$DM_NA[is.na(d$DMday)]+1
        NAs <- NAs + 1

        ck1 <- d$DMday=='since birth'
        d$DMday[ck1] <- NA
        ck <- as.numeric(d$DMday) >0
        d$DM[ck & Preg] <- 'GDM'
        d$DM[ck & noPreg] <- 'DM'
        d$DM[ck1] <- 'DM'
    }
    # 3. HbA1c
    # A1c, diabetes: ≥ 6.5%
    if ('lbxgh' %in% colnames(d)){
        ck <- d$lbxgh>=6.5
        d$DM[ck & noPreg] <- 'DM'
        d$DM_NA[is.na(d$lbxgh)] <- d$DM_NA[is.na(d$lbxgh)]+1
        NAs <- NAs + 1
    }


    # 4. fast glucose
    # fast glucose >=7.1mmol/L DM
    # fast glucose >= 6.11mmol/L and fast glucose <7.0 IFG
    if ('fglu' %in% colnames(d)){
        d$DM[Preg & d$fglu >= 5.1] <- 'GDM'
        d$DM[noPreg & d$fglu>=7.1] <- 'DM'
        d$DM[noPreg & d$fglu>=6.11 & d$fglu <7.1 & d$DM != 'DM'] <- 'IFG'

        d$DM_NA[is.na(d$fglu)] <- d$DM_NA[is.na(d$fglu)]+1
        NAs <- NAs + 1
    }


    # 5. glucose
    # glucose >=11.1mmol/L DM
    if ('glu1' %in% colnames(d)){
        d$DM[d$glu1 >= 8.5 & Preg] <- 'GDM'
        d$DM[d$glu1 >= 11.1 & noPreg] <- 'DM'
        d$DM_NA[is.na(d$glu1)] <- d$DM_NA[is.na(d$glu1)]+1
        NAs <- NAs + 1
    }


    if ('glu2' %in% colnames(d)){
        d$DM[d$glu2 >= 8.5 & Preg] <- 'GDM'
        d$DM[d$glu2 >= 11.1 & noPreg] <- 'DM'
        d$DM_NA[is.na(d$glu2)] <- d$DM_NA[is.na(d$glu2)]+1
        NAs <- NAs + 1
    }
    if ('glu3' %in% colnames(d)){
        d$DM[d$glu3 >= 8.5 & Preg] <- 'GDM'
        d$DM[d$glu3 >= 11.1 & noPreg] <- 'DM'
        d$DM_NA[is.na(d$glu3)] <- d$DM_NA[is.na(d$glu3)]+1
        NAs <- NAs + 1
    }


    # 6. OGTT

    # OGTT: 2-hour glucose >= 11.1: DM
    # OGTT: 2-hour glucose >= 7.7 and < 11.1 : IGT
    if ('lbdgltsi' %in% colnames(d)){
        d$DM[Preg & d$lbdgltsi >= 8.5] <- 'GDM'
        d$DM[noPreg & d$lbdgltsi>=11.1] <- 'DM'
        d$DM[noPreg & d$lbdgltsi>=7.7 & d$lbdgltsi <11.1 & d$DM != 'DM'] <- 'IGT'

        d$DM_NA[is.na(d$lbdgltsi)] <- d$DM_NA[is.na(d$lbdgltsi)]+1
        NAs <- NAs + 1
    }


    # 7. drug use
    d <- attach_Drug('antidiabetic',data =d,cat = FALSE,drugname = 'DMdrug',goal01 = 1)
    ck <- d$DMdrug == 1
    d$DM[Preg & ck] <- 'GDM'
    d$DM[noPreg & ck] <- 'DM'

    d$DM_NA[is.na(d$DMdrug)] <- d$DM_NA[is.na(d$DMdrug)]+1
    NAs <- NAs + 1
    d$DM[d$DM_NA==NAs | d$DM_NA==0] <- NA
    if (missing(data)) return(d[,c("seqn","Year","DM")])
    dplyr::left_join(data,d[,c("seqn","DM")],'seqn')
}
yikeshu0611/nhanesR documentation built on Jan. 29, 2022, 6:08 a.m.