demo_analysis/2_medical_history.R

# Load package
library(OHDSIDeriveVariables)

####### Setup environment ###########
cdmDatabaseSchema <- 'cdm5'
cohortDatabaseSchema <- 'study'
cohortTableName <- 'masterfile'

connectionDetails <- createConnectionDetails(dbms="postgresql",
                                             server="localhost/ohdsi",
                                             user="postgres", password="",
                                             port=5433,
                                             schema=cdmDatabaseSchema)

# Get the study cohort as a dataframe (studyPop)
cohort <- getCohort(connectionDetails, cdmDatabaseSchema, cohortDatabaseSchema, cohortTableName)
attach(cohort)

########### Medical History ##############
## History of stroke (I63)
stroke_ids <- c(443454,443790,443864,444091,4031045,4043731,4043732,4045735,4045737,4045738,4045740,4045741,4046237,4046358,4046359,4046360,4046361,4046362,4048784,4077086,4108356,4110189,4110190,4110192,4111714,4112026,4119140,4129534,4131383,4138327,4141405,4142739,4145897,4146185,4319146,40479572,43530683,43531607,44782773,45767658,45772786,46270031,46270380,46270381,46273649)
stroke_history <- getConditionHistory(stroke_ids,
                                      connectionDetails,
                                      target_schema = cohortDatabaseSchema,
                                      target_table = cohortTableName)
# Create contigency table
stroke_contigency <- table(stroke_history, INDEX_DRUG_STRING)
# Show contigency table as percentage of total number of riva/vka patients
prop.table(stroke_contigency, margin = 2)
# Perform a Chi Square test
chisq.test(stroke_contigency, correct = FALSE)

## Bleeding
# R can't handle creating this vector. TODO: load these ids from a file?
bleed_ids <- c(42873045,44806343,79146,4299540,4154797,4292531,4032887,4301129,4164920,43022013,4137430,4095410,441259,437388,4096761,4094842,46269907,4345688,4178406,4095942,26441,4065618,4095261,194093,4084844,4242720,44784542,42709943,4318829,4144946,4341796,4266066,433515,4129155,4025198,4172545,4152629,44809112,4334127,4217947,4091199,4108678,4009167,4242106,4175673,433778,4327745,436204,46270145,4124844,442783,437922,45773167,4129546,4048607,4088926,4055219,4216478,4307826,4307661,432625,440869,43021405,4316366,435378,4236844,435391,432904,4213652,4167030,46269911,4220776,438478,442547,4320020,4308202,434402,374025,4344255,4221119,4319328,45766652,201366,4311406,4147276,4095275,4105109,4111708,22665,380113,4168060,4199903,4207217,4232237,46269863,4071589,4341253,439944,4181933,4170068,4314000,44784414,45770895,4228479,4215879,45757570,4115355,4048194,44784367,4051612,4225959,4071732,4249570,43531486,436553,42709870,4095555,4119616,4176148,4049466,45757810,4094980,42709923,46269902,4099014,4091380,442190,4055720,4306943,4094023,4150060,42873109,4139411,4247776,4131053,433052,27026,43530909,4047731,4049270,432923,4328086,4084272,440561,4043737,443133,435104,4329097,373347,4123077,4126908,4263089,438796,4334260,4204900,438814,4162754,4095563,4201300,4336230,4204040,4056605,4120621,44784545,46269841,4297900,43021073,44783437,75345,442796,72403,438595,4090259,4127410,42873046,46269822,4088442,4027161,44807159,4129535,438259,197925,434273,4129158,40478937,4270650,4317284,4082618,46273491,4095561,443530,4260059,4095878,434789,4048602,4235591,4006327,4099747,4126909,197349,42873041,4179773,44782484,4115357,444197,4051752,45757241,4178942,4201094,4195045,4145901,4180610,4077200,46272492,192671,75043,4207171,442102,439171,4088113,435358,195845,433051,45757783,4071717,4047863,4121633,4342882,437106,4299377,4031282,4096013,45766118,4048278,46273182,437323,196751,44782988,44784546,4144926,440244,4295428,46269910,4214947,4189259,440583,4096017,4215480,4099658,4323618,4100838,195958,42709922,4107277,4297911,46269819,261433,4161407,4341777,257375,4205670,4079972,373501,4114486,4178726,4244406,40488840,46269935,4167918,4134808,4046364,438214,4095877,198467,4038924,4291461,434166,4034092,4096645,4096219,24397,436136,4017105,4300760,437242,4171108,4059604,4218781,200562,4155624,4217209,44784603,432366,4071068,194516,4187333,4083969,4247081,46269877,4326561,193249,4171240,4336971,45768629,4300127,37017911,45766119,4188154,4117716,4274491,23245,4173032,4102873,4234757,42873110,42709925,4149586,4136545,46269908,4032901,193535,4058524,4048174,4131525,4232607,4171119,4154461,4120107,4176892,4095572,4091379,4006994,4003676,4282300,4161203,4218081,4340509,437935,42872427,435959,4169412,4039449,4032430,439949,4096032,4079973,4048606,433899,4169592,438285,4100660,436519,4145805,4256233,76154,199887,4088773,42872436,4201028,40485019,4082617,4110186,4195068,4126713,4335983,44783206,45757208,4230266,4154566,45766068,4048460,4345816,4079847,4290251,442314,4045745,46269912,4058527,4332645,4341083,135370,4087310,4048172,4273101,46270025,438681,4094839,4096343,44784282,4161205,4151359,4273219,4134162,4265205,4300126,44784543,441328,46269847,4256114,258449,45757247,443929,4094958,4210115,4129031,4174044,4078936,4096495,4202511,196757,432474,4231580,4129033,4116316,4107340,4294426,4298842,4347548,4204815,440870,4235740,46269851,4062451,432346,4104922,45770898,4125656,4289830,4019285,4178738,4344137,438468,4040456,44784544,434756,4159966,24076,434089,4263072,4318535,437312,4265196,37016882,4046341,439724,437413,4204041,4154699,4110661,437027,4121632,4078448,4009587,42873124,42872438,437867,44782463,4048277,78513,42873157,440250,4126076,4196275,193271,4343234,4129545,4112946,193250,4166847,4202535,197024,4124866,43530674,4103011,432743,4096984,4234389,4232181,4320492,4342905,4121664,4235223,4065644,4101602,4091198,4077828,4219594,42873125,4033857,46270359,4059114,42873043,4126907,4243309,4119134,4226025,42709832,441087,4285805,4120805,4046365,4227086,4103390,4047864,44797721,4051335,438596,4082042,4040450,4082774,4036966,4115356,4048603,4185784,4121653,4258539,4096001,46269837,4048286,4248080,44806407,4009796,4301613,40483641,4137219,46269906,46273478,4048133,4062417,46269887,4295334,4298855,440756,4145107,4330466,4318408,194690,193795,4065747,194986,4107213,439130,4216970,443752,197018,4336164,256036,4185268,4090901,4169954,4318539,4311252,437326,4136546,4128705,4144945,194109,4090247,441432,4045744,4317281,43531483,4334749,40491822,440531,4343220,4196276,4078317,4062150,443624,26727,4249574,4296604,4218280,46269901,4185539,4054952,201885,42873044,4117890,4048283,320739,4324264,45773077,4115149,4112018,79915,4144374,4310668,316457,4126115,4211001,434275,4316205,4121266,4175167,441063,4142581,4120943,4048484,4155517,4094983,4126906,37017451,4161830,4055636,197625,4092677,37016887,432752,4049659,4050203,4243452,44784425,4298750,4140545,4151132,4244349,4060178,4173076,4147587,441062,4127414,4121636,198212,435141,255105,30770,4111709,4206466,201368,4095570,435964,4047852,4078446,4048173,195876,4110185,4117717,37016888,4085234,4048279,4135466,4173897,4301024,443779,4184738,4310314,4173481,4171123,4078628,4048889,315276,433037,4147995,4094972,437133,435855,436148,432764,4169394,4245614,37016889,439182,4284824,4069838,197134,4028488,4156625,4077958,4106058,4226021,260841,4144154,4208209,4117720,4006156,4240364,4149010,4080892,4127415,4143074,4096626,435254,4247596,4156628,4341790,4196713,4289307,436529,4117889,4154731,4062416,201627,4083118,4300128,4027729,4129157,42872437,438279,4043254,44784547,4084787,439194,4096644,4171122,432478,4135626,4098289,4318833,37016886,438894,380650,4030212,4035167,4252697,40493244,45757654,439950,4129037,444198,28779,4294427,4209571,4324880,45766653,4266523,4146007,434785,4124845,40482451,4048179,4152062,435672,4220445,4145439,43530851,46271801,76784,4078447,4154564,4230680,4252864,4162439,438489,46273470,37017589,200455,37016883,42873126,4077827,4040280,433624,4063086,4135629,4341252,4183593,194158,42873122,252477,4091359,4338544,195737,4130539,4336012,4134158,4231825,4242238,4256238,4121267,46272978,4072086,4184484,4002659,4148906,4129828,4143871,4227291,4071070,132584,4169890,4100339,4012540,4047732,4085415,4264487,4246979,45772943,318556,4124469,4177600,46270529,4136541,443925,436430,4241331,436557,4209304,4111119,200149,438203,4071069,4071203,4152061,46271323,40490492,45757543,436841,4095094,4131786,40488343,4077959,4222477,4124679,4117719,4014781,4096556,4051478,4180743,197711,44794467,4071599,4061223,44784432,4164351,4263648,4126712,40492969,4121635,4006464,4199890,4096916,437021,199068,198260,4106997,40488379,4056046,4088092,4203885,4049502,4031035,4307825,4206906,4149692,4154569,4070523,440537,4129544,4310674,4309836,4177184,4117715,4319445,4304254,201435,4154597,4174299,4155911,4030995,4342904,4084815,442341,45766120,77640,4115699,439932,4094979,4310298,193264,441985,436258,256556,46269892,4334887,4234376,261687,4058526,42873123,4347554,4231740,4291649,4318684,4202474,4205675,4109072,4203009,4096781,4164379,443452,4164350,4096682,4155800,23237,4271696,4096627,4155509,4049224,4026112,4056607,42873042,4301602,441940,196488,4091358,4317980,4096706,4029156,439040,376713,4199665,4125012,4183005,4095876,4189651,4299560,4017107,4091382,4335737,198195,4119169,436729,45766830,4009926,4105125,4045743,432734,4299449,4144804,4301275,46269893,44782730,4307580,4151242,43530671,4048281,4228297,4308009,441210,4115998,432475,434508,42709831,4062807,4078535,4117718,4096773,4042409,381440,4201635,4216673,4174885,4061189,4001326,4173004,435938,4152492,4320635,4095178,4199413,4195179,4123284,46273183,42872434,4301606,4127844,4045746,373205,442539,4231820,4193496,438873,40490914,433882,441709,4124695,373057,436842,45757074,42709921,4121268,4328027,4102469,46269905,4036965,4307438,4299415,433050,196442,443245,441011,4294973,4129827,4318553,4094840,4048152,4343915,4041707,433623,378756,4034091,4112019,4028773,201441,4048601,442533,43530727,42709924,4322061,4139778,46270358,4117891,4209297,4223979,4077201,435390,4084431,436526,4294259,439847,4028339,44801577,37016885,378768,4261336,4125655,4302474,4080436,433058,4341776,46269821,4199815,4067764,45772779,4164343,4108952,4336003,195321,78833,46269882,4129156,4216866,4101065,4331608,46270111,44784111,435960,4258261,4234260,4223239,195289,40479853,4152155,4091378,4060973,4196670,435323,4112156,318397,375539,4247008,4087649,37016924,4115999,4264629,4260189,4129032,4198840,4145316,4170380,4096023,4338977,4060934,376419,4208719,198221,4273874,4121850,4342987,440236,4095943,4308716,4206643,43531608,4070534,444196,4056757,4233296,4080556,434155,201043,46269953,4083478,4177495,4318837,4256889,195012,46269904,4223856,438003,435931,37016372,4033350,4121303,4299088,46270360,438925,4343214,4100839,4002836,4071066,256844,4094684,4249692,437211,4294425,4046500,4143460,4236721,46269891,4155510,4136550,4279640,4031954,31335,4308835,4129290,440251,441158,198801,4260196,4083209,4217836,4119597,199855,4247597,197617,4028772,4325373,4035178,4096494,4094971,440327,260245,4173332,43530728
               )
bleeding_history <- getConditionHistory( bleed_ids,
                                            connectionDetails,
                                            target_schema = cohortDatabaseSchema,
                                            target_table = cohortTableName)
bleed_contigency <- table(bleeding_history, INDEX_DRUG_STRING)
print("Bleeding history")
prop.table(bleed_contigency, margin=2)
chisq.test(bleed_contigency, correct = FALSE)

## Diabetes diagnosis BEFORE index.
diabetes_ids <- c(195771,201254,201820,201826,4058243,44793114,4193704)
diabetes_history <- getConditionHistory(diabetes_ids,
                                          connectionDetails,
                                          target_schema = cohortDatabaseSchema,
                                          target_table = cohortTableName)
diabetes_contigency <- table(diabetes_history, INDEX_DRUG_STRING)
print("Diabetes history")
prop.table(diabetes_contigency, margin = 2)
chisq.test(diabetes_contigency, correct = FALSE)

######## Drug History ########
## ACE inhibitors
# ATC codes with wildcard '%'. "C09AA%" matches all codes starting with "C09AA"
ace_ids <- c("C09AA%","C09B%")
ace_history <- getDrugHistoryByATC( ace_ids, connectionDetails, cohortDatabaseSchema, cohortTableName )

ace_contigency <-  table(ace_history, INDEX_DRUG_STRING)
print("ACE-inhibitor")
prop.table(ace_contigency, margin = 2)
chisq.test(ace_contigency, correct = FALSE)


###### Starting new medication #########
ace_ids <- c("C09AA%","C09B%")
ace_atIndex <- getDrugAtIndexByATC( ace_ids, connectionDetails, cohortDatabaseSchema, cohortTableName )

ace_contigency <-  table(ace_atIndex, INDEX_DRUG_STRING)
print("ACE-inhibitor At Index")
prop.table(ace_contigency)
chisq.test(ace_contigency, correct = FALSE)
thehyve/ohdsi-DeriveVariables documentation built on May 31, 2019, 9:13 a.m.