R/hei_method_2010.R

Defines functions hei_2010

hei_2010 <- function(indat,
                      kcal = 'dr1tkcal',
                      lv_total= 'legume_added_dr1t_v_total',
                      lbeangrn= 'legume_added_beangrn',
                      f_total= 'dr1t_f_total',
                      wholefrt='wholefrt',
                      g_whl= 'dr1t_g_whole',
                      d_total= 'dr1t_d_total',
                      lallmeat= 'legume_added_allmeat',
                      lseaplant= 'legume_added_seaplant',
                      monopoly='monopoly',
                      sfat='dr1tsfat',
                      sodi='dr1tsodi',
                      g_nwhl= 'dr1t_g_refined',
                      emptycal10='emptycal10',
                      varLabel=FALSE,
                      energy=TRUE,component=TRUE,density=FALSE,join='seqn'
){

    # x1
    k0 <- indat[,kcal] >0
    indat$vegden[k0] <- indat[k0,lv_total]/(indat[k0,kcal]/1000)
    indat$heix1_totalveg=5*(indat$vegden/1.1)
    indat$heix1_totalveg[indat$heix1_totalveg>5] <- 5
    indat$heix1_totalveg[indat$vegden==0] <- 0

    # x2
    indat$grbnden[k0]=indat[k0,lbeangrn]/(indat[k0,kcal]/1000)
    indat$heix2_green_and_bean=5*(indat$grbnden/0.2)
    indat$heix2_green_and_bean[indat$heix2_green_and_bean>5] <- 5
    indat$heix2_green_and_bean[indat$grbnden==0] <- 0

    # x3
    indat$frtden[k0]=indat[k0,f_total]/(indat[k0,kcal]/1000)
    indat$heix3_totalfruit=5*(indat$frtden/0.8)
    indat$heix3_totalfruit[indat$heix3_totalfruit>5] <- 5
    indat$heix3_totalfruit[indat$frtden==0] <- 0

    # x4
    indat$whfrden[k0]=indat[k0,wholefrt]/(indat[k0,kcal]/1000)
    indat$heix4_wholefruit=5*(indat$whfrden/0.4)
    indat$heix4_wholefruit[indat$heix4_wholefruit>5] <- 5
    indat$heix4_wholefruit[indat$whfrden==0] <- 0

    # x5
    indat$wgrnden[k0]=indat[k0,g_whl]/(indat[k0,kcal]/1000)
    indat$heix5_wholegrain=10*(indat[,"wgrnden"]/1.5)
    indat$heix5_wholegrain[indat$heix5_wholegrain>10] <-  10
    indat$heix5_wholegrain[indat$wgrnden==0] <- 0

    # x6
    indat$dairyden[k0]=indat[k0,d_total]/(indat[k0,kcal]/1000)
    indat$heix6_totaldairy=10*(indat$dairyden/1.3)
    indat$heix6_totaldairy[indat$heix6_totaldairy>10] <-  10
    indat$heix6_totaldairy[indat$dairyden==0] <-  0

    # x7
    indat$meatden[k0]=indat[k0,lallmeat]/(indat[k0,kcal]/1000)
    indat$heix7_totprot=5*(indat$meatden/2.5)
    indat$heix7_totprot[indat$heix7_totprot>5] <- 5
    indat$heix7_totprot[indat$meatden==0] <- 0

    # x8
    indat$seaplden[k0] <- indat[k0,lseaplant]/(indat[k0,kcal]/1000)
    indat$heix8_seaplant_prot=5*(indat$seaplden/0.8)
    indat$heix8_seaplant_prot[indat$heix8_seaplant_prot>5] <- 5
    indat$heix8_seaplant_prot[indat$seaplden==0] <- 0

    # x9
    indat$faratio[indat[,sfat]>0] <- indat[indat[,sfat]>0,"monopoly"]/indat[indat[,sfat]>0,sfat]
    farmin=1.2
    farmax=2.5
    indat$heix9_fattyacid <- ifelse(indat[,sfat]==0 & indat[,monopoly]==0,0,
                                    ifelse(indat[,sfat]==0 & indat[,monopoly]>0,10,
                                           ifelse(indat[,"faratio"] >= farmax,10,
                                                  ifelse(indat[,"faratio"]<=farmin,0,
                                                         10*(indat[,"faratio"]-farmin)/(farmax-farmin)))))
    # x10
    indat$sodden[k0] <- indat[k0,sodi]/indat[k0,kcal]
    sodmin=1.1
    sodmax=2.0
    indat$heix10_sodium <- ifelse(indat[,"sodden"]<=sodmin,10,
                                  ifelse(indat[,"sodden"]>=sodmax,0,
                                         10 - (10 * (indat[,"sodden"]-sodmin) / (sodmax-sodmin) )))

    # x11
    indat$rgden[k0] <- indat[k0,g_nwhl]/(indat[k0,kcal]/1000)
    rgmin=1.8
    rgmax=4.3
    indat$heix11_refinedgrain <- ifelse(indat[,"rgden"] <= rgmin,10,
                                        ifelse(indat[,"rgden"]>=rgmax,0,
                                               10 - ( 10* (indat[,"rgden"]-rgmin) / (rgmax-rgmin) )))

    # x12
    indat$sofa_perc[k0] <- 100*indat[k0,"emptycal10"]/indat[k0,kcal]
    sofamin=19
    sofamax=50
    indat$heix12_sofaas <- ifelse(indat$sofa_perc<=sofamin,20,
                                  ifelse(indat$sofa_perc>=sofamax,0,
                                         20 - ( 20* (indat$sofa_perc-sofamin) / (sofamax-sofamin) )))



    indat$heix1_totalveg[indat[,kcal]==0] <- 0
    indat$heix2_green_and_bean[indat[,kcal]==0] <- 0
    indat$heix3_totalfruit[indat[,kcal]==0] <- 0
    indat$heix4_wholefruit[indat[,kcal]==0] <- 0
    indat$heix5_wholegrain[indat[,kcal]==0] <- 0
    indat$heix6_totaldairy[indat[,kcal]==0] <- 0
    indat$heix7_totprot[indat[,kcal]==0] <- 0
    indat$heix8_seaplant_prot[indat[,kcal]==0] <- 0
    indat$heix9_fattyacid[indat[,kcal]==0] <- 0
    indat$heix10_sodium[indat[,kcal]==0] <- 0
    indat$heix11_refinedgrain[indat[,kcal]==0] <- 0
    indat$heix12_sofaas[indat[,kcal]==0] <- 0



    indat$hei2010_total_score = with(indat,heix1_totalveg + heix2_green_and_bean + heix3_totalfruit + heix4_wholefruit + heix5_wholegrain + heix6_totaldairy +
                                         heix7_totprot + heix8_seaplant_prot + heix9_fattyacid + heix10_sodium + heix11_refinedgrain + heix12_sofaas)
    if(varLabel){
        indat <- expss::apply_labels(indat,
                                     hei2010_total_score='total hei-2010 score',
                                     heix1_totalveg='hei-2010 component 1 total vegetables',
                                     heix2_green_and_bean='hei-2010 component 2 greens and beans',
                                     heix3_totalfruit='hei-2010 component 3 total fruit',
                                     heix4_wholefruit='hei-2010 component 4 whole fruit',
                                     heix5_wholegrain='hei-2010 component 5 whole grains',
                                     heix6_totaldairy='hei-2010 component 6 dairy',
                                     heix7_totprot='hei-2010 component 7 total protein foods',
                                     heix8_seaplant_prot='hei-2010 component 8 seafood and plant protein',
                                     heix9_fattyacid='hei-2010 component 9 fatty acid ratio',
                                     heix10_sodium='hei-2010 component 10 sodium',
                                     heix11_refinedgrain='hei-2010 component 11 refined grains',
                                     heix12_sofaas='hei-2010 component 12 sofaas calories',
                                     vegden='density of mped/fped total vegetables per 1000 kcal',
                                     grbnden='density of mped/fped of dark green veg and beans per 1000 kcal',
                                     frtden='density of mped/fped total fruit per 1000 kcal',
                                     whfrden='density of mped/fped whole fruit per 1000 kcal',
                                     wgrnden='density of mped/fped of whole grain per 1000 kcal',
                                     dairyden='density of mped/fped of dairy per 1000 kcal',
                                     meatden='density of mped/fped total meat/protein per 1000 kcal',
                                     seaplden='denstiy of mped/fped of seafood and plant protein per 1000 kcal',
                                     faratio='fatty acid ratio',
                                     sodden='density of sodium per 1000 kcal',
                                     rgden='density of mped/fped of refined grains per 1000 kcal',
                                     sofa_perc='percent of calories from added sugar, solid fat, and alcohol')
    }
    colnms <- c(join,"hei2010_total_score")
    if (energy) colnms <- c(join,kcal,"hei2010_total_score")

    if (component) colnms <- c(colnms,c("heix1_totalveg", "heix2_green_and_bean",
                                        "heix3_totalfruit", "heix4_wholefruit", "heix5_wholegrain", "heix6_totaldairy",
                                        "heix7_totprot", "heix8_seaplant_prot", "heix9_fattyacid", "heix10_sodium",
                                        "heix11_refinedgrain", "heix12_sofaas"))
    if (density) colnms <- c(colnms,"vegden", "grbnden",
                             "frtden", "whfrden", "wgrnden", "dairyden", "meatden", "seaplden",
                             "faratio", "sodden", "rgden", "sofa_perc")
    indat[,colnms]
}
yikeshu0611/nhanesR documentation built on Jan. 29, 2022, 6:08 a.m.