[R] AHRQ - Creation of Comorbidity Variables

GL pflugg at shands.ufl.edu
Tue Sep 7 18:28:05 CEST 2010


If there are any other users who use AHRQ's SAS code comoanaly2010 and
comformat2010 to create comorbidity variables, I thought you might be
interested in the following PRELIM code we wrote to mimic its functionality
in R. It seems to yield similar results, but may contain errors. Please feel
free to comment (kindly) or enhance. I'm sure there are better ways to skin
this cat, but we at least took a stab at it. Thought this would be a good
use of the community if there are any other interested users. 


################################################################################
# Function flag 
#
# Intended to provide functionality from AHRQ comformat2010 comoanaly2010
#
# Input, dataframe with
#   id in column 1
#   msdrg in column 2
#   diagnosis in columns 4-53
# Output, numeriuc list with id and one element per cc
# dimnames = c('ID',
#     'CHF','VALVE','PULMCIRC','PERIVASC',
#     'HTN_C','PARA','NEURO','CHRNLUNG','DM',
#     'DMCX','HYPOTHY','RENLFAIL','LIVER','ULCER',
#     'AIDS','LYMPH','METS','TUMOR','ARTH',
#     'ANEMDEF','ALCOHOL','DRUG','PSYCH','DEPRESS') )

flag = function(data, k) {
    data = data[k, ]
    print(data)
    print(k)
    id = as.matrix(data[1])
    DX = data[4:53]
    DX = as.matrix(DX)
    DRG = as.matrix(data[2])
   
##########format############################################################
    chf = c(39891, 4280:4289, 42800:42889)
    v1 = paste(0, 9320:9324, sep = "")
    v5 = paste("V422", "", sep = "")
    v6 = paste("V433", "", sep = "")
    valve = c(v1, 3940:3971, 39400:39709, 3979, 4240:4249, 42400:42499, 
        7463:7466, 74630:74659, v5, v6)
    pulmcirc = c(41511:41519, 4160:4169, 41600:41689, 4179)
    p3 = paste(c(4471, 5571, 5579, "V434"), "", sep = "")
    perivasc = c(4400:4409, 44000:44089, 4411:4419, 44100:44189, 
        4420:4429, 44200:44289, 4431:4439, 44310:44389, 44421:44422, 
        p3, 449)
    htn = c(4011, 4019, 64200:64204)
    htncx = c(4010, 4372)
   
############################################################################
    #  the following are special, temporary formats used in the creation of
the 
    #  hypertension complicated comorbidity when overlapping with congestive 
    #  heart failure or renal failure occurs. These temporary formats are 
    #  referenced in the program called comoanaly2009.txt
   
############################################################################
    htnpreg = c(64220:64224)
    htnwochf = c(40200, 40210, 40290, 40509, 40519, 40599)
    htnwchf = c(40201, 40211, 40291)
    hrenworf = c(40300, 40310, 40390, 40501, 40511, 40591, 64210:64214)
    hrenwrf = c(40301, 40311, 40391)
    hhrwohrf = c(40400, 40410, 40490)
    hhrwchf = c(40401, 40411, 40491)
    hhrwrf = c(40402, 40412, 40492)
    hhrwhrf = c(40403, 40413, 40493)
    ohtnpreg = c(64270:64274, 64290:64294)
   
############################################################################
    para = c(3420:3449, 34200:34489, 43820:43853, 78072)
    neuro = c(3300:3319, 33000:33189, 3340:3359, 33400:33589, 
        3411:3419, 34110:34189, 3452:3453, 34520:34529, 3320, 
        3334, 3335, 3337, 3380, 7687, 7803, 7843, 340, 33371, 
        33372, 33379, 33385, 33394, 34500:34511, 34540:34591, 
        34700:34701, 34710:34711, 64940:64944, 76870:76873, 78031, 
        78032, 78039, 78097)
    chrnlung = c(490:492, 4900:4928, 49000:49279, 49300:49392, 
        494, 4940:4941, 49400:49409, 496:505, 4950:5049, 49500:50499, 
        5064)
    dm = c(25000:25033, 64800:64804, 24900:24931)
    dmcx = c(25040:25093, 7751, 24940:24991)
    hypothy = c(243:244, 2430:2442, 24300:24419, 2448, 2449)
    renlfail3 = paste(c("V420", "V451", "V568"), "", sep = "")
    renlfail4 = paste("V", c(4511:4512), sep = "")
    renlfail5 = paste("V", c(560:563, 5600:5632), sep = "")
    renlfail = c(5853:5856, 5859, 586, renlfail3, renlfail4, 
        renlfail5)
    liver1 = paste(0, c(7022, 7023, 7032, 7033, 7044, 7054), 
        sep = "")
    liver = c(liver1, 4560, 4561, 45620, 45621, 5710, 5712, 5713, 
        57140:57149, 5715:5716, 5718:5719, 5723, 5728, "V427")
    ulcer1 = paste(531, c(41, 51, 61, 70, 71, 91), sep = "")
    ulcer2 = paste(532, c(41, 51, 61, 70, 71, 91), sep = "")
    ulcer3 = paste(533, c(41, 51, 61, 70, 71, 91), sep = "")
    ulcer4 = paste(534, c(41, 51, 61, 70, 71, 91), sep = "")
    ulcer = c(ulcer1, ulcer2, ulcer3, ulcer4)
    aids = paste(0, c(42:44, 420:449, 4200:4289), sep = "")
    lymph = c(20000:20238, 20250:20301, 20302:20382, 2386, 2733)
    mets = c(1960:1991, 19600:19909, 20970:20975, 20979, 78951)
    tumor = c(1400:1729, 1740:1759, 14000:17289, 17400:17589, 
        20900:20924, 20931:20936, 25801:25803, 2093, 20925:20929, 
        179:195, 1790:1958, 17900:19579)
    arth = c(7010, 7100:7109, 7140:7149, 7200:7209, 71000:71089, 
        71400:71489, 72000:72089, 725)
    c1 = paste(c(2860:2869, 2871, 2873:2875), "", sep = "")
    coag = c(2860:2869, 2871, 2873:2875, 28600:28689, 28730:28749, 
        64930:64934, 28984)
    ob3 = paste("V", c(8530:8549, 8554), sep = "")
    obese = c(2780, "V854", ob3, 27800:27801, 64910:64914, 79391)
    wghtloss = c(260:263, 2600:2639, 26000:26389, 78321:78322)
    lytes = c(2760:2769, 27600:27689)
    bldloss = c(2800, 64820:64824)
    anemdef = c(2801:2819, 2859, 28010:28189, 28521:28529)
    alcohol = c(2910:2913, 2915, 2918, 2919, 29100:29129, 29181, 
        29182, 29189, 30300:30393, 30500:30503)
    drug = c(2920, 2929, 29282:29289, 30400:30493, 30520:30593, 
        64830:64834)
    psych = c(29500:29889, 2951:2989, 29910, 29911)
    depress = c(3004, 3090, 3091, 311, 30112)
    other = c("?")
    ############## V27 MS-DRG
##################################################
    card1 = paste(0, 1:2, sep = "")
    card2 = paste(0, card1, sep = "")
    carddrg = c(card2, 215:238, 242:251, 253:254, 258:262, 265, 
        280:293, 296:298, 302:303, 306:313)
    peridrg = c(299:301)
    renaldrg = c(652, 656:661, 673:675, 682:700)
    nerv1 = paste(0, c(20:42, 52:99), sep = "")
    nervdrg = c(nerv1, 100:103)
    ceredrg = paste(0, c(20:22, 34:38, 64:72), sep = "")
    pulmdrg = c(190:192, 202, 203)
    diabdrg = c(637:639)
    hypodrg = c(625:627, 643:645)
    renfdrg = c(652, 682:685)
    liverdrg = c(420:425, 432:434, 441:446)
    ulcedrg = c(377:384)
    hivdrg = c(969:970, 974:977)
    leukdrg = c(820:830, 834:849)
    canc1 = paste(0, 54:55, sep = "")
    cancdrg = c(canc1, 146:148, 180:182, 374:376, 435:437, 542:544, 
        582:585, 597:599, 656:658, 686:688, 715:716, 722:724, 
        736:741, 754:756, 826:830, 843:849)
    arthdrg = c(545:547)
    nutrdrg = c(640:641)
    anemdrg = c(808:812)
    alcdrg = c(894:897)
    coagdrg = c(813)
    htncxdrg = c(304, paste(0, 77:78, sep = ""))
    htndrg = c(305, paste(0, 79, sep = ""))
    psydrg = c(885)
    obesedrg = c(619:621)
    deprsdrg = c(881)
    
    ### FLAG
###################################################################
    flag = matrix(0, 1, 30)
    tmp = matrix(0, 1, 10)
    for (i in 2:50) {
        flag[1, 1] = pmax(flag[1, 1], as.numeric(c(DX[1, i] %in% 
            chf)))
        flag[1, 2] = pmax(flag[1, 2], as.numeric(c(DX[1, i] %in% 
            valve)))
        flag[1, 3] = pmax(flag[1, 3], as.numeric(c(DX[1, i] %in% 
            pulmcirc)))
        flag[1, 4] = pmax(flag[1, 4], as.numeric(c(DX[1, i] %in% 
            perivasc)))
        flag[1, 5] = pmax(flag[1, 5], as.numeric(c(DX[1, i] %in% 
            htn)))
        flag[1, 6] = pmax(flag[1, 6], as.numeric(c(DX[1, i] %in% 
            htncx)))
        flag[1, 7] = pmax(flag[1, 7], as.numeric(c(DX[1, i] %in% 
            para)))
        flag[1, 8] = pmax(flag[1, 8], as.numeric(c(DX[1, i] %in% 
            neuro)))
        flag[1, 9] = pmax(flag[1, 9], as.numeric(c(DX[1, i] %in% 
            chrnlung)))
        flag[1, 10] = pmax(flag[1, 10], as.numeric(c(DX[1, i] %in% 
            dm)))
        flag[1, 11] = pmax(flag[1, 11], as.numeric(c(DX[1, i] %in% 
            dmcx)))
        flag[1, 12] = pmax(flag[1, 12], as.numeric(c(DX[1, i] %in% 
            hypothy)))
        flag[1, 13] = pmax(flag[1, 13], as.numeric(c(DX[1, i] %in% 
            renlfail)))
        flag[1, 14] = pmax(flag[1, 14], as.numeric(c(DX[1, i] %in% 
            liver)))
        flag[1, 15] = pmax(flag[1, 15], as.numeric(c(DX[1, i] %in% 
            ulcer)))
        flag[1, 16] = pmax(flag[1, 16], as.numeric(c(DX[1, i] %in% 
            aids)))
        flag[1, 17] = pmax(flag[1, 17], as.numeric(c(DX[1, i] %in% 
            lymph)))
        flag[1, 18] = pmax(flag[1, 18], as.numeric(c(DX[1, i] %in% 
            mets)))
        flag[1, 19] = pmax(flag[1, 19], as.numeric(c(DX[1, i] %in% 
            tumor)))
        flag[1, 20] = pmax(flag[1, 20], as.numeric(c(DX[1, i] %in% 
            arth)))
        flag[1, 21] = pmax(flag[1, 21], as.numeric(c(DX[1, i] %in% 
            coag)))
        flag[1, 22] = pmax(flag[1, 22], as.numeric(c(DX[1, i] %in% 
            obese)))
        flag[1, 23] = pmax(flag[1, 23], as.numeric(c(DX[1, i] %in% 
            wghtloss)))
        flag[1, 24] = pmax(flag[1, 24], as.numeric(c(DX[1, i] %in% 
            lytes)))
        flag[1, 25] = pmax(flag[1, 25], as.numeric(c(DX[1, i] %in% 
            bldloss)))
        flag[1, 26] = pmax(flag[1, 26], as.numeric(c(DX[1, i] %in% 
            anemdef)))
        flag[1, 27] = pmax(flag[1, 27], as.numeric(c(DX[1, i] %in% 
            alcohol)))
        flag[1, 28] = pmax(flag[1, 28], as.numeric(c(DX[1, i] %in% 
            drug)))
        flag[1, 29] = pmax(flag[1, 29], as.numeric(c(DX[1, i] %in% 
            psych)))
        flag[1, 30] = pmax(flag[1, 30], as.numeric(c(DX[1, i] %in% 
            depress)))
            
        ##### TMP
##############################################################
        tmp[1, 1] = pmax(tmp[1, 1], as.numeric(c(DX[1, i] %in% 
            htnpreg)))
        tmp[1, 2] = pmax(tmp[1, 2], as.numeric(c(DX[1, i] %in% 
            htnwochf)))
        tmp[1, 3] = pmax(tmp[1, 3], as.numeric(c(DX[1, i] %in% 
            htnwchf)))
        tmp[1, 4] = pmax(tmp[1, 4], as.numeric(c(DX[1, i] %in% 
            hrenworf)))
        tmp[1, 5] = pmax(tmp[1, 5], as.numeric(c(DX[1, i] %in% 
            hrenwrf)))
        tmp[1, 6] = pmax(tmp[1, 6], as.numeric(c(DX[1, i] %in% 
            hhrwohrf)))
        tmp[1, 7] = pmax(tmp[1, 7], as.numeric(c(DX[1, i] %in% 
            hhrwchf)))
        tmp[1, 8] = pmax(tmp[1, 8], as.numeric(c(DX[1, i] %in% 
            hhrwrf)))
        tmp[1, 9] = pmax(tmp[1, 9], as.numeric(c(DX[1, i] %in% 
            hhrwhrf)))
        tmp[1, 10] = pmax(tmp[1, 10], as.numeric(c(DX[1, i] %in% 
            ohtnpreg)))
    }
    ###### DRG
#################################################################
    drg = matrix(0, nrow(DRG), 24)
    drg[1, 1] = as.numeric(c(DRG %in% carddrg))
    drg[1, 2] = as.numeric(c(DRG %in% peridrg))
    drg[1, 3] = as.numeric(c(DRG %in% ceredrg))
    drg[1, 4] = as.numeric(c(DRG %in% nervdrg))
    drg[1, 5] = as.numeric(c(DRG %in% pulmdrg))
    drg[1, 6] = as.numeric(c(DRG %in% diabdrg))
    drg[1, 7] = as.numeric(c(DRG %in% hypodrg))
    drg[1, 8] = as.numeric(c(DRG %in% renaldrg))
    drg[1, 9] = as.numeric(c(DRG %in% renfdrg))
    drg[1, 10] = as.numeric(c(DRG %in% liverdrg))
    drg[1, 11] = as.numeric(c(DRG %in% ulcedrg))
    drg[1, 12] = as.numeric(c(DRG %in% hivdrg))
    drg[1, 13] = as.numeric(c(DRG %in% leukdrg))
    drg[1, 14] = as.numeric(c(DRG %in% cancdrg))
    drg[1, 15] = as.numeric(c(DRG %in% arthdrg))
    drg[1, 16] = as.numeric(c(DRG %in% nutrdrg))
    drg[1, 17] = as.numeric(c(DRG %in% anemdrg))
    drg[1, 18] = as.numeric(c(DRG %in% alcdrg))
    drg[1, 19] = as.numeric(c(DRG %in% htncxdrg))
    drg[1, 20] = as.numeric(c(DRG %in% htndrg))
    drg[1, 21] = as.numeric(c(DRG %in% coagdrg))
    drg[1, 22] = as.numeric(c(DRG %in% psydrg))
    drg[1, 23] = as.numeric(c(DRG %in% obesedrg))
    drg[1, 24] = as.numeric(c(DRG %in% deprsdrg))
   
############################################################################
    for (i in 1:nrow(drg)) {
        if (tmp[1, 1] == 1 || tmp[1, 2] == 1 || tmp[1, 4] == 
            1 || tmp[1, 6] == 1) 
            flag[1, 6] = 1
        if (tmp[1, 3] == 1 || tmp[1, 7] == 1) {
            flag[1, 6] = 1
            flag[1, 1] = 1
        }
        if (tmp[1, 5] == 1 || tmp[1, 8] == 1) {
            flag[1, 6] = 1
            flag[1, 13] = 1
        }
        if (tmp[1, 9] == 1) {
            flag[1, 6] = 1
            flag[1, 1] = 1
            flag[1, 13] = 1
        }
        if (tmp[1, 10] == 1) {
            flag[1, 6] = 1
        }
       
########################################################################
        #set up code to only count the more severe comorbidity
       
########################################################################
        if (flag[1, 6] == 1) 
            flag[1, 5] = 0
        if (flag[1, 18] == 1) 
            flag[1, 19] = 0
        if (flag[1, 11] == 1) 
            flag[1, 10] = 0
       
########################################################################
        # redefining comorbidities by eliminating the DRG directly related 
        # to comorbidity, thus limiting the screens to principal
        # diagnoses not directly related to comorbidity in question
       
########################################################################
        if (flag[1, 1] == 1 && drg[1, 1] == 1) 
            flag[1, 1] = 0
        if (flag[1, 2] == 1 && drg[1, 1] == 1) 
            flag[1, 2] = 0
        if (flag[1, 3] == 1 && max(drg[1, 1], drg[1, 5]) == 1) 
            flag[1, 3] = 0
        if (flag[1, 4] == 1 && drg[1, 2] == 1) 
            flag[1, 4] = 0
        if (flag[1, 5] == 1 && drg[1, 20] == 1) 
            flag[1, 5] = 0
       
########################################################################
        # apply DRG exclusions to hypertension complicated, congestive heart 
        # failure, and renal failure comorbidities using the detailed
        # hypertension flags created above
       
########################################################################
        if (flag[1, 6] == 1 && drg[1, 19] == 1) 
            flag[1, 6] = 0
        if (tmp[1, 1] == 1 && drg[1, 19] == 1) 
            flag[1, 6] = 0
        if (tmp[1, 2] == 1 && max(drg[1, 1], drg[1, 19]) == 1) 
            flag[1, 6] = 0
        if (tmp[1, 3] == 1 && drg[1, 19] == 1) 
            flag[1, 6] = 0
        if (tmp[1, 3] == 1 && drg[1, 1] == 1) {
            flag[1, 6] = 0
            flag[1, 1] = 0
        }
        if (tmp[1, 4] == 1 && max(drg[1, 19], drg[1, 8]) == 1) 
            flag[1, 6] = 0
        if (tmp[1, 5] == 1 && drg[1, 19] == 1) {
            flag[1, 6] = 0
        }
        if (tmp[1, 5] == 1 && drg[1, 8] == 1) {
            flag[1, 6] = 0
            flag[1, 13] = 0
        }
        if (tmp[1, 6] == 1 && max(drg[1, 19], drg[1, 1], drg[1, 
            8]) == 1) 
            flag[1, 6] = 0
        if (tmp[1, 7] == 1 && drg[1, 19] == 1) 
            flag[1, 6] = 0
        if (tmp[1, 7] == 1 && drg[1, 1] == 1) {
            flag[1, 6] = 0
            flag[1, 1] = 0
        }
        if (tmp[1, 7] == 1 && drg[1, 8] == 1) {
            flag[1, 6] = 0
        }
        if (tmp[1, 8] == 1 && max(drg[1, 1], drg[1, 19]) == 1) 
            flag[1, 6] = 0
        if (tmp[1, 8] == 1 && drg[1, 8] == 1) {
            flag[1, 6] = 0
            flag[1, 13] = 0
        }
        if (tmp[1, 9] == 1 && drg[1, 19] == 1) {
            flag[1, 6] = 0
        }
        if (tmp[1, 9] == 1 && drg[1, 1] == 1) {
            flag[1, 6] = 0
            flag[1, 1] = 0
        }
        if (tmp[1, 9] == 1 && drg[1, 8] == 1) {
            flag[1, 6] = 0
            flag[1, 13] = 0
        }
        if (tmp[1, 10] == 1 && max(drg[1, 19], drg[1, 1], drg[1, 
            8]) == 1) 
            flag[1, 6] = 0
        
        for (j in c(7:10)) {
            if (flag[1, j] == 1 && drg[1, j - 4] == 1) 
                flag[1, j] = 0
        }
        for (j in c(11:12)) {
            if (flag[1, j] == 1 && drg[1, j - 5] == 1) 
                flag[1, j] = 0
        }
        for (j in c(13:18)) {
            if (flag[1, j] == 1 && drg[1, j - 4] == 1) 
                flag[1, j] = 0
        }
        for (j in c(19:20)) {
            if (flag[1, j] == 1 && drg[1, j - 5] == 1) 
                flag[1, j] = 0
        }
        
        if (flag[1, 21] == 1 && drg[1, 21] == 1) 
            flag[1, 21] = 0
        if (flag[1, 22] == 1 && max(drg[1, 16], drg[1, 23]) == 
            1) 
            flag[1, 22] = 0
        if (flag[1, 23] == 1 && drg[1, 16] == 1) 
            flag[1, 23] = 0
        if (flag[1, 24] == 1 && drg[1, 16] == 1) 
            flag[1, 24] = 0
        if (flag[1, 25] == 1 && drg[1, 17] == 1) 
            flag[1, 25] = 0
        if (flag[1, 26] == 1 && drg[1, 17] == 1) 
            flag[1, 26] = 0
        if (flag[1, 27] == 1 && drg[1, 18] == 1) 
            flag[1, 27] = 0
        if (flag[1, 28] == 1 && drg[1, 18] == 1) 
            flag[1, 28] = 0
        if (flag[1, 29] == 1 && drg[1, 22] == 1) 
            flag[1, 29] = 0
        if (flag[1, 30] == 1 && drg[1, 24] == 1) 
            flag[1, 30] = 0
       
########################################################################
        # combine HTN and HTNCX into HTN_C
       
########################################################################
        flag[1, 5] = max(flag[1, 5], flag[1, 6])
    }
    flag = flag[1, c(1:5, 7:30)]
    # final=matrix(, nrow(flag), 30 ,
    #       dimnames = c('ID',
    #                            'CHF','VALVE','PULMCIRC','PERIVASC',
    #                            'HTN_C','PARA','NEURO','CHRNLUNG','DM',
    #                           
'DMCX','HYPOTHY','RENLFAIL','LIVER','ULCER',
    #                            'AIDS','LYMPH','METS','TUMOR','ARTH',
    #                           
'COAG','OBESE','WGHTLOSS','LYTES','BLDLOSS',
    #                           
'ANEMDEF','ALCOHOL','DRUG','PSYCH','DEPRESS') )
    return(flag)
}



-- 
View this message in context: http://r.789695.n4.nabble.com/AHRQ-Creation-of-Comorbidity-Variables-tp2529997p2529997.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list