R/hei_method_2015.R

Defines functions hei_2015

# simple sum method
hei_2015 <- function(indat=indat,
                 kcal= 'dr1tkcal',
                 vtotalleg= 'vtotalleg',
                 vdrkgrleg= 'vdrkgrleg',
                 f_total= 'dr1t_f_total',
                 fwholefrt= 'fwholefrt',
                 g_whole= 'dr1t_g_whole',
                 d_total= 'dr1t_d_total',
                 pfallprotleg= 'pfallprotleg',
                 pfseaplantleg= 'pfseaplantleg',
                 monopoly= 'monopoly',
                 satfat= 'dr1tsfat',
                 sodium= 'dr1tsodi',
                 g_refined= 'dr1t_g_refined',
                 add_sugars= 'dr1t_add_sugars',
                 varLabel=FALSE,energy=TRUE,component=TRUE,
                 density=FALSE,join='seqn'){
    # 1. vtotalleg
    ck <- indat[,kcal]>0
    indat$vegden[ck] <- indat[ck,vtotalleg]/(indat[ck,kcal]/1000)
    indat$hei2015c1_totalveg=5*(indat$vegden/1.1)
    indat$hei2015c1_totalveg[indat$hei2015c1_totalveg>5] <- 5
    indat$hei2015c1_totalveg[indat$vegden==0] <- 0

    # 2. vdrkgrleg
    ck <- indat[,kcal]>0
    indat$grbnden[ck] <- indat[ck,vdrkgrleg]/(indat[ck,kcal]/1000)
    indat$hei2015c2_green_and_bean=5*(indat$grbnden/0.2)
    indat$hei2015c2_green_and_bean[indat$hei2015c2_green_and_bean > 5] <- 5
    indat$hei2015c2_green_and_bean[indat$grbnden==0] <- 0

    # 3. f_total
    ck <- indat[,kcal]>0
    indat$frtden[ck] <- indat[ck,f_total]/(indat[ck,kcal]/1000)
    indat$hei2015c3_totalfruit=5*(indat$frtden/0.8)
    indat$hei2015c3_totalfruit[indat$hei2015c3_totalfruit > 5] <- 5
    indat$hei2015c3_totalfruit[indat$frtden==0] <- 0


    # 4. fwholefrt
    ck <- indat[,kcal]>0
    indat$whfrden[ck] <- indat[ck,fwholefrt]/(indat[ck,kcal]/1000)
    indat$hei2015c4_wholefruit=5*(indat$whfrden/0.4)
    indat$hei2015c4_wholefruit[indat$hei2015c4_wholefruit > 5] <- 5
    indat$hei2015c4_wholefruit[indat$whfrden==0] <- 0

    # 5. g_whole
    ck <- indat[,kcal]>0
    indat$wgrnden[ck] <- indat[ck,g_whole]/(indat[ck,kcal]/1000)
    indat$hei2015c5_wholegrain=10*(indat$wgrnden/1.5)
    indat$hei2015c5_wholegrain[indat$hei2015c5_wholegrain > 10] <- 10
    indat$hei2015c5_wholegrain[indat$wgrnden==0] <- 0

    # 6. d_total
    ck <- indat[,kcal]>0
    indat$dairyden[ck] <- indat[ck,d_total]/(indat[ck,kcal]/1000)
    indat$hei2015c6_totaldairy=10*(indat$dairyden/1.3)
    indat$hei2015c6_totaldairy[indat$hei2015c6_totaldairy > 10] <- 10
    indat$hei2015c6_totaldairy[indat$dairyden==0] <- 0


    # 7. pfallprotleg
    ck <- indat[,kcal]>0
    indat$protden[ck] <- indat[ck,pfallprotleg]/(indat[ck,kcal]/1000)
    indat$hei2015c7_totprot=5*(indat$protden/2.5)
    indat$hei2015c7_totprot[indat$hei2015c7_totprot > 5] <- 5
    indat$hei2015c7_totprot[indat$protden==0] <- 0


    # 8. pfseaplantleg
    ck <- indat[,kcal]>0
    indat$seaplden[ck] <- indat[ck,pfseaplantleg]/(indat[ck,kcal]/1000)
    indat$hei2015c8_seaplant_prot=5*(indat$seaplden/0.8)
    indat$hei2015c8_seaplant_prot[indat$hei2015c8_seaplant_prot > 5] <- 5
    indat$hei2015c8_seaplant_prot[indat$seaplden==0] <- 0


    # 9.
    ck <- indat[,satfat]>0
    indat$faratio[ck] <- indat[ck,monopoly]/indat[ck,satfat]
    farmin=1.2
    farmax=2.5
    indat$hei2015c9_fattyacid <- ifelse(indat[,satfat]==0 & indat[,monopoly]==0,0,
                                        ifelse(indat[,satfat]==0 & indat[,monopoly]>0,10,
                                               ifelse(indat$faratio >= farmax,10,
                                                      ifelse(indat$faratio <= farmin,0,10*( (indat$faratio-farmin) / (farmax-farmin) ))
                                               )))

    # 10.sodium
    ck <- indat[,kcal]>0
    indat$sodden[ck] <- indat[ck,sodium]/indat[ck,kcal]
    sodmin=1.1
    sodmax=2.0
    indat$hei2015c10_sodium <- ifelse(indat$sodden <= sodmin,10,
                                      ifelse(indat$sodden>=sodmax,0,
                                             10 - (10 * (indat$sodden-sodmin) / (sodmax-sodmin) )))

    # 11.
    ck <- indat[,kcal]>0
    indat$rgden[ck] <- indat[ck,g_refined]/(indat[ck,kcal]/1000)
    rgmin=1.8
    rgmax=4.3
    indat$hei2015c11_refinedgrain <- ifelse(indat$rgden<=rgmin,10,
                                            ifelse(indat$rgden>=rgmax,0,
                                                   10 - ( 10* (indat$rgden-rgmin) / (rgmax-rgmin) )))


    # 12.
    ck <- indat[,kcal]>0
    indat$sfat_perc[ck] <- 100*(indat[ck,satfat]*9/indat[ck,kcal])
    sfatmin=8
    sfatmax=16
    indat$hei2015c12_sfat <- ifelse(indat$sfat_perc>=sfatmax,0,
                                    ifelse(indat$sfat_perc<=sfatmin,10,
                                           10 - ( 10* (indat$sfat_perc-sfatmin) / (sfatmax-sfatmin) )))

    # 13.
    ck <- indat[,kcal]>0
    indat$addsug_perc[ck] <- 100*(indat[ck,add_sugars]*16/indat[ck,kcal])
    addsugmin=6.5
    addsugmax=26
    indat$hei2015c13_addsug <- ifelse(indat$addsug_perc >= addsugmax,0,
                                      ifelse(indat$addsug_perc<=addsugmin,10,
                                             10 - ( 10* (indat$addsug_perc-addsugmin) / (addsugmax-addsugmin) )))


    ck <- indat[,kcal]==0
    indat$hei2015c1_totalveg[ck]=0
    indat$hei2015c2_green_and_bean[ck]=0
    indat$hei2015c3_totalfruit[ck]=0
    indat$hei2015c4_wholefruit[ck]=0
    indat$hei2015c5_wholegrain[ck]=0
    indat$hei2015c6_totaldairy[ck]=0
    indat$hei2015c7_totprot[ck]=0
    indat$hei2015c8_seaplant_prot[ck]=0
    indat$hei2015c9_fattyacid[ck]=0
    indat$hei2015c10_sodium[ck]=0
    indat$hei2015c11_refinedgrain[ck]=0
    indat$hei2015c12_sfat[ck]=0
    indat$hei2015c13_addsug[ck]=0
    indat$hei2015_total_score <- with(indat,hei2015c1_totalveg + hei2015c2_green_and_bean + hei2015c3_totalfruit + hei2015c4_wholefruit + hei2015c5_wholegrain + hei2015c6_totaldairy +
                                          hei2015c7_totprot + hei2015c8_seaplant_prot + hei2015c9_fattyacid + hei2015c10_sodium + hei2015c11_refinedgrain + hei2015c12_sfat + hei2015c13_addsug)

    if (varLabel){
        indat <- expss::apply_labels(indat,
                                     hei2015_total_score='total hei-2015 score',
                                     hei2015c1_totalveg='hei-2015 component 1 total vegetables',
                                     hei2015c2_green_and_bean='hei-2015 component 2 greens and beans',
                                     hei2015c3_totalfruit='hei-2015 component 3 total fruit',
                                     hei2015c4_wholefruit='hei-2015 component 4 whole fruit',
                                     hei2015c5_wholegrain='hei-2015 component 5 whole grains',
                                     hei2015c6_totaldairy='hei-2015 component 6 dairy',
                                     hei2015c7_totprot='hei-2015 component 7 total protein foods',
                                     hei2015c8_seaplant_prot='hei-2015 component 8 seafood and plant protein',
                                     hei2015c9_fattyacid='hei-2015 component 9 fatty acid ratio',
                                     hei2015c10_sodium='hei-2015 component 10 sodium',
                                     hei2015c11_refinedgrain='hei-2015 component 11 refined grains',
                                     hei2015c12_sfat='hei-2015 component 12 sat fat',
                                     hei2015c13_addsug='hei-2015 component 13 added sugar',
                                     vegden='density of total vegetables per 1000 kcal',
                                     grbnden='density of dark green veg and beans per 1000 kcal',
                                     frtden='density of total fruit per 1000 kcal',
                                     whfrden='density of whole fruit per 1000 kcal',
                                     wgrnden='density of whole grain per 1000 kcal',
                                     dairyden='density of dairy per 1000 kcal',
                                     protden='density of total protein per 1000 kcal',
                                     seaplden='density of seafood and plant protein per 1000 kcal',
                                     faratio='fatty acid ratio',
                                     sodden='density of sodium per 1000 kcal',
                                     rgden='density of refined grains per 1000 kcal',
                                     sfat_perc='percent of calories from sat fat',
                                     addsug_perc='percent of calories from added sugar')
    }

    colnms <- c(join,"hei2015_total_score")
    if (energy) colnms <- c(join,kcal,"hei2015_total_score")

    if (component) colnms <- c(colnms,"hei2015c1_totalveg", "hei2015c2_green_and_bean",
                               "hei2015c3_totalfruit", "hei2015c4_wholefruit", "hei2015c5_wholegrain",
                               "hei2015c6_totaldairy", "hei2015c7_totprot", "hei2015c8_seaplant_prot",
                               "hei2015c9_fattyacid", "hei2015c10_sodium", "hei2015c11_refinedgrain",
                               "hei2015c12_sfat", "hei2015c13_addsug")
    if (density) colnms <- c(colnms,"vegden", "grbnden", "frtden", "whfrden", "wgrnden", "dairyden",
                             "protden", "seaplden", "faratio", "sodden", "rgden", "sfat_perc",
                             "addsug_perc")
    indat[,colnms]


}
yikeshu0611/nhanesR documentation built on Jan. 29, 2022, 6:08 a.m.