├── test.txt ├── pubs ├── README.md ├── figures │ └── README.md ├── cinc2014-abstract.txt └── dynamic_features.txt ├── sql ├── README.md ├── treatments.sql ├── SevereSepsisDefinitionAngus2001.sql ├── cohort.sql ├── SevereSepsisDefinitionMartin2003.sql ├── extract_main_table.sql ├── labs_var_raw.sql ├── static_features.sql ├── li_raw_saps_variables.sql ├── lab_values.sql ├── ards_data_extraction_may13.sql ├── physiological_var.sql └── sepsis_data_jan14_lab_vitalSigns_coomorbidities.sql ├── R ├── README.md └── GA_echo.R ├── README.md ├── LICENSE └── matlab ├── evalModel.m ├── gene2model.m └── geneticalgorithm.m /test.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /pubs/README.md: -------------------------------------------------------------------------------- 1 | #Documents 2 | 3 | ##LaTex 4 | 5 | 6 | -------------------------------------------------------------------------------- /sql/README.md: -------------------------------------------------------------------------------- 1 | #SQL (Oracle) 2 | 3 | ##Cohort 4 | 5 | ##Lab parameters 6 | 7 | ##Vital Signs 8 | -------------------------------------------------------------------------------- /sql/treatments.sql: -------------------------------------------------------------------------------- 1 | 2 | create materialized view mimic2v26_48hr_vitals_labs_treatments as 3 | 4 | with -------------------------------------------------------------------------------- /pubs/figures/README.md: -------------------------------------------------------------------------------- 1 | #Pubs# 2 | 3 | ##Figures 4 | 5 | A collection of analysis and figures for the GA-GMM project 6 | 7 | 8 | -------------------------------------------------------------------------------- /sql/SevereSepsisDefinitionAngus2001.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MIT-LCP/ga-gmm/master/sql/SevereSepsisDefinitionAngus2001.sql -------------------------------------------------------------------------------- /R/README.md: -------------------------------------------------------------------------------- 1 | #Code Overview 2 | 3 | GA_echo.r ga implementation to select best subset of features based on AUC 4 | data_2.csv test data for GA_echo.r 5 | -------------------------------------------------------------------------------- /sql/cohort.sql: -------------------------------------------------------------------------------- 1 | with cohort as ( 2 | select cd.icustay_id 3 | , ca.* 4 | , cd.icustay_intime 5 | from MIMIC2V26.icustay_detail cd 6 | on cd.subject_id=ca.subject_id 7 | and cd.hadm_id=ca.hadm_id 8 | ) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GA-GMM 2 | ====== 3 | 4 | Identify predictive parameters for 28-day mortality using a genetic algorithm (GA) and Gaussian mixture models (GMM). 5 | 6 | ##Design 7 | Identify predictive variables for patient cohort using a unsupervised data drive process. 8 | 9 | ##Cohort 10 | All adult ICU admissions using MIMIC (http://mimic.physionet.org) 11 | 12 | ##Methods/Software 13 | SQL (Oracle) to extract data 14 | R to run Genetic Algorithm (GA) and Expectation-Maximization (EM) for Gaussian Mixture Model (GMM) 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 tpbrennan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /matlab/evalModel.m: -------------------------------------------------------------------------------- 1 | %AUHTOR: MOHAMMAD MAHDI GHASSEMI, MIT 2 | %EMAIL: ghassemi@mit.edu 3 | function [lse_result] = evalModel(x,y,bval_i,opts,model,i,num_points_to_test,upper_bound,lower_bound,ms) 4 | % Evaluate the passed in function and return the results 5 | 6 | 7 | 8 | %Partition the data into testing and training sets. 9 | Indices = crossvalind('Kfold', size(y,1), 2); %Generate the indicies 10 | 11 | for i=1:2 12 | testing_ind = Indices == i; %testing indicies (10%) 13 | training_ind = not(testing_ind); %training indicies (90%) 14 | 15 | %% CHOOSE THE OPTIMIAL INITIAL STEP UISNG OPTIMIZATION STEP 16 | problem = createOptimProblem('lsqcurvefit',... 17 | 'objective', model,... 18 | 'xdata',x(training_ind,:),... 19 | 'ydata',y(training_ind),... 20 | 'x0',ones(1,bval_i),... 21 | 'lb',lower_bound*ones(1,bval_i),... 22 | 'ub',upper_bound*ones(1,bval_i)); 23 | [b,fval,exitflag,output,solutions] = run(ms,problem,num_points_to_test); 24 | best_coefficients = b; 25 | 26 | %mean squared error for each model 27 | lse_in(i) = sum((model(best_coefficients,x(testing_ind,:))-y(testing_ind)).^2)/length(y(testing_ind)); 28 | 29 | end 30 | %averaged. 31 | lse_result = mean(lse_in); 32 | 33 | end 34 | 35 | -------------------------------------------------------------------------------- /matlab/gene2model.m: -------------------------------------------------------------------------------- 1 | %AUTHOR: MOHAMMAD MAHDI GHASSEMI 2 | %EMAIL: ghassemi@mit.edu 3 | 4 | function [fun_out, b_i] =gene2model(x, genes) 5 | %genes is a column of 0s and 1s that say if the feature is on or off. 6 | %Define THE OVERALL STRUCTURE OF THE "GENOME" 7 | %model = @(b,x) b(1) + b(2)*x(:,1).^(b(3)) + ... 8 | % b(4)*x(:,2).^(b(5)) + ... 9 | % b(6)*x(:,3).^(b(7)) + ... 10 | % b(8)*x(:,4).^(b(9)) + ... 11 | % b(10)*x(:,5).^(b(11)) + ... 12 | % b(12)*x(:,6).^(b(13)) + ... 13 | % b(14)*x(:,7).^(b(15)) + ... 14 | % b(16)*x(:,8).^(b(17)) + ... 15 | % b(18)*x(:,9).^(b(19)); 16 | 17 | num_betas = 2*size(x,2) + 1; 18 | %Generate a genome 19 | %genes = randi(2,num_betas,1)-1; 20 | model_params = find(genes); 21 | 22 | x_i = 1; 23 | b_i = 2; 24 | out = []; 25 | for(i = 2:2:2*size(x,2)) 26 | if( i == 2) 27 | out = [out,'b(1) + ']; 28 | end 29 | 30 | %% IF i is on 31 | if(sum(i == model_params) & sum(i+1 == model_params)) 32 | out = [out,['b(' num2str(b_i) ')*x(:,' num2str(x_i) ').^(b(' num2str(b_i+1) ')) + ']]; 33 | b_i = b_i + 2; 34 | %If it's even - mutiplicative 35 | elseif(sum( i == model_params)) 36 | out = [out,['b(' num2str(b_i) ')*x(:,' num2str(x_i) ') + ']]; 37 | b_i = b_i + 1; 38 | %If it's odd - exponential 39 | elseif(sum(i+1 == model_params)) 40 | out = [out,['x(:,' num2str(x_i) ').^(b(' num2str(b_i) ')) + ']]; 41 | b_i = b_i + 1; 42 | end 43 | x_i = x_i + 1; 44 | end 45 | out = out(1:end-2); 46 | b_i = b_i - 1; 47 | 48 | eval(['fun_out = @(b,x)' out ';']) 49 | 50 | end -------------------------------------------------------------------------------- /pubs/cinc2014-abstract.txt: -------------------------------------------------------------------------------- 1 | Predicting mortality in intensive care units (ICU) remains a significant challenge. The problem is faced 2 | by physicians and patients' families alike, both in terms of triaging resources and accessing whether to 3 | switch code status from full status, to do not resuscitate (DNR) to comfort measures only (CMO). 4 | Common methods of predicting mortality, such as Severity of Acute Physiology Scure (SAPS) or the 5 | Acute Physiology and Chronic Health Evaluation (APACHE), use a combination of features to assess 6 | patient condition. However, both SAPS and APACHE tend to have poor predictivity across a 7 | population of patients. Our hypothesis is that in any given population, patient phenotypes can 8 | be clustered and that different features could be used to predict mortality for 9 | different patient subgroups. To assess this hypothesis we propose a data-drive approach that uses 10 | a genetic algorithm (GA) coupled with Gaussian mixture model (GMM) to identify which features best 11 | predict mortality for different patient subgroups. We extracted 50 variables from the MIMIC-II database 12 | from the first 24 hours of an ICU stay for patients admited to the ICU with sepsis. Median values for each 13 | variable were calculated, with the addition of trend information for vital sign variables where higher 14 | frequency data was available. The final feature vector consists of 152 parameters. Each iteration of the GA 15 | was terminated when a particular mixture in the GMM had a 28-mortality prediction AUC greater than 0.75. 16 | Patients associated with the most predictive mixture were removed from the training dataset and the GA is 17 | repeated to identify additional patient subgroups. The top 5 predictive mixtures were used in an ensemble 18 | classifier and compared against SAPS-I and a simple SVM classifier using all 152 parameters. 19 | -------------------------------------------------------------------------------- /pubs/dynamic_features.txt: -------------------------------------------------------------------------------- 1 | Cohort: Martin-criteria sepsis adult patients 2 | 3 | Primary Output: 30-day mortality 4 | 5 | Data: First 72 hours of ICU stay 6 | 7 | Patient: 8 | - age 9 | - gender 10 | - weight 11 | - height 12 | - SAPS-I 13 | - SOFA 14 | - APACHE IV 15 | 16 | Physiologic variables: 17 | - systolic, diastolic and mean blood pressure (invasive or non-invasive) 18 | - heart rate 19 | - central venous pressure 20 | - peripheral oxygen saturation 21 | - respiration rate 22 | - urine output 23 | - temperature 24 | 25 | Lab variables: 26 | - lactate 27 | - creatinin 28 | - calcium 29 | - chloride 30 | - sodium 31 | - bicardbonate 32 | - BUN 33 | - hematocrit 34 | - hemoglobin 35 | - platelets 36 | - glucose 37 | - magnesium 38 | - phosphorous 39 | - white blood cell count (WBC) 40 | - total bilirubin 41 | - alanine transaminase (ALT) 42 | - albumin 43 | - international normalized ratio (prothombin time) (INR) 44 | - partial pressure of oxygen (PaO2) 45 | - partial pressure of carbon dioxide (PaCO2) 46 | - blood pH 47 | 48 | Static Features (daily) 49 | - mean and std 50 | - 25,50 & 70 percentiles 51 | - inter-quartile range (75-25) 52 | - kurtosis 53 | - skewness 54 | - Rescaled range, i.e. (max-min)/std 55 | 56 | Dynamics Features (over 12,24,48,72 hours) 57 | - m.x + c (linear approximation) 58 | - max delta (%), both increase and decrease in timeseries 59 | - entropy 60 | - delta (percentage change) of static features 61 | 62 | Treatments/Procedures 63 | - onset of vasopressor therapy (onset - icustay_intime) 64 | - vasopressor dosage 65 | - duration of vasopressor therapy 66 | - total amount of fluid in 67 | - rate of fluid in 68 | - onset of renal replacement therapy (onset - icustay_intime) 69 | - duration of renal replacement therapy 70 | - onset of ventilation (onset - icustay_intime) 71 | - duration of ventilation 72 | - fraction of inspired oxygen (FiO2) 73 | - ventilated volume 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /sql/SevereSepsisDefinitionMartin2003.sql: -------------------------------------------------------------------------------- 1 | create table "MAYAUDLO".SevereSepsisIDMartin2003 as 2 | with organFailurePopulation as ( 3 | SELECT distinct h.hadm_id, 4 | h.subject_ID 5 | FROM mimic2v26.admissions h, 6 | mimic2v26.ICD9 code 7 | WHERE code.SUBJECT_ID = h.SUBJECT_ID 8 | and ( code.CODE = '518.81' -- Acute respiratory failure 9 | or code.CODE = '518.82' -- Acute respiratory distress syndrome' 10 | or code.CODE = '518.85' -- Acute respiratory distress syndrome after shock or trauma 11 | or code.CODE = '786.09' -- Respiratory insufficiency 12 | or code.CODE like '799.1%' -- Respiratory arrest 13 | or code.CODE like '96.7%' -- Ventilator management 14 | or code.CODE like '458.0%' -- Hypotension, postural 15 | or code.CODE like '785.5%' -- Shock 16 | or code.CODE = '785.51' -- Shock, cardiogenic 17 | or code.CODE = '785.59' -- Shock, circulatory or septic 18 | or code.CODE like '458.0%' -- Hypotension, postural 19 | or code.CODE like '458.8%' -- Hypotension, specified type, not elsewhere classified 20 | or code.CODE like '458.9%' -- Hypotension, arterial, constitutional 21 | or code.CODE like '796.3%' -- Hypotension, transient 22 | or code.CODE like '584%' -- Acute renal failure 23 | or code.CODE like '580%' -- Acute glomerulonephritis 24 | or code.CODE like '585%' -- Renal shutdown, unspecified 25 | or code.CODE = '39.95%' -- 26 | or code.CODE like '570%' -- Acute hepatic failure or necrosis 27 | or code.CODE like '572.2%' -- Hepatic encephalopathy 28 | or code.CODE like '573.3%' -- Hepatitis, septic or unspecified 29 | or code.CODE like '286.2%' -- Disseminated intravascular coagulation 30 | or code.CODE like '286.6%' -- Purpura fulminans 31 | or code.CODE like '286.9%' -- Coagulopathy 32 | or code.CODE like '287.3%' -- Thrombocytopenia, primary, secondary, or unspecified 33 | or code.CODE like '287.4%' 34 | or code.CODE like '287.5%' 35 | or code.CODE like '276.2%' -- Acidosis, metabolic or lactic 36 | or code.CODE like '293%' -- Transient organic psychosis 37 | or code.CODE like '348.1%' -- Anoxic brain injury 38 | or code.CODE like '348.3%' -- Encephalopathy, acute 39 | or code.CODE = '780.01' -- Coma 40 | or code.CODE = '780.09' -- Altered consciousness, unspecified 41 | or code.CODE = '89.14' -- Electroencephalography 42 | ) 43 | ), 44 | infectionPopulation as ( 45 | SELECT distinct h.hadm_id, 46 | h.subject_ID 47 | FROM mimic2v26.admissions h, 48 | mimic2v26.ICD9 code 49 | WHERE code.SUBJECT_ID = h.SUBJECT_ID 50 | AND ( 51 | code.CODE like '038%' -- septicemia 52 | or code.CODE like '020.0%' --septicemic 53 | or code.CODE like '790.7%' -- (bacteremia) 54 | or code.CODE like '117.9%' -- (disseminated fungal infection), 55 | or code.CODE like '112.5%' -- (disseminated candida infection), 56 | or code.CODE = '112.81%' -- (disseminated fungal endocarditis) 57 | ) 58 | ) 59 | , organFailurePopulationProc as ( 60 | SELECT distinct h.hadm_id, 61 | h.subject_ID 62 | FROM infectionPopulation h, 63 | mimic2v26.procedureevents proc 64 | WHERE proc.SUBJECT_ID = h.SUBJECT_ID 65 | and ( proc.itemid = '101781' --Respiratory Mechanical ventilation 66 | or proc.itemid = '101782' --Respiratory Mechanical ventilation 67 | or proc.itemid = '101783' --Respiratory Mechanical ventilation 68 | or proc.itemid = '100622' -- Hemodialysis 69 | or proc.itemid = '101174' -- Electroencephalography 70 | ) 71 | ) 72 | 73 | -- select distinct itemid 74 | -- from mimic2v26.d_codeditems 75 | -- where code like '8914%' 76 | -- or code like '967%' 77 | -- or code like '3995%' 78 | -- and type = 'PROCEDURE' 79 | 80 | select * 81 | from organFailurePopulationProc 82 | 83 | ; 84 | select distinct ip.hadm_id, 85 | ip.subject_id 86 | from organFailurePopulation oFp, 87 | --organFailurePopulationProc oFpProc, 88 | infectionPopulation ip 89 | where oFp.hadm_id = ip.hadm_id 90 | --or oFpProc.hadm_id = ip.hadm_id 91 | 92 | 93 | -------------------------------------------------------------------------------- /sql/extract_main_table.sql: -------------------------------------------------------------------------------- 1 | create table mayaudlo.septicshock_cohort_details as 2 | with demo as ( 3 | select s.*, 4 | admission_type_descr, admission_source_descr, 5 | case 6 | when ethnicity_descr like '%WHITE%' then 'White' 7 | when ethnicity_descr like '%BLACK%' then 'Black' 8 | when ethnicity_descr like '%HISPANIC%' then 'Hispanic' 9 | when ethnicity_descr like '%ASIAN%' then 'Asian' 10 | else 'Other' 11 | end as Ethnicity 12 | 13 | from sepshock_angus2001 s, -- main table 14 | mimic2v26.demographic_detail d 15 | 16 | where s.hadm_id = d.hadm_id (+) 17 | 18 | ) 19 | 20 | ,icustay_details as ( 21 | 22 | select --s.subject_id, s.hadm_id, s.icustay_id, 23 | s.*, 24 | gender, hospital_expire_flg, icustay_expire_flg, 25 | icustay_first_careunit first_careunit, 26 | sapsi_first, 27 | trunc(extract( day from (d.icustay_intime - d.dob) )/365 , 2) age, 28 | trunc(extract( day from s.he_onset - d.icustay_intime )*24 + extract( hour from s.he_onset - d.icustay_intime ) + extract( minute from s.he_onset - d.icustay_intime )/60 , 2) onset_time_hr, 29 | trunc(extract( day from s.he_offset - s.he_onset )*24*60 + extract( hour from s.he_offset - s.he_onset )*60 + extract( minute from s.he_offset - s.he_onset ) , 2) he_length_min, 30 | trunc( -1 + extract( day from dod - s.he_offset ) + extract( hour from dod - s.he_offset )/24, 2) time_to_death_day, 31 | trunc(extract( day from d.icustay_intime - d.hospital_admit_dt ) + extract( hour from d.icustay_intime - d.hospital_admit_dt )/24, 2) pre_los_day 32 | 33 | from demo s, -- main table 34 | mimic2v26.icustay_detail d 35 | where s.icustay_id = d.icustay_id (+) 36 | 37 | ) 38 | 39 | , hepfail_ as ( 40 | select s.icustay_id, 41 | min( i.sequence ) as hepfail_sequence 42 | 43 | from icustay_details s, -- main table 44 | mimic2v26.icd9 i 45 | 46 | where s.hadm_id = i.hadm_id (+) 47 | and i.code like '570%' 48 | group by s.icustay_id 49 | 50 | ) 51 | , hepfail as ( 52 | 53 | select s.*, 54 | h.hepfail_sequence 55 | from icustay_details s, -- main table 56 | hepfail_ h 57 | where s.icustay_id = h.icustay_id (+) 58 | 59 | ) 60 | 61 | ,comorbid as ( 62 | 63 | select --s.subject_id, s.hadm_id, s.icustay_id, 64 | s.*, 65 | e.congestive_heart_failure , 66 | e.cardiac_arrhythmias, 67 | e.valvular_disease, 68 | e.pulmonary_circulation, 69 | e.peripheral_vascular, 70 | e.hypertension, 71 | e.paralysis, 72 | e.other_neurological, 73 | e.chronic_pulmonary, 74 | e.diabetes_uncomplicated, 75 | e.diabetes_complicated, 76 | e.hypothyroidism, 77 | e.renal_failure, 78 | e.liver_disease, 79 | e.peptic_ulcer, 80 | e.aids, 81 | e.lymphoma, 82 | e.metastatic_cancer, 83 | e.solid_tumor, 84 | e.rheumatoid_arthritis, 85 | e.coagulopathy, 86 | e.obesity, 87 | e.weight_loss, 88 | e.fluid_electrolyte, 89 | e.blood_loss_anemia, 90 | e.deficiency_anemias, 91 | e.alcohol_abuse, 92 | e.drug_abuse, 93 | e.psychoses, 94 | e.depression 95 | from hepfail s, -- main table 96 | mimic2v26.comorbidity_scores e 97 | where s.hadm_id = e.hadm_id (+) 98 | 99 | ) 100 | 101 | 102 | , RRT_ as ( 103 | 104 | select --s.subject_id, s.hadm_id, 105 | s.icustay_id, 106 | 1 as RRT_TEXT 107 | from comorbid s, -- main table 108 | mimic2v26.noteevents n, 109 | mimic2v26.icustay_detail d 110 | where s.icustay_id = n.icustay_id 111 | and d.icustay_id (+) = s.icustay_id 112 | and charttime between d.icustay_intime and he_offset + interval '1' day 113 | and category = 'Nursing/Other' 114 | and ( 115 | lower(text) like '% dialysis %' or 116 | lower(text) like '% hemodialysis %' or 117 | lower(text) like '% ihd %' or 118 | lower(text) like '% crrt %' or 119 | lower(text) like '% cvvh %' or 120 | lower(text) like '% cvvhd %' or 121 | lower(text) like '% esrd %' 122 | ) 123 | ) 124 | 125 | , RRT as ( 126 | 127 | select s.*, 128 | r.RRT_TEXT 129 | from RRT_ r, 130 | comorbid s 131 | where r.icustay_id (+) = s.icustay_id 132 | 133 | ) 134 | 135 | select * from RRT 136 | 137 | 138 | -------------------------------------------------------------------------------- /sql/labs_var_raw.sql: -------------------------------------------------------------------------------- 1 | drop materialized view mimic2v26_labs_raw_48hr; 2 | create materialized view mimic2v26_labs_raw_48hr as 3 | 4 | with labs_raw as ( 5 | select s.subject_id, 6 | s.hadm_id, 7 | s.icustay_id, 8 | extract(day from c.charttime - s.icustay_intime)*1440 + 9 | extract(hour from c.charttime - s.icustay_intime)*60 + 10 | extract(minute from c.charttime - s.icustay_intime) 11 | as post_adm, 12 | case 13 | when c.itemid in (50383,50029) 14 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'HCT' 15 | when c.itemid in (51326,50468,50316) 16 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'WBC' 17 | when c.itemid in (50112,50936,50006) 18 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'GLUCOSE' 19 | when c.itemid in (50803,50022,50172,50025) 20 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'HCO3' 21 | when c.itemid in (50009,50821,50976,50149) 22 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'POTASSIUM' 23 | when c.itemid in (50989,50823,50159,50012) 24 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'SODIUM' 25 | when c.itemid in (51011,50177) 26 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'BUN' 27 | when c.itemid in (50090,50916) 28 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'CREATININE' 29 | when c.itemid in (50386,50007,50184) 30 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'HGB' 31 | when c.itemid in (50428) 32 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'PLATELETS' 33 | when c.itemid in (50083,50004) 34 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'CHLORIDE' 35 | when c.itemid in (50010) 36 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'LACTATE' 37 | when c.itemid in (50018) 38 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'PH' 39 | when c.itemid in (50019) 40 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'PO2' 41 | when c.itemid in (664,838) 42 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'SVO2' 43 | when c.itemid in (50195) 44 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'BNP' 45 | when c.itemid in (50189) 46 | and extract(day from c.charttime - s.icustay_intime) < 3 then 'TROPONIN' 47 | end as category, 48 | valuenum 49 | from tbrennan.mimic2v26_48hr_vital_signs s 50 | join mimic2v26.labevents c on s.icustay_id = c.icustay_id 51 | where extract(day from c.charttime - s.icustay_intime) < 3 52 | and ((c.itemid in (50383,50029) -- 'HCT' 53 | and c.valuenum between 5 and 100) -- 0 <> 390 54 | or (c.itemid in (51326,50468,50316) -- 'WBC' 55 | and c.valuenum*1000 between 5 and 2000000) -- 0 <> 1,250,000 56 | or (c.itemid in (50112,50936,50006)-- 'GLUCOSE' 57 | and c.valuenum between 0.5 and 1000) -- -251 <> 3555 58 | or (c.itemid in (50803,50022,50172,50025)--'HCO3' 59 | and c.valuenum between 2 and 100) -- 0 <> 231 60 | or (c.itemid in (50009,50821,50976,50149)-- 'POTASSIUM' 61 | and c.valuenum between 0.5 and 70) -- 0.7 <> 52 62 | or (c.itemid in (50989,50823,50159,50012)-- 'SODIUM' 63 | and c.valuenum between 50 and 300) -- 1.07 <> 1332 64 | or (c.itemid in (51011,50177) -- 'BUN' 65 | and c.valuenum between 1 and 100) -- 0 <> 280 66 | or (c.itemid in (50090,50916) -- 'CREATININE' 67 | and c.valuenum between 0 and 30) -- 0 <> 73 68 | or (c.itemid in (50386,50007,50184) -- 'HEMOGLOBIN' 69 | and c.valuenum between 5 and 30) -- 70 | or (c.itemid in (50428) -- 'PLATELETS' 71 | and c.valuenum between 50 and 600) -- 72 | or (c.itemid in (50083,50004) -- 'CHLORIDE' 73 | and c.valuenum between 50 and 200) -- 74 | or (c.itemid in (50010) -- 'LACTATE' 75 | and c.valuenum between 0 and 10) -- 76 | or (c.itemid in (50018) -- 'PH' 77 | and c.valuenum between 7 and 8) -- 78 | or (c.itemid in (50019) -- 'PO2' 79 | and c.valuenum between 50 and 150) -- 80 | or (c.itemid in (664,838) -- 'SVO2' 81 | and c.valuenum between 50 and 100) -- 82 | or (c.itemid in (50195) -- 'BNP' 83 | and c.valuenum between 0 and 150) -- 84 | or (c.itemid in (50189) -- 'TROPONIN' 85 | and c.valuenum between 0 and 50) -- 86 | ) 87 | and c.valuenum is not null 88 | order by subject_id, icustay_id, category 89 | ) 90 | select * from labs_raw; 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /sql/static_features.sql: -------------------------------------------------------------------------------- 1 | with population as ( 2 | select sc.subject_id, 3 | sc.hadm_id, 4 | id.icustay_id, 5 | id.gender, 6 | case when id.icustay_admit_age > 90 7 | then 92.4 8 | else round(id.icustay_admit_age,2) 9 | end as age, 10 | id.icustay_intime, 11 | round(id.icustay_los / 1400, 3) icustay_los_days, 12 | round(id.hospital_los / 1400, 3) hospital_los_days, 13 | id.icustay_expire_flg icu_exp_flg, 14 | id.hospital_expire_flg hosp_exp_flg, 15 | extract(day from id.dod - id.icustay_intime) survival_days, 16 | id.icustay_first_service careservice, 17 | id.weight_first weight, 18 | id.height, 19 | id.sapsi_first saps, 20 | id.sofa_first sofa 21 | 22 | from tbrennan.angus_sepsis_cohort sc 23 | join mimic2v26.icustay_detail id 24 | on sc.subject_id = id.subject_id 25 | and sc.hadm_id = id.hadm_id 26 | 27 | where id.subject_icustay_seq = 1 28 | ) 29 | --select * from population; -- icustay_id/subject_id 5156 30 | 31 | , bmi as ( 32 | select icustay_id, 33 | round(weight / power(height/100,2),2) bmi 34 | from population 35 | ) 36 | --select * from bmi; 37 | 38 | , demo as ( 39 | select p.icustay_id, 40 | d.admission_type_descr, 41 | d.admission_source_descr, 42 | case 43 | when ethnicity_descr like '%WHITE%' then 'White' 44 | when ethnicity_descr like '%BLACK%' then 'Black' 45 | when ethnicity_descr like '%HISPANIC%' then 'Hispanic' 46 | when ethnicity_descr like '%ASIAN%' then 'Asian' 47 | else 'Other' 48 | end as ethnicity 49 | 50 | from population p, 51 | mimic2v26.demographic_detail d 52 | 53 | where p.hadm_id = d.hadm_id (+) 54 | ) 55 | --select * from demo; 56 | 57 | , comorb as ( 58 | 59 | select --s.subject_id, s.hadm_id, s.icustay_id, 60 | p.subject_id, 61 | p.hadm_id, 62 | p.icustay_id, 63 | e.congestive_heart_failure , 64 | e.cardiac_arrhythmias, 65 | e.valvular_disease, 66 | e.pulmonary_circulation, 67 | e.peripheral_vascular, 68 | e.hypertension, 69 | e.paralysis, 70 | e.other_neurological, 71 | e.chronic_pulmonary, 72 | e.diabetes_uncomplicated, 73 | e.diabetes_complicated, 74 | e.hypothyroidism, 75 | e.renal_failure, 76 | e.liver_disease, 77 | e.peptic_ulcer, 78 | e.aids, 79 | e.lymphoma, 80 | e.metastatic_cancer, 81 | e.solid_tumor, 82 | e.rheumatoid_arthritis, 83 | e.coagulopathy, 84 | e.obesity, 85 | e.weight_loss, 86 | e.fluid_electrolyte, 87 | e.blood_loss_anemia, 88 | e.deficiency_anemias, 89 | e.alcohol_abuse, 90 | e.drug_abuse, 91 | e.psychoses, 92 | e.depression 93 | from population p, -- main table 94 | mimic2v26.comorbidity_scores e 95 | where p.hadm_id = e.hadm_id (+) 96 | 97 | ) 98 | --select * from comorb; 99 | 100 | , vasopressor_therapy as ( 101 | select distinct p.subject_id, 102 | p.hadm_id, 103 | p.icustay_id, 104 | m.itemid, 105 | case when m.dose <> 0 then 1 106 | else 0 107 | end as vasopressor 108 | from population p 109 | left join mimic2v26.medevents m 110 | on m.icustay_id = p.icustay_id 111 | and m.itemid in (42, 43, 44, 47, 51, 119, 120, 125, 127, 128) 112 | ) 113 | --select * from vasopressor_therapy; -- rows 2632 114 | 115 | /* 116 | , pressors as ( 117 | select distinct cd.icustay_id, 118 | m.itemid, 119 | m.charttime, 120 | round(m.dose,2) dose, 121 | m.doseuom 122 | from tbrennan.echo_cohort cd 123 | join mimic2v26.medevents m 124 | on cd.icustay_id = m.icustay_id 125 | and m.itemid in (42, 43, 44, 47, 51, 119, 120, 125, 127, 128) 126 | and m.dose<>0 127 | ) 128 | --select count(icustay_id) from pressors; -- 848 rows 129 | 130 | 131 | , pressor_start as ( 132 | select distinct icustay_id, itemid, 133 | first_value(charttime) over (partition by icustay_id, itemid order by charttime) start_time 134 | from pressors 135 | ) 136 | --select * from pressor_start; 137 | 138 | , pressor_end as ( 139 | select distinct icustay_id, itemid, 140 | first_value(charttime) over (partition by icustay_id, itemid order by charttime desc) end_time 141 | from pressors 142 | ) 143 | --select * from pressor_end; 144 | 145 | , pressor_therapy as ( 146 | select distinct p.icustay_id, 147 | p.itemid, 148 | p.dose, 149 | ps.start_time, 150 | pe.end_time, 151 | extract(day from pe.end_time - ps.start_time)*1440 + 152 | extract(hour from pe.end_time - ps.start_time)*60 + 153 | extract(minute from pe.end_time - ps.start_time) duration 154 | from pressors p 155 | join pressor_start ps on p.icustay_id = ps.icustay_id and p.itemid = ps.itemid 156 | join pressor_end pe on p.icustay_id = pe.icustay_id and p.itemid = pe.itemid 157 | order by p.icustay_id, ps.start_time 158 | ) 159 | --select count(icustay_id) from pressor_therapy; 160 | */ 161 | 162 | , final_therapy as ( 163 | select distinct p.subject_id, p.icustay_id, 164 | first_value(t.vasopressor) over (partition by p.icustay_id order by t.vasopressor desc) vasopressor, 165 | sum(t.vasopressor) over (partition by p.icustay_id) no_pressors, 166 | case when v.seq = 1 then 1 167 | else 0 168 | end as ventilated, 169 | case when rc.rrt = 1 then 1 170 | else 0 171 | end as rrt 172 | from population p 173 | left join vasopressor_therapy t on t.icustay_id = p.icustay_id 174 | left join mimic2devel.ventilation v on v.icustay_id = p.icustay_id 175 | left join tbrennan.rrt_cohort rc on rc.icustay_id = p.icustay_id 176 | ) 177 | --select * from final_therapy; 178 | 179 | 180 | , assemble as ( 181 | select p.*, 182 | b.bmi, 183 | d.admission_type_descr, 184 | d.admission_source_descr, 185 | d.ethnicity, 186 | e.congestive_heart_failure , 187 | e.cardiac_arrhythmias, 188 | e.valvular_disease, 189 | e.pulmonary_circulation, 190 | e.peripheral_vascular, 191 | e.hypertension, 192 | e.paralysis, 193 | e.other_neurological, 194 | e.chronic_pulmonary, 195 | e.diabetes_uncomplicated, 196 | e.diabetes_complicated, 197 | e.hypothyroidism, 198 | e.renal_failure, 199 | e.liver_disease, 200 | e.peptic_ulcer, 201 | e.aids, 202 | e.lymphoma, 203 | e.metastatic_cancer, 204 | e.solid_tumor, 205 | e.rheumatoid_arthritis, 206 | e.coagulopathy, 207 | e.obesity, 208 | e.weight_loss, 209 | e.fluid_electrolyte, 210 | e.blood_loss_anemia, 211 | e.deficiency_anemias, 212 | e.alcohol_abuse, 213 | e.drug_abuse, 214 | e.psychoses, 215 | e.depression, 216 | ft.vasopressor, 217 | ft.no_pressors, 218 | ft.ventilated, 219 | ft.rrt 220 | from population p 221 | left join final_therapy ft on p.icustay_id = ft.icustay_id 222 | left join bmi b on b.icustay_id = ft.icustay_id 223 | left join demo d on d.icustay_id = ft.icustay_id 224 | left join comorb e on e.icustay_id = ft.icustay_id 225 | ) 226 | select * from assemble; -------------------------------------------------------------------------------- /R/GA_echo.R: -------------------------------------------------------------------------------- 1 | # install.packages("caret") 2 | # install.packages("ggplot2") 3 | # install.packages("reshape2") 4 | # install.packages("RColorBrewer") 5 | # install.packages("GA") 6 | # install.packages("latticeExtra") 7 | # install.packages("pROC") 8 | # install.packages("doMC") 9 | # install.packages("caTools") 10 | #install.packages("doParallel") 11 | #install.packages("kernlab") 12 | 13 | library(caret) 14 | library(ggplot2) 15 | library(reshape2) 16 | library(RColorBrewer) 17 | library(GA) 18 | library(latticeExtra) 19 | library(pROC) 20 | library(doMC) 21 | library(MASS) 22 | library(caTools) 23 | library(doParallel) 24 | #library(kernlab) 25 | 26 | ## CLEAR WORKSPACE 27 | rm(list=ls()) 28 | 29 | ## load data 30 | #setwd("/home/mornin/Dropbox/ECHO/R/reresults"); 31 | fulldata <- read.csv("data-2.csv") 32 | # fulldata1<- fulldata 33 | # fulldata1<- transform(fulldata 34 | # , GENDER_NUM= factor(GENDER_NUM)) 35 | 36 | fulldata1<- transform(fulldata 37 | , GENDER_NUM=factor(GENDER_NUM, c(0,1)) 38 | , SERVICE_NUM=factor(SERVICE_NUM, c(0,1,2)) #remove csru 39 | , VENT_FLG=factor(VENT_FLG, c(0,1)) 40 | , VASO_FLG=factor(VASO_FLG, c(0,1)) 41 | , ANES_12HR_FLG=factor(ANES_12HR_FLG, c(0,1)) 42 | , CHF_FLG=factor(CHF_FLG, c(0,1)) 43 | , AFIB_FLG=factor(AFIB_FLG, c(0,1)) 44 | , RENAL_FLG=factor(RENAL_FLG, c(0,1)) 45 | , LIVER_FLG=factor(LIVER_FLG, c(0,1)) 46 | , COPD_FLG=factor(COPD_FLG, c(0,1)) 47 | , CAD_FLG=factor(CAD_FLG, c(0,1)) 48 | , STROKE_FLG=factor(STROKE_FLG, c(0,1)) 49 | , MAL_FLG=factor(MAL_FLG, c(0,1)) 50 | , WBC_ABNORMAL_FLG=factor(WBC_ABNORMAL_FLG, c(0,1)) 51 | , HGB_ABNORMAL_FLG=factor(HGB_ABNORMAL_FLG, c(0,1)) 52 | , PLATELET_ABNORMAL_FLG=factor(PLATELET_ABNORMAL_FLG, c(0,1)) 53 | , SODIUM_ABNORMAL_FLG=factor(SODIUM_ABNORMAL_FLG, c(0,1)) 54 | , POTASSIUM_ABNORMAL_FLG=factor(POTASSIUM_ABNORMAL_FLG, c(0,1)) 55 | , TCO2_ABNORMAL_FLG=factor(TCO2_ABNORMAL_FLG, c(0,1)) 56 | , CHLORIDE_ABNORMAL_FLG=factor(CHLORIDE_ABNORMAL_FLG, c(0,1)) 57 | , BUN_ABNORMAL_FLG=factor(BUN_ABNORMAL_FLG, c(0,1)) 58 | , CREATININE_ABNORMAL_FLG=factor(CREATININE_ABNORMAL_FLG, c(0,1)) 59 | , LACTATE_ABNORMAL_FLG=factor(LACTATE_ABNORMAL_FLG, c(0,1)) 60 | , PH_ABNORMAL_FLG=factor(PH_ABNORMAL_FLG, c(0,1)) 61 | , PO2_ABNORMAL_FLG=factor(PO2_ABNORMAL_FLG, c(0,1)) 62 | , PCO2_ABNORMAL_FLG=factor(PCO2_ABNORMAL_FLG, c(0,1)) 63 | ) 64 | fulldata1=fulldata1[complete.cases(fulldata1),] 65 | 66 | ## define objective function 67 | ## 'ind' is a vector of 0/1 data denoting which features are being evaluated. 68 | ROCcv <- function(ind, x, y, cntrl) 69 | { 70 | library(caret) 71 | library(MASS) 72 | ind <- which(ind == 1) 73 | if(length(ind) == 0) return(0) 74 | # max allowed feature number = 20 75 | if(length(ind) > 50) return(0) 76 | out <- train(x[,ind], y, method = "glm", 77 | metric = "ROC", trControl = cntrl) 78 | caret:::getTrainPerf(out)[, "TrainROC"] 79 | } 80 | 81 | # ROCcv1 <- function(ind, data, cntrl) 82 | # { 83 | # #library(caret) 84 | # #library(MASS) 85 | # ind <- which(ind == 1) 86 | # if(length(ind) == 0) return(0) 87 | # # max allowed feature number = 20 88 | # if(length(ind) > 20) return(0) 89 | # out <- train(CLASS~., data, method = "qda", 90 | # metric = "ROC", trControl = cntrl) 91 | # caret:::getTrainPerf(out)[, "TrainROC"] 92 | # } 93 | # define initialization fucntion 94 | initialPop <- function(object, ...) 95 | { 96 | population <- sample(0:1, 97 | replace = TRUE, 98 | size = object@nBits * object@popSize, 99 | prob = c(0.9, 0.1)) 100 | population <- matrix(population, 101 | nrow = object@popSize, 102 | ncol = object@nBits) 103 | return(population) 104 | } 105 | 106 | ## For testing 107 | # ## set-up cross-validation 108 | cvIndex <- caret::createMultiFolds(fulldata1$CLASS, times = 1) 109 | ctrl <- caret::trainControl(method = "repeatedcv", 110 | repeats = 1, 111 | classProbs = TRUE, 112 | summaryFunction = twoClassSummary, 113 | allowParallel = TRUE, 114 | index = cvIndex) 115 | # temp=as.double(rep(1,79)) 116 | # #ROCcv1(temp,fulldata1,ctrl) 117 | # x = fulldata1[,-ncol(fulldata1)] 118 | # y = fulldata1$CLASS 119 | # ROCcv(temp,x,y,ctrl) 120 | # 121 | # train(x,y, method = "glm", metric = "ROC", trControl = ctrl) 122 | # modelAll <- train(CLASS ~ ., data = fulldata1, method = "qda", metric = "ROC", trControl = ctrl) 123 | # caret:::getTrainPerf(modelAll)[, "TrainROC"] 124 | 125 | 126 | # ## call Genetic Algrithm to do feature selection (generation No. = maxiter, it might take 4 mins per generation) 127 | 128 | set.seed( as.integer((as.double(Sys.time())*1000+Sys.getpid()) %% 2^31) ) 129 | ga_results <- ga(type = "binary", 130 | fitness = ROCcv, 131 | min = 0, max = 1, 132 | maxiter = 1000, 133 | population = initialPop, 134 | nBits = ncol(fulldata1) - 1, 135 | names = names(fulldata1)[-ncol(fulldata1)], 136 | x = fulldata1[,-ncol(fulldata1)], 137 | y = fulldata1$CLASS, 138 | cntrl = ctrl, 139 | keepBest = TRUE, 140 | parallel = TRUE) 141 | # ## view results 142 | summary(ga_results) 143 | 144 | Sys.time() 145 | 146 | # ## obtain feature vector 147 | 148 | solution <- ga_results@solution 149 | features <- which(solution[nrow(solution),]!=0) 150 | write.csv(features, file = "features2_1000.csv") 151 | # ## build model using all features 152 | # modelAll <- train(CLASS ~ ., data = fulldata, method = "qda", metric = "ROC", trControl = ctrl) 153 | # ## make predictions using modelAll 154 | # #prediction <- predict(modelAll, fulldata, type = "prob") 155 | # # build model using selected features 156 | modelReduced <- train(fulldata1[,features],fulldata1$CLASS, method = "glm", metric = "ROC", trControl = ctrl) 157 | # ## make predictions using modelReduced 158 | #prediction <- predict(modelReduced, fulldata1[,features], type = "prob") 159 | # ## view first a few predictions, the score of each samples can be found here 160 | # head(prediction) 161 | # ## generate ROC curve 162 | # curve <- roc(fulldata$CLASS, prediction[,1], levels = rev(levels(fulldata$CLASS))) 163 | # ## plot ROC curve and compute AUROC 164 | # rocColors <- c("black", "grey", brewer.pal(8,"Dark2")) 165 | # plot(curve, col = rocColors[1], lwd = 2) 166 | # 167 | 168 | 169 | # cvIndex <- caret::createMultiFolds(fulldata$CLASS, times = 2) 170 | # ctrl <- caret::trainControl(method = "repeatedcv", 171 | # repeats = 2, 172 | # classProbs = TRUE, 173 | # summaryFunction = twoClassSummary, 174 | # allowParallel = FALSE, 175 | # index = cvIndex) 176 | # ## call Genetic Algrithm to do feature selection (generation No. = maxiter, it might take 4 mins per generation) 177 | # set.seed(123) 178 | # ga_results <- ga(type = "binary", 179 | # fitness = ROCcv, 180 | # min = 0, max = 1, 181 | # maxiter = 100, 182 | # population = initialPop, 183 | # nBits = ncol(fulldata) - 1, 184 | # names = names(fulldata)[-ncol(fulldata)], 185 | # x = fulldata[,-ncol(fulldata)], 186 | # y = fulldata$CLASS, 187 | # cntrl = ctrl, 188 | # keepBest = TRUE, 189 | # parallel = TRUE) 190 | # ## view results 191 | # summary(ga_results) 192 | 193 | -------------------------------------------------------------------------------- /sql/li_raw_saps_variables.sql: -------------------------------------------------------------------------------- 1 | -- Variables used in SAPS from mimic2v26 2 | -- Li Lehman (lilehman@mit.edu) 3 | 4 | --creatinine 5 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 6 | where itemid in (50090) 7 | AND valuenum BETWEEN 0 AND 30 8 | AND valuenum IS NOT NULL 9 | order by subject_id, charttime 10 | 11 | 12 | --pH 13 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 14 | where itemid in (50018) 15 | AND valuenum IS NOT NULL 16 | order by subject_id, charttime 17 | 18 | 19 | 20 | --lactate in blood, blood gas, LOINC_CODE 32693-4 [Moles/volume] 21 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 22 | where itemid in (50010) 23 | AND valuenum IS NOT NULL 24 | order by subject_id, charttime 25 | 26 | 27 | --pco2 in blood, blood gas, LOINC_CODE 11557-6 Carbon dioxide [Partial pressure] in Blood 28 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 29 | where itemid in (50016) 30 | AND valuenum IS NOT NULL 31 | order by subject_id, charttime 32 | 33 | 34 | 35 | -- hematocrit (50383) 36 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 37 | where itemid in (50383) 38 | AND valuenum IS NOT NULL 39 | order by subject_id, charttime 40 | 41 | 42 | -- wbc (50316, 50468) 43 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 44 | where itemid in (50316, 50468) 45 | AND valuenum IS NOT NULL 46 | order by subject_id, charttime 47 | 48 | -- glucose (50006, 50112) 49 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 50 | where itemid in (50006, 50112) 51 | AND valuenum IS NOT NULL 52 | order by subject_id, charttime 53 | 54 | -- potassium(50009, 50149) 55 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 56 | where itemid in (50009, 50149) 57 | AND valuenum IS NOT NULL 58 | order by subject_id, charttime 59 | 60 | -- sodium (50012, 50159) 61 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 62 | where itemid in (50012, 50159) 63 | AND valuenum IS NOT NULL 64 | order by subject_id, charttime 65 | 66 | -- hco3(50022, 50025, 50172) 67 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 68 | where itemid in (50022, 50025, 50172) 69 | AND valuenum IS NOT NULL 70 | order by subject_id, charttime 71 | 72 | -- BUN (50177) labevents 73 | select subject_id, hadm_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, valuenum from mimic2v26.labevents 74 | where itemid in (50177) 75 | AND valuenum IS NOT NULL 76 | order by subject_id, charttime 77 | 78 | 79 | 80 | ---------------------------CHARTEVENTS---------------------- 81 | --hr (676, 677, 678, 679) 82 | select subject_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, value1num from mimic2v26.chartevents 83 | where itemid in (676, 677, 678, 679) 84 | AND value1num IS NOT NULL 85 | order by subject_id, charttime 86 | 87 | 88 | 89 | --abpmean 90 | select subject_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, value1num from mimic2v26.chartevents 91 | where itemid in (52) 92 | AND value1num BETWEEN 0 AND 300 93 | AND value1num IS NOT NULL 94 | order by subject_id, charttime 95 | 96 | 97 | 98 | --abpsys 99 | select subject_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, value1num from mimic2v26.chartevents 100 | where itemid in (51) 101 | AND value1num IS NOT NULL 102 | order by subject_id, charttime 103 | 104 | 105 | --temp(676, 677, 678, 679) 106 | select subject_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, value1num from mimic2v26.chartevents 107 | where itemid in (676, 677, 678, 679) 108 | AND value1num IS NOT NULL 109 | order by subject_id, charttime 110 | 111 | --Glasgow Coma Score 112 | select subject_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, value1num from mimic2v26.chartevents 113 | where itemid in (198) --GCS total 114 | AND value1num IS NOT NULL 115 | order by subject_id, charttime 116 | 117 | 118 | 119 | 120 | -- ventilated resp 121 | 122 | select subject_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, value1num from mimic2v26.chartevents 123 | where itemid in (543, 544, 545, 619, 39, 535, 683, 720, 721, 722, 732) 124 | AND value1num IS NOT NULL 125 | order by subject_id, charttime 126 | 127 | -- extract ventilated resp as -1 128 | WITH all_icustay_days AS 129 | (SELECT icud.subject_id, 130 | icud.hadm_id, 131 | icud.icustay_id, 132 | idays.seq, 133 | idays.begintime, 134 | idays.endtime 135 | FROM mimic2v26.icustay_detail icud 136 | JOIN mimic2v26.icustay_days idays 137 | ON icud.icustay_id =idays.icustay_id 138 | WHERE icud.icustay_age_group='adult' 139 | AND icud.icustay_los > 3*24*60 140 | AND idays.seq <= 3 141 | -- and icud.subject_id < 100 142 | ) 143 | --select * from all_icustay_days; 144 | , 145 | pivot_begintime AS 146 | (SELECT * 147 | FROM 148 | (SELECT subject_id, hadm_id, icustay_id, seq, begintime FROM all_icustay_days 149 | ) pivot (MIN(begintime) FOR seq IN ('1' AS begintime_day1, '2' AS begintime_day2, '3' AS begintime_day3)) 150 | ) 151 | --select * from pivot_begintime; 152 | , 153 | pivot_endtime AS 154 | (SELECT * 155 | FROM 156 | (SELECT subject_id, hadm_id, icustay_id, seq, endtime FROM all_icustay_days 157 | ) pivot (MIN(endtime) FOR seq IN ('1' AS endtime_day1, '2' AS endtime_day2, '3' AS endtime_day3)) 158 | ) --select * from pivot_endtime; 159 | ,icustay_days_in_columns AS 160 | (SELECT b.subject_id, 161 | b.hadm_id, 162 | b.icustay_id, 163 | b.begintime_day1, 164 | e.endtime_day1, 165 | b.begintime_day2, 166 | e.endtime_day2, 167 | b.begintime_day3, 168 | e.endtime_day3 169 | FROM pivot_begintime b 170 | JOIN pivot_endtime e 171 | ON b.icustay_id=e.icustay_id 172 | ), 173 | VentilatedRespParams AS 174 | (SELECT s.subject_id, 175 | s.hadm_id, 176 | s.icustay_id, 177 | CASE 178 | WHEN c.charttime BETWEEN s.begintime_day1 AND s.endtime_day1 179 | THEN 'VENTILATED_RESP_day1' 180 | WHEN c.charttime BETWEEN s.begintime_day2 AND s.endtime_day2 181 | THEN 'VENTILATED_RESP_day2' 182 | WHEN c.charttime BETWEEN s.begintime_day3 AND s.endtime_day3 183 | THEN 'VENTILATED_RESP_day3' 184 | END AS category, 185 | -1 AS valuenum -- force invalid number 186 | FROM icustay_days_in_columns s 187 | JOIN mimic2v26.chartevents c 188 | ON s.icustay_id=c.icustay_id 189 | WHERE c.charttime BETWEEN s.begintime_day1 AND s.endtime_day3 190 | AND c.itemid IN (543, 544, 545, 619, 39, 535, 683, 720, 721, 722, 732) 191 | ) select * from VentilatedRespParams; 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | ----------------------IOEVENTS-------------------------------- 200 | -- urine output, volume in ml 201 | select subject_id, icustay_id, to_char(CHARTTIME, 'yyyy-mm-dd HH24:MI:SS') CHARTTIME, volume from mimic2v26.ioevents 202 | where itemid in ( 651, 715, 55, 56, 57, 61, 65, 69, 85, 94, 96, 288, 405, 428, 473, 2042, 2068, 2111, 2119, 2130, 1922, 2810, 2859, 3053, 3462, 3519, 3175, 2366, 2463, 2507, 2510, 2592, 2676, 3966, 3987, 4132, 4253, 5927 ) 203 | AND volume IS NOT NULL 204 | order by subject_id, charttime 205 | 206 | --====================================================================== 207 | 208 | 209 | -- resp rate 210 | 211 | -- ventilation or cpap 212 | 213 | -- urine output 214 | 215 | 216 | 217 | --gcs 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | -- ===================================== 232 | select * from mimic2v26.d_labitems 233 | where lower(loinc_description) like '%lactate%' 234 | 235 | 236 | select * from mimic2v26.d_labitems 237 | where lower(loinc_description) like '%paco%' 238 | 239 | select * from mimic2v26.d_labitems 240 | where lower(test_name) like '%co%' 241 | and lower(fluid) like '%blood%' 242 | 243 | -------------------------------------------------------------------------------- /matlab/geneticalgorithm.m: -------------------------------------------------------------------------------- 1 | %AUTHOR: MOHAMMAD M. GHASSEMI 2 | %EMAIL: ghassemi@mit.edu 3 | 4 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 5 | %% NON LINEAR STUFF 6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 7 | clear all; 8 | load('Heparin_05_21_13.mat') 9 | 10 | 11 | %% To begin with, we constrained our data to include only those non-null entries 12 | %QUESTION: DO WE WANT TO ADD ANY MORE FEATURES!!! 13 | index = (CREATININBEFORE >= 0).*... 14 | (AGE >= 0).*... 15 | (GENDER >= 0).*... 16 | (SOFAADJUSTED >= 0).*... 17 | (ELIXHAUSERPT >= 0).*... 18 | (TRANSFERFLAG >= 0).*... 19 | (ICUSTAYGROUPCSRUSICU >=0).*... 20 | (DOSEBYWEIGHT >= 0).*... 21 | (PTT6HRTIMEFROMHEP >= 0).*... 22 | (ETHNICITYDESCR >= 0).*... 23 | (NUMHEPEVENTS >= 0); 24 | index = find(index ==1) 25 | 26 | %% THE OUTCOMES 27 | y = PTTVAL6HR(index) 28 | 29 | x_label = {'CREATININBEFORE',... 30 | 'AGE',... 31 | 'GENDER',... 32 | 'SOFAADJUSTED',... 33 | 'ELIXHAUSERPT',... 34 | 'ICUSTAYGROUPCSRUSICU',... %1 is MICU 35 | 'DOSEBYWEIGHT',... 36 | 'PTT6HRTIMEFROMHEP',... 37 | 'ETHNICITYDESCR',... 38 | 'NUMHEPEVENTS'}; %1 is white 39 | 40 | %THE INPUT VARIABLES 41 | x = [ CREATININBEFORE(index),... 42 | AGE(index),... 43 | GENDER(index),... 44 | SOFAADJUSTED(index),... 45 | ELIXHAUSERPT(index),... 46 | ICUSTAYGROUPCSRUSICU(index),... %1 is MICU 47 | DOSEBYWEIGHT(index),... 48 | PTT6HRTIMEFROMHEP(index),... 49 | ETHNICITYDESCR(index),... %0 is white 50 | NUMHEPEVENTS(index)]; 51 | 52 | %% FIND THE BINARY VARIABLES 53 | binary_vars = []; 54 | for(i=1:size(x,2)) 55 | if(sum(unique(x(:,i))) == 1) 56 | binary_vars = [binary_vars, i]; 57 | end 58 | end 59 | %FIND THE CONTINUOUS VARIABLES 60 | continuous_vars = setdiff([1:size(x,2)],binary_vars); 61 | %% Figure out which features should be log transformed. 62 | %To do this, we used the Lillie test for normality on each of the features 63 | %both before and after a log transform. Note that we added +1 to the data to prevent the logarithm from 64 | % Exploding. If the distribution of feature data was more normal after the log transformation than before, we transformed 65 | %the features into log form. 66 | %QUESTION: WHY DO WE WANT NORMALLY DISTRIBUTED VARIABLES?! 67 | 68 | %Check for normal 69 | for i=1:size(x,2) 70 | [h,p1(i),kstat1(i)] = lillietest(x(:,i)) 71 | end 72 | 73 | %check for log normal 74 | for i=1:size(x,2) 75 | [h,p2(i),kstat2(i)] = lillietest(log(x(:,i)+1)); 76 | end 77 | 78 | vars_needing_log_transform = intersect(find(kstat2 < kstat1),continuous_vars); 79 | 80 | %% Log transform the variables that take a more normal form when log transformed. 81 | for i=vars_needing_log_transform 82 | x(:,i) = log(x(:,i)+1); 83 | end 84 | 85 | %% Normalize all the continuous variables to mean 0 and unit variace 86 | y = (y - mean(y))/std(y); %The output 87 | for i=continuous_vars 88 | x(:,i) =(x(:,i) - mean(x(:,i)))/std(x(:,i)); %The input 89 | end 90 | 91 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 92 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 93 | %% GENETIC ALGORITHM 94 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 95 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 96 | clearvars -except x y 97 | 98 | %%CHANGE THE X and Y Vars so that the data is positive 99 | x = x +10; 100 | y = y +10; 101 | 102 | %NEWTONIAN INITAL GUESS OPTIMIZATION: 103 | num_points_to_test = 50; 104 | lower_bound = -10; 105 | upper_bound = 10; 106 | ms = MultiStart; 107 | ms.UseParallel = 'always'; 108 | ms.Display = 'off'; 109 | 110 | 111 | %INPUT PARAMETERS 112 | %General form of the model is b1*x1^k1 + ... + bn*xn^kn 113 | num_params = 2*size(x,2) + 1; %The number of features 114 | num_models = 500; %The number of models in the population 115 | num_generations = 50; %The number of generations you want for the models 116 | mutation_rate = .01; %some number between 0 and 1 where 0 means no mutations next gen, and 1 means will completely mutate. 117 | 118 | sexual = 1; %denotes sexual reproduction. 119 | top_perc = 0.1; %What top % survive 120 | dominent_gene_strength = .5; 121 | 122 | opts = statset('TolFun',1e-8,'MaxIter',100); %The tolerance for convergence of the non-linear solver, and the %Max-Iterations 123 | warning off; 124 | 125 | matlabpool open; 126 | pctRunOnAll warning off; 127 | for k = 1:num_generations 128 | 129 | %In the first iteration- we will need to create a random population 130 | if(k == 1) 131 | genes = randi(2,num_params,num_models)-1; %create genomes, in length 132 | genes(:,1) = [1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]'; %Ensure that the linear model is one of the models we are looking at. 133 | end 134 | %gene2model(x,genes(:,1)) 135 | 136 | 137 | %EVALUATE THE GENOME MODELS 138 | tic 139 | clear lse 140 | parfor i = 1:num_models %Generate the models 141 | [models{i}, bval(i)] = gene2model(x,genes(:,i)); %calling the gene2modelfunction 142 | try 143 | i 144 | %THIS WILL DO A NON-LINEAR SOLVE FOR THE BEST PARAMETERS AND 145 | %SPIT OUT A 2 FOLD CROSS VALIDATION MSE 146 | [lse(i)] = evalModel(x,y,bval(i),opts,models{i}, i,num_points_to_test,upper_bound,lower_bound,ms); %evaluate the models 147 | catch 148 | %If y the model screws 149 | lse(i)= Inf; 150 | 'bad apple' 151 | end 152 | end 153 | toc 154 | 155 | 156 | %NOW THAT WE HAVE GENERATED SOME MODELS, WE JUDGE THEIR FITNESS USING 157 | %LSE OF THE FIT ON THE NOVEL DATA. 158 | [sorted_lse ranked_genes] = sort(lse,'ascend'); 159 | 160 | %FOR THE ASEXUAL POPULATION - EXTRACT THE TOP 10% 161 | 162 | %if(sexual == 0) 163 | % best_genes = (:,ranked_genes(1:(num_models*.1))); 164 | % new_genes = best_genes; 165 | % %Take some mutations as well. 166 | % for i= 1:9 167 | % mutations = rand(size(best_genes,1),size(best_genes,2)) < mutation_rate; 168 | % new_genes = [new_genes,xor(best_genes,mutations)]; 169 | % end 170 | %end 171 | 172 | %no dominent or recessive tradeoff 173 | clear best_genes; 174 | if(sexual == 1) 175 | best_genes = genes(:,ranked_genes(1:(num_models*top_perc))); 176 | new_genes = repmat(best_genes,1,1/top_perc); 177 | 178 | %everyone get's to breed - they are called moms 179 | moms = new_genes; 180 | %Who they choose to breed with are the dads. Like human mating, the 181 | %males are at a distinct disadvantage. 182 | dads = new_genes(:,randi(size(new_genes,2),1,size(new_genes,2))); 183 | 184 | %Dominent and recessive traits 185 | dominent_traits = dads.*moms; 186 | recessive_traits = abs(dads-moms).*((rand(size(dominent_traits,1),size(dominent_traits,2)))<=dominent_gene_strength); 187 | 188 | new_genes = dominent_traits + recessive_traits; 189 | 190 | %Take some mutations as well. 191 | mutations = rand(size(new_genes,1),size(new_genes,2)) < mutation_rate; 192 | new_genes(mutations) = not(new_genes(mutations)); 193 | end 194 | 195 | %Now update this generations genes 196 | genes = new_genes; 197 | genes(:,1) = [1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]'; 198 | 199 | %Record the best rsquared from this generation 200 | best_lse(k) = sorted_lse(1); 201 | winning_genes(:,k) = genes(:,ranked_genes(1)); 202 | save best_lse best_lse 203 | save winning_genes winning_genes 204 | 205 | hold on; 206 | plot(k,best_lse(k),'.') 207 | 208 | end 209 | matlabpool close; 210 | 211 | 212 | 213 | 214 | %% NOW LET'S LOOK AT THE WINNING GENES AND RUN AN OPTIMIZER ON THEM TO SEE HOW THEY DO. 215 | clearvars -except x y 216 | x = x+10; y = y+10; 217 | load winning_genes; 218 | 219 | winning_genes = winning_genes(:,48) 220 | winning_model = gene2model(x,winning_genes) 221 | 222 | ms = MultiStart 223 | ms.UseParallel = 'always'; 224 | 225 | num_points_to_test = 10000 226 | ms = MultiStart 227 | ms.UseParallel = 'always'; 228 | 229 | model = winning_model; 230 | 231 | Indices = crossvalind('Kfold', size(y,1), 10); %Generate the indicies 232 | for(i=1:10) 233 | num2str(i*10) 234 | testing_ind = Indices == i; %testing indicies (10%) 235 | training_ind = not(testing_ind); %training indicies (90%) 236 | 237 | %SET UP THE PROBLEM 238 | problem = createOptimProblem('lsqcurvefit',... 239 | 'objective', model,... 240 | 'xdata',x(training_ind,:),... 241 | 'ydata',y(training_ind),... 242 | 'x0',ones(1,11),... 243 | 'lb',-50*ones(1,11),... 244 | 'ub',50*ones(1,11)) 245 | 246 | %Evaluate the function at many initial Guesses, and take the coefficients that fit best 247 | [b,fval,exitflag,output,solutions] = run(ms,problem,num_points_to_test); 248 | coefficients(:,i) = b; 249 | save coefficients coefficients 250 | %!! MAYBE YOU SHOULD REMOVE THE COOKS D OUTLIERS 251 | 252 | %least_square_error of the model on the testing data. 253 | lse(i) = sum((model(b,x(testing_ind,:)) - y(testing_ind)).^2); 254 | save lse lse 255 | end 256 | 257 | X = coefficients'; 258 | 259 | %And we stepped up the number of components in the kmeans calssifer until 2 260 | %of the means came close. We set the number of replicates to 10, and chose 261 | %the one with the best total sum of distances. 262 | 263 | %!! SHOW A PROJECTION FROM THE 11 DIMENTIONAL SPACE INTO A 2 DIMENTIONAL 264 | %SPACE. 265 | opts = statset('Display','final'); 266 | [idx,ctrs] = kmeans(X,2,... 267 | 'distance','sqEuclidean',... 268 | 'Replicates',10,... 269 | 'Options',opts); 270 | 271 | %Let's generate the predictions for each of the models 272 | model_1_predictions = model(ctrs(1,:), x); 273 | [m1_r2 m1_rmse] = rsquare(y,model_1_predictions) 274 | model_2_predictions = model(ctrs(2,:), x); 275 | [M2_r2 m2_rmse] = rsquare(y,model_2_predictions) 276 | 277 | for i= 1:10 278 | coefficients(:,i) 279 | predictions = model(coefficients(:,i),x); 280 | plot(y,predictions,'.') 281 | hold on; 282 | plot([8 12],[8 12],'--r') 283 | waitforbuttonpress 284 | 285 | end 286 | 287 | [r2 rmse] = rsquare(y,predictions) 288 | 289 | 290 | -------------------------------------------------------------------------------- /sql/lab_values.sql: -------------------------------------------------------------------------------- 1 | 2 | create materialized view mimic2v26_48hr_vitals_labs as 3 | 4 | with labs_analysis as ( 5 | select * 6 | from 7 | (select subject_id, hadm_id, icustay_id, category, valuenum from tbrennan.mimic2v26_labs_raw_48hr) 8 | pivot (count(valuenum) 9 | for category in ( 10 | 'HCT' as hct_no, 11 | 'WBC' as wbc_no, 12 | 'GLUCOSE' as glucose_no, 13 | 'HCO3' as hco3_no, 14 | 'POTASSIUM' as k_no, 15 | 'SODIUM' as na_no, 16 | 'BUN' as bun_no, 17 | 'CREATININE' as creat_no, 18 | 'PH' as ph_no, 19 | 'LACTATE' as lactate_no, 20 | 'TROPONIN' as troponin_no, 21 | 'PLATELETS' as platelets_no, 22 | 'PO2' as po2_no, 23 | 'HGB' as hgb_no, 24 | 'BNP' as bnp_no, 25 | 'CHLORIDE' as cl_no 26 | ) 27 | ) 28 | 29 | ) 30 | /* 31 | select median(hct_no), stddev(hct_no), 32 | median(wbc_no), stddev(wbc_no), 33 | median(glucose_no), stddev(glucose_no), 34 | median(hco3_no), stddev(hco3_no), 35 | median(k_no), stddev(k_no), 36 | median(na_no), stddev(na_no), 37 | median(bun_no), stddev(bun_no), 38 | median(creat_no), stddev(creat_no), 39 | median(ph_no), stddev(ph_no), 40 | median(lactate_no), stddev(lactate_no), 41 | median(troponin_no), stddev(troponin_no), 42 | median(platelets_no), stddev(platelets_no), 43 | median(po2_no), stddev(po2_no), 44 | median(hgb_no), stddev(hgb_no), 45 | median(bnp_no), stddev(bnp_no), 46 | median(cl_no), stddev(cl_no) 47 | from labs_analysis; 48 | */ 49 | 50 | , labs_first_raw as ( 51 | select distinct subject_id, hadm_id, icustay_id, 52 | category, 53 | first_value(valuenum) over (partition by icustay_id, category order by post_adm) first_lab 54 | from tbrennan.mimic2v26_labs_raw_48hr 55 | ) 56 | --select * from labs_first_raw order by icustay_id; 57 | 58 | , labs_first as ( 59 | select * 60 | from 61 | (select * from labs_first_raw) 62 | pivot (median(first_lab) 63 | for category in ( 64 | 'HCT' as hct_first, 65 | 'WBC' as wbc_first, 66 | 'GLUCOSE' as glucose_first, 67 | 'HCO3' as hco3_first, 68 | 'POTASSIUM' as k_first, 69 | 'SODIUM' as na_first, 70 | 'BUN' as bun_first, 71 | 'CREATININE' as creat_first, 72 | 'PH' as ph_first, 73 | 'LACTATE' as lactate_first, 74 | 'TROPONIN' as troponin_first, 75 | 'PLATELETS' as platelets_first, 76 | 'PO2' as po2_first, 77 | 'HGB' as hgb_first, 78 | 'BNP' as bnp_first, 79 | 'CHLORIDE' as cl_first 80 | )) 81 | ) 82 | --select * from labs_first; 83 | 84 | , labs_med_raw as ( 85 | select distinct subject_id, hadm_id, icustay_id, 86 | category, 87 | median(valuenum) over (partition by icustay_id, category) med_lab 88 | from tbrennan.mimic2v26_labs_raw_48hr 89 | ) 90 | --select * from labs_med_raw order by icustay_id; 91 | 92 | , labs_med as ( 93 | select * 94 | from 95 | (select * from tbrennan.labs_med_raw) 96 | pivot (median(med_lab) 97 | for category in ( 98 | 'HCT' as hct_med, 99 | 'WBC' as wbc_med, 100 | 'GLUCOSE' as glucose_med, 101 | 'HCO3' as hco3_med, 102 | 'POTASSIUM' as k_med, 103 | 'SODIUM' as na_med, 104 | 'BUN' as bun_med, 105 | 'CREATININE' as creat_med, 106 | 'PH' as ph_med, 107 | 'LACTATE' as lactate_med, 108 | 'TROPONIN' as troponin_med, 109 | 'PLATELETS' as platelets_med, 110 | 'PO2' as po2_med, 111 | 'HGB' as hgb_med, 112 | 'BNP' as bnp_med, 113 | 'CHLORIDE' as cl_med 114 | )) 115 | ) 116 | --select * from labs_med order by icustay_id; 117 | 118 | , labs_min_raw as ( 119 | select distinct subject_id, hadm_id, icustay_id, 120 | category, 121 | min(valuenum) over (partition by icustay_id, category) min_lab 122 | from tbrennan.mimic2v26_labs_raw_48hr 123 | ) 124 | 125 | 126 | , labs_min as ( 127 | select * 128 | from 129 | (select * from labs_min_raw) 130 | pivot (median(min_lab) 131 | for category in ( 132 | 'HCT' as hct_min, 133 | 'WBC' as wbc_min, 134 | 'GLUCOSE' as glucose_min, 135 | 'HCO3' as hco3_min, 136 | 'POTASSIUM' as k_min, 137 | 'SODIUM' as na_min, 138 | 'BUN' as bun_min, 139 | 'CREATININE' as creat_min, 140 | 'PH' as ph_min, 141 | 'LACTATE' as lactate_min, 142 | 'TROPONIN' as troponin_min, 143 | 'PLATELETS' as platelets_min, 144 | 'PO2' as po2_min, 145 | 'HGB' as hgb_min, 146 | 'BNP' as bnp_min, 147 | 'CHLORIDE' as cl_min 148 | )) 149 | ) 150 | --select * from labs_min order by icustay_id; 151 | 152 | , labs_max_raw as ( 153 | select distinct subject_id, hadm_id, icustay_id, 154 | category, 155 | max(valuenum) over (partition by icustay_id, category) max_lab 156 | from tbrennan.mimic2v26_labs_raw_48hr 157 | ) 158 | 159 | , labs_max as ( 160 | select * 161 | from 162 | (select * from labs_max_raw) 163 | pivot (median(max_lab) 164 | for category in ( 165 | 'HCT' as hct_max, 166 | 'WBC' as wbc_max, 167 | 'GLUCOSE' as glucose_max, 168 | 'HCO3' as hco3_max, 169 | 'POTASSIUM' as k_max, 170 | 'SODIUM' as na_max, 171 | 'BUN' as bun_max, 172 | 'CREATININE' as creat_max, 173 | 'PH' as ph_max, 174 | 'LACTATE' as lactate_max, 175 | 'TROPONIN' as troponin_max, 176 | 'PLATELETS' as platelets_max, 177 | 'PO2' as po2_max, 178 | 'HGB' as hgb_max, 179 | 'BNP' as bnp_max, 180 | 'CHLORIDE' as cl_max 181 | )) 182 | ) 183 | --select * from labvalues_max; 184 | 185 | , labs_std_raw as ( 186 | select distinct subject_id, hadm_id, icustay_id, 187 | category, 188 | round(stddev(valuenum) over (partition by icustay_id, category),2) std_lab 189 | from tbrennan.mimic2v26_labs_raw_48hr 190 | ) 191 | 192 | , labs_std as ( 193 | select * 194 | from 195 | (select * from labs_std_raw) 196 | pivot (median(std_lab) 197 | for category in ( 198 | 'HCT' as hct_std, 199 | 'WBC' as wbc_std, 200 | 'GLUCOSE' as glucose_std, 201 | 'HCO3' as hco3_std, 202 | 'POTASSIUM' as k_std, 203 | 'SODIUM' as na_std, 204 | 'BUN' as bun_std, 205 | 'CREATININE' as creat_std, 206 | 'PH' as ph_std, 207 | 'LACTATE' as lactate_std, 208 | 'TROPONIN' as troponin_std, 209 | 'PLATELETS' as platelets_std, 210 | 'PO2' as po2_std, 211 | 'HGB' as hgb_std, 212 | 'BNP' as bnp_std, 213 | 'CHLORIDE' as cl_std 214 | )) 215 | ) 216 | --select * from labs_std order by icustay_id; 217 | 218 | , labs_slp_raw as ( 219 | select distinct subject_id, hadm_id, icustay_id, 220 | category, 221 | round(regr_slope(post_adm,valuenum) over (partition by icustay_id, category),2) slp_lab 222 | from tbrennan.mimic2v26_labs_raw_48hr 223 | ) 224 | 225 | , labs_slp as ( 226 | select * 227 | from 228 | (select * from labs_slp_raw) 229 | pivot (median(slp_lab) 230 | for category in ( 231 | 'HCT' as hct_slp, 232 | 'WBC' as wbc_slp, 233 | 'GLUCOSE' as glucose_slp, 234 | 'HCO3' as hco3_slp, 235 | 'POTASSIUM' as k_slp, 236 | 'SODIUM' as na_slp, 237 | 'BUN' as bun_slp, 238 | 'CREATININE' as creat_slp, 239 | 'PH' as ph_slp, 240 | 'LACTATE' as lactate_slp, 241 | 'TROPONIN' as troponin_slp, 242 | 'PLATELETS' as platelets_slp, 243 | 'PO2' as po2_slp, 244 | 'HGB' as hgb_slp, 245 | 'BNP' as bnp_slp, 246 | 'CHLORIDE' as cl_slp 247 | )) 248 | ) 249 | --select * from labs_slp order by icustay_id; 250 | 251 | , final as ( 252 | select distinct 253 | fc.*, 254 | 255 | lf.hct_first, 256 | ld.hct_med, 257 | ln.hct_min, 258 | lx.hct_max, 259 | ls.hct_std, 260 | lp.hct_slp, 261 | 262 | lf.wbc_first, 263 | ld.wbc_med, 264 | ln.wbc_min, 265 | lx.wbc_max, 266 | ls.wbc_std, 267 | lp.wbc_slp, 268 | 269 | lf.glucose_first, 270 | ld.glucose_med, 271 | ln.glucose_min, 272 | lx.glucose_max, 273 | ls.glucose_std, 274 | lp.glucose_slp, 275 | 276 | lf.hco3_first, 277 | ld.hco3_med, 278 | ln.hco3_min, 279 | lx.hco3_max, 280 | ls.hco3_std, 281 | lp.hco3_slp, 282 | 283 | lf.k_first, 284 | ld.k_med, 285 | ln.k_min, 286 | lx.k_max, 287 | ls.k_std, 288 | lp.k_slp, 289 | 290 | lf.na_first, 291 | ld.na_med, 292 | ln.na_min, 293 | lx.na_max, 294 | ls.na_std, 295 | lp.na_slp, 296 | 297 | lf.bun_first, 298 | ld.bun_med, 299 | ln.bun_min, 300 | lx.bun_max, 301 | ls.bun_std, 302 | lp.bun_slp, 303 | 304 | lf.creat_first, 305 | ld.creat_med, 306 | ln.creat_min, 307 | lx.creat_max, 308 | ls.creat_std, 309 | lp.creat_slp, 310 | 311 | lf.ph_first, 312 | ld.ph_med, 313 | ln.ph_min, 314 | lx.ph_max, 315 | ls.ph_std, 316 | lp.ph_slp, 317 | 318 | lf.lactate_first, 319 | ld.lactate_med, 320 | ln.lactate_min, 321 | lx.lactate_max, 322 | ls.lactate_std, 323 | lp.lactate_slp, 324 | 325 | lf.troponin_first, 326 | ld.troponin_med, 327 | ln.troponin_min, 328 | lx.troponin_max, 329 | ls.troponin_std, 330 | lp.troponin_slp, 331 | 332 | lf.platelets_first, 333 | ld.platelets_med, 334 | ln.platelets_min, 335 | lx.platelets_max, 336 | ls.platelets_std, 337 | lp.platelets_slp, 338 | 339 | lf.po2_first, 340 | ld.po2_med, 341 | ln.po2_min, 342 | lx.po2_max, 343 | ls.po2_std, 344 | lp.po2_slp, 345 | 346 | lf.hgb_first, 347 | ld.hgb_med, 348 | ln.hgb_min, 349 | lx.hgb_max, 350 | ls.hgb_std, 351 | lp.hgb_slp, 352 | 353 | lf.bnp_first, 354 | ld.bnp_med, 355 | ln.bnp_min, 356 | lx.bnp_max, 357 | ls.bnp_std, 358 | lp.bnp_slp, 359 | 360 | lf.cl_first, 361 | ld.cl_med, 362 | ln.cl_min, 363 | lx.cl_max, 364 | ls.cl_std, 365 | lp.cl_slp 366 | 367 | from tbrennan.mimic2v26_24hr_vital_signs fc 368 | join labs_first lf on fc.icustay_id = lf.icustay_id 369 | join labs_med ld on fc.icustay_id = ld.icustay_id 370 | join labs_min ln on fc.icustay_id = ln.icustay_id 371 | join labs_max lx on fc.icustay_id = lx.icustay_id 372 | join labs_std ls on fc.icustay_id = ls.icustay_id 373 | join labs_slp lp on fc.icustay_id = lp.icustay_id 374 | ) 375 | select * from final; -------------------------------------------------------------------------------- /sql/ards_data_extraction_may13.sql: -------------------------------------------------------------------------------- 1 | /* 2 | ards_data_extraction 3 | To extract related data for the cohort for ARDS study 4 | Created on : Mar 2013 by Mornin Feng 5 | Last updated : May 2013 by mornin 6 | 7 | */ 8 | --drop materialized view ards_data_v3; 9 | --drop table ards_data_v3_1; 10 | --drop table ards_data_mar13_v2; 11 | --create table ards_data_Apr13 as 12 | 13 | 14 | drop table ards2_data_may13; 15 | create table ards2_data_may13 as 16 | with pnuemonia_group as 17 | (select distinct ards.icustay_id, 1 as flg 18 | from mornin.ards2_cohort_may13 ards join mimic2v26.icd9 icd9 19 | on ards.hadm_id=icd9.hadm_id 20 | where code 21 | in ('003.22', '020.3', '020.4', '020.5', '021.2', '022.1', '031.0', '039.1', '052.1', '055.1', '073.0', '083.0', '112.4', '114.0', '114.4', '114.5', '115.05', '115.15', '115.95', '130.4' 22 | ,'136.3', '480.0', '480.1', '480.2', '480.3', '480.8', '480.9', '481', '482.0', '482.1', '482.2', '482.3', '482.30', '482.31', '482.32', '482.39', '482.4', '482.40', '482.41', '482.42' 23 | ,'482.49', '482.8', '482.81', '482.82', '482.83', '482.84', '482.89', '482.9', '483', '483.0', '483.1', '483.8', '484.1', '484.3', '484.5', '484.6', '484.7', '484.8', '485', '486' 24 | ,'513.0', '517.1') 25 | ) 26 | --select count(*) from pnuemonia_group; --1053 27 | 28 | , sepsis_group as 29 | (select distinct ards.icustay_id, 1 as flg 30 | from mornin.ards2_cohort_may13 ards join mimic2devel.martin_sepsis_admissions sep 31 | on ards.hadm_id = sep.hadm_id 32 | ) 33 | --select count(*) from sepsis_group; --784 34 | 35 | , vent_time as 36 | (select 37 | distinct ards.icustay_id 38 | , sum(round((extract(day from (vent.end_time-vent.begin_time))+ 39 | extract(hour from (vent.end_time-vent.begin_time))/24+ 40 | extract(minute from (vent.end_time-vent.begin_time))/60/24), 3)) as vent_time_day 41 | from mornin.ards2_cohort_may13 ards 42 | join mimic2devel.ventilation vent 43 | on ards.icustay_id = vent.icustay_id 44 | group by ards.icustay_id 45 | order by 1 46 | ) 47 | 48 | --select * from vent_time; 49 | --select count(*) from vent_time; 50 | --where vent_time_day<=0; 51 | 52 | , survive_day as 53 | (select 54 | ards.icustay_id 55 | , (case when d.dod is null then 730 -- 2 years 56 | when (extract(day from (d.dod-ards.icustay_intime))+extract(hour from (d.dod-ards.icustay_intime))/24)>730 then 730 57 | else round((extract(day from (d.dod-ards.icustay_intime))+ 58 | extract(hour from (d.dod-ards.icustay_intime))/24), 3) 59 | end)as survival_day 60 | , (case when d.dod is null then 1 61 | when (extract(day from (d.dod-ards.icustay_intime))+extract(hour from (d.dod-ards.icustay_intime))/24)>730 then 1 62 | else 0 end) as sensor_flg 63 | from mornin.ards2_cohort_may13 ards 64 | join mimic2devel.d_patients d 65 | on ards.subject_id=d.subject_id 66 | ) 67 | 68 | --select * from survive_day order by 2 desc; 69 | 70 | 71 | --------------------- icd9 groupings -------------------------------------- 72 | , icd9_group as 73 | (select 74 | icustay_id 75 | , replace(ICD9_GROUPINGS, ' ','') as icd9_group 76 | from mornin.ARDS_ICD9_GROUPING 77 | ) 78 | 79 | , icd9_codes as 80 | (select distinct 81 | icd9.code 82 | , icd9.description 83 | , gp.icd9_group 84 | from icd9_group gp 85 | join mimic2v26.icustay_detail icud on gp.icustay_id=icud.icustay_id 86 | join mimic2v26.icd9 icd9 on icd9.hadm_id=icud.hadm_id and icd9.sequence=1 87 | where gp.icd9_group in ('malignancy', 'neurological', 'headinjury') 88 | order by 3 89 | ) 90 | 91 | --select * from icd9_codes; 92 | , icd9_flg as 93 | (select 94 | ards.icustay_id 95 | , case when icd9.code in (select code from icd9_codes where icd9_group='malignancy') then 1 else 0 end as malignancy_flg 96 | , case when icd9.code in (select code from icd9_codes where icd9_group='neurological') then 1 else 0 end as neurological_flg 97 | , case when icd9.code in (select code from icd9_codes where icd9_group='headinjury') then 1 else 0 end as headinjury_flg 98 | from mornin.ards2_cohort_may13 ards 99 | join mimic2v26.icd9 icd9 on icd9.hadm_id=ards.hadm_id and icd9.sequence=1 100 | ) 101 | 102 | --select * from icd9_flg; 103 | 104 | ,demo_data as 105 | (select 106 | ards.subject_id 107 | , ards.hadm_id 108 | , ards.icustay_id 109 | , ards.icustay_intime 110 | -- 111 | , ards.category 112 | , (case when ards.category='ADMIN' then 1 when ards.category='LATE' then 2 else 0 end) as category_num 113 | -- 114 | , ards.ARDS_SEVERITY 115 | , (case when ards.ARDS_SEVERITY = 'MILD' then 1 116 | when ards.ARDS_SEVERITY = 'MODERATE' then 2 117 | when ards.ARDS_SEVERITY = 'SEVERE' then 3 118 | else 0 119 | end) as ARDS_SEVERITY_NUM 120 | -- 121 | , ards.day_to_onset 122 | 123 | -- 124 | , icud.gender as gender 125 | ,case when icud.gender='M' then 1 else 0 end as gender_num 126 | ,case when icud.ICUSTAY_ADMIT_AGE>120 then 91.4 else icud.ICUSTAY_ADMIT_AGE end as age --correction for age 127 | ,icud.WEIGHT_FIRST 128 | ,icud.HEIGHT 129 | -- 130 | ,icud.SAPSI_FIRST 131 | ,icud.SOFA_FIRST 132 | , elix.TWENTY_EIGHT_DAY_MORT_PT as elix_mimic_28_day 133 | , elix.ONE_YR_MORT_PT as elix_mimic_1_year 134 | , elix.TWO_YR_MORT_PT as elix_mimic_2_year 135 | , elix.TWO_YEAR_SURVIVAL_PT as elix_mimic_surv 136 | -- 137 | , ards.service_unit 138 | , case when service_unit is null then null 139 | when service_unit='CCU' then 3 140 | when service_unit='CSRU' then 2 141 | when service_unit='SICU' then 1 142 | else 0 end as service_unit_num 143 | , ards.chf_flg 144 | , ards.vent_48hr_flg 145 | , case when pnu.flg is null then 0 else 1 end as pnuemonia_flg 146 | , case when sep.flg is null then 0 else 1 end as sepsis_flg 147 | , icd9.malignancy_flg 148 | , icd9.neurological_flg 149 | , icd9.headinjury_flg 150 | -- 151 | , (case when icud.ICUSTAY_EXPIRE_FLG='Y' then 1 else 0 end) as icu_exp_flg 152 | ,round(icud.ICUSTAY_LOS/60/24,3) as icustay_los 153 | -- 154 | , v.vent_time_day 155 | , s.survival_day 156 | , s.sensor_flg 157 | , (case when s.survival_day<=28 then 1 else 0 end) as mort_28_day 158 | , (case when s.survival_day<=365 then 1 else 0 end) as mort_1_year 159 | , (case when s.survival_day<730 then 1 else 0 end) as mort_2_year 160 | from mornin.ARDS2_COHORT_may13 ards 161 | join mimic2v26.icustay_detail icud on ards.icustay_id=icud.icustay_id 162 | left join MIMIC2DEVEL.elixhauser_points elix on elix.hadm_id=ards.hadm_id 163 | left join pnuemonia_group pnu on pnu.icustay_id=ards.icustay_id 164 | left join sepsis_group sep on sep.icustay_id=ards.icustay_id 165 | left join icd9_flg icd9 on icd9.icustay_id=ards.icustay_id 166 | left join vent_time v on ards.icustay_id=v.icustay_id 167 | left join survive_day s on ards.icustay_id=s.icustay_id 168 | ) 169 | select * from demo_data; 170 | --select count(*) from demo_data where sepsis_flg=1; 171 | --select sum(sensor_flg*mort_2_year) from demo_data; 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | /*********************************** remove missing values ********************/ 181 | drop table ards2_surv_data_may13; 182 | create table ards2_surv_data_may13 as 183 | select * 184 | from mornin.ards2_data_may13 185 | where 186 | --chf_flg=0 -- exclude chf patients 187 | --and 188 | service_unit_num<2 --exclude csru and ccu 189 | and gender_num is not null 190 | and age is not null 191 | and weight_first is not null 192 | and sapsi_first is not null 193 | and elix_mimic_surv is not null 194 | and sepsis_flg is not null 195 | and malignancy_flg is not null 196 | and neurological_flg is not null 197 | and headinjury_flg is not null; 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | --------------------------- correct for missing value ------------------------- 209 | --drop table ards_data_mar13_nonan; 210 | create table ards_data_Apr13_named as 211 | select 212 | ICUSTAY_ID 213 | ,CATEGORY 214 | ,CATEGORY_NUM 215 | ,ADMIN_FLG 216 | ,LATE_FLG 217 | ,ARDS_SEVERITY 218 | , ARDS_SEVERITY_NUM 219 | /*,SEVER_GROUP 220 | ,SEVER_1_FLG 221 | ,SEVER_2_FLG 222 | ,SEVER_3_FLG 223 | ,SEVER_4_FLG 224 | ,SEVER_5_FLG 225 | ,SEVER_6_FLG*/ 226 | ,DAY_TO_ONSET 227 | ,SUBJECT_ID 228 | ,HADM_ID 229 | ,GENDER 230 | ,GENDER_NUM 231 | , (case 232 | when age is null then (select median(age) from mornin.ARDS_DATA_MAR13) 233 | else age end) as AGE 234 | , (case 235 | when WEIGHT_FIRST is null then (select median(WEIGHT_FIRST) from mornin.ARDS_DATA_MAR13) 236 | else WEIGHT_FIRST end) as WEIGHT_FIRST 237 | , (case 238 | when HEIGHT is null then (select median(HEIGHT) from mornin.ARDS_DATA_MAR13) 239 | else HEIGHT end) as HEIGHT 240 | , (case 241 | when SAPSI_FIRST is null then (select median(SAPSI_FIRST) from mornin.ARDS_DATA_MAR13) 242 | else SAPSI_FIRST end) as SAPSI_FIRST 243 | , (case 244 | when SOFA_FIRST is null then (select median(SOFA_FIRST) from mornin.ARDS_DATA_MAR13) 245 | else SOFA_FIRST end) as SOFA_FIRST 246 | ,ICU_EXP_FLG 247 | ,ICUSTAY_LOS 248 | ,ICUSTAY_FIRST_SERVICE 249 | ,MICU_FLG 250 | ,ICUSTAY_INTIME 251 | ,VENT_TIME_DAY 252 | ,SEPSIS_FLG 253 | , (case 254 | when ELIX_28_DAY is null then (select median(ELIX_28_DAY) from mornin.ARDS_DATA_MAR13) 255 | else ELIX_28_DAY end) as ELIX_28_DAY 256 | , (case 257 | when ELIX_1_YEAR is null then (select median(ELIX_1_YEAR) from mornin.ARDS_DATA_MAR13) 258 | else ELIX_1_YEAR end) as ELIX_1_YEAR 259 | , (case 260 | when ELIX_2_YEAR is null then (select median(ELIX_2_YEAR) from mornin.ARDS_DATA_MAR13) 261 | else ELIX_2_YEAR end) as ELIX_2_YEAR 262 | , (case 263 | when ELIX_SURV is null then (select median(ELIX_SURV) from mornin.ARDS_DATA_MAR13) 264 | else ELIX_SURV end) as ELIX_SURV 265 | , (case 266 | when ELIX_MIMIC_28_DAY is null then (select median(ELIX_MIMIC_28_DAY) from mornin.ARDS_DATA_MAR13) 267 | else ELIX_MIMIC_28_DAY end) as ELIX_MIMIC_28_DAY 268 | , (case 269 | when ELIX_MIMIC_1_YEAR is null then (select median(ELIX_MIMIC_1_YEAR) from mornin.ARDS_DATA_MAR13) 270 | else ELIX_MIMIC_1_YEAR end) as ELIX_MIMIC_1_YEAR 271 | , (case 272 | when ELIX_MIMIC_2_YEAR is null then (select median(ELIX_MIMIC_2_YEAR) from mornin.ARDS_DATA_MAR13) 273 | else ELIX_MIMIC_2_YEAR end) as ELIX_MIMIC_2_YEAR 274 | , (case 275 | when ELIX_MIMIC_SURV is null then (select median(ELIX_MIMIC_SURV) from mornin.ARDS_DATA_MAR13) 276 | else ELIX_MIMIC_SURV end) as ELIX_MIMIC_SURV 277 | ,ICD9_GROUP 278 | ,ICD9_INF 279 | ,ICD9_MAL 280 | ,ICD9_NEU 281 | ,ICD9_CAR 282 | ,ICD9_RES 283 | ,ICD9_GAS 284 | ,ICD9_TRA 285 | ,ICD9_HEA 286 | ,SURVIVAL_DAY 287 | ,MORT_28_DAY 288 | ,MORT_1_YEAR 289 | ,MORT_2_YEAR 290 | ,SENSOR_FLG 291 | from mornin.ARDS_DATA_Apr13; 292 | 293 | --select * from mornin.ARDS_DATA_MAR13; 294 | 295 | ---------------- Add in admission date --------------------------------- 296 | create table mornin.ards_admin_dt as 297 | select 298 | ards.subject_id 299 | --, (case when hr_to_onset is null then 735 else round(ards.hr_to_onset/24, 3) end) as day_to_onset --735 means never onsets 300 | , to_char(adm.admit_dt, 'dd-mm-yyyy') as admin_date 301 | from mornin.ARDS_DATA_MAR13_NONAN ards 302 | left join mimic2v26.admissions adm on ards.hadm_id=adm.hadm_id; 303 | 304 | -------------- Output data with adminssion year group: 0: 2001~2002; 1: 2003; 2: 2004; 3: 2005; 4: 2006~2007 --------------------------- 305 | drop table mornin.ARDS_DATA_MAR13_NONNAN_V2; 306 | 307 | create table mornin.ARDS_DATA_MAR13_NONNAN_V2 as 308 | select 309 | ards.* 310 | , (case when ards.sever_group in (1,4) then 1 311 | when ards.sever_group in (2,5) then 2 312 | when ards.sever_group in (3,6) then 3 313 | else 0 314 | end) as sever_3grp 315 | , (case when hr_to_onset is null then 735 else round(ards.hr_to_onset/24, 3) end) as day_to_onset --735 means never onsets 316 | , adm.admin_yr_grp 317 | from mornin.ARDS_DATA_MAR13_NONAN ards 318 | left join mornin.ards_admin_yr adm on adm.subject_id=ards.subject_id; -------------------------------------------------------------------------------- /sql/physiological_var.sql: -------------------------------------------------------------------------------- 1 | --created by mpimentel, GA-GMM project 2 | -- Last Updated: August 2013 3 | 4 | drop materialized view mimic2v26_48hr_vital_signs; 5 | 6 | create materialized view mimic2v26_48hr_vital_signs as 7 | 8 | with cohort as ( 9 | select 10 | subject_id, 11 | hadm_id, 12 | icustay_id, 13 | icustay_seq, 14 | icustay_intime, 15 | weight_first weight, 16 | sapsi_first sapsi 17 | from MIMIC2V26.icustay_detail cd 18 | where icustay_los > 24*60 19 | and hadm_id is not null 20 | and icustay_id is not null 21 | and weight_first is not null 22 | and sapsi_first is not null 23 | ) 24 | --select * from cohort order by subject_id; --19,550 rows 25 | 26 | -- Datatype: 27 | -- 1 - NBP Systolic, 2 - NBP Diastolic, 3 - NBP MAP, 4 - NBP PP 28 | -- 5 - IBP Systolic, 6 - IBP Diastolic, 7 - IBP MAP, 8 - IBP PP 29 | -- 9 - HR, 10 - Central Venous Pressure (CVP), 11 - SpO2, 12 - RR 30 | -- 13 - urine output, 14 - temperature 31 | 32 | , sysbp_raw as ( 33 | select fc.subject_id 34 | , fc.hadm_id 35 | , fc.icustay_id 36 | , fc.icustay_intime 37 | , extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) as post_adm 38 | , ce.value1num val 39 | , ce.value1uom unit 40 | , '1' datatype 41 | from mimic2v26.chartevents ce 42 | join cohort fc on ce.icustay_id = fc.icustay_id 43 | where ce.itemid in (51, 442, 455) --noninvasive (442, 455) & invasive blood pressure (51) 44 | and ce.value1num <> 0 45 | and ce.value1num is not null 46 | and extract(day from ce.charttime - fc.icustay_intime) < 3 47 | order by subject_id, icustay_id, post_adm 48 | ) 49 | --select * from sysbp; -- 29785 50 | 51 | , sysbp as ( 52 | select distinct 53 | subject_id, 54 | hadm_id, 55 | icustay_id, 56 | round(median(val) over (partition by icustay_id),2) sysbp_med, 57 | round(min(val) over (partition by icustay_id),2) sysbp_max, 58 | round(max(val) over (partition by icustay_id),2) sysbp_min, 59 | round(stddev(val) over (partition by icustay_id),2) sysbp_std, 60 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) sysbp_slp 61 | from sysbp_raw 62 | ) 63 | --select * from sysbp; 64 | 65 | , diabp_raw as ( 66 | select fc.subject_id 67 | , fc.icustay_id 68 | , fc.hadm_id 69 | , fc.icustay_intime, 70 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 71 | ce.value2num val, 72 | ce.value2uom unit, 73 | '2' datatype 74 | from mimic2v26.chartevents ce 75 | join cohort fc 76 | on ce.icustay_id = fc.icustay_id 77 | where ce.itemid in (51, 442,455) --noninvasive & invasive blood pressure 78 | and ce.value2num <> 0 79 | and ce.value2num is not null 80 | and extract(day from ce.charttime - fc.icustay_intime) < 3 81 | order by subject_id, icustay_id, post_adm 82 | ) 83 | --select count(distinct icustay_id) from diabp_raw; --29780 84 | 85 | , diabp as ( 86 | select distinct 87 | subject_id, 88 | hadm_id, 89 | icustay_id, 90 | round(median(val) over (partition by icustay_id),2) diabp_med, 91 | round(min(val) over (partition by icustay_id),2) diabp_min, 92 | round(max(val) over (partition by icustay_id),2) diabp_max, 93 | round(stddev(val) over (partition by icustay_id),2) diabp_std, 94 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) diabp_slp 95 | from diabp_raw 96 | ) 97 | --select count(*) from diabp where diabp = 0; 98 | 99 | -- get mean arterial blood pressure blood prssure 100 | , mbp_raw as ( 101 | select fc.subject_id 102 | , fc.icustay_id 103 | , fc.hadm_id 104 | , fc.icustay_intime, 105 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 106 | ce.value1num val, 107 | ce.value1uom unit, 108 | '3' datatype 109 | from mimic2v26.chartevents ce 110 | join cohort fc 111 | on ce.icustay_id = fc.icustay_id 112 | where itemid in (52, 224, 443, 456) -- invasive (52, 224) 113 | and ce.value1num <> 0 114 | and ce.value1num is not null 115 | and extract(day from ce.charttime - fc.icustay_intime) < 3 116 | order by subject_id, icustay_id, post_adm 117 | ) 118 | --select count(distinct icustay_id) from mbp_raw; --29765 119 | 120 | , mbp as ( 121 | select distinct 122 | subject_id, 123 | hadm_id, 124 | icustay_id, 125 | round(median(val) over (partition by icustay_id),2) mbp_med, 126 | round(min(val) over (partition by icustay_id),2) mbp_min, 127 | round(max(val) over (partition by icustay_id),2) mbp_max, 128 | round(stddev(val) over (partition by icustay_id),2) mbp_std, 129 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) mbp_slp 130 | from mbp_raw 131 | ) 132 | --select count(*) from mbp where mbp = 0; 133 | 134 | , pulsep_raw as ( 135 | select fc.subject_id 136 | , fc.icustay_id 137 | , fc.hadm_id 138 | , fc.icustay_intime, 139 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 140 | ce.value1num - ce.value2num val, 141 | ce.value1uom unit, 142 | '4' datatype 143 | from mimic2v26.chartevents ce 144 | join cohort fc 145 | on ce.icustay_id = fc.icustay_id 146 | where ce.itemid in (51,442,455) --noninvasive & invasive blood pressure 147 | and ce.value1num <> 0 148 | and ce.value1num is not null 149 | and ce.value2num <> 0 150 | and ce.value2num is not null 151 | and extract(day from ce.charttime - fc.icustay_intime) < 3 152 | order by subject_id, icustay_id, post_adm 153 | 154 | ) 155 | --select * from pulsep_raw; 156 | 157 | , pulsep as ( 158 | select distinct 159 | subject_id, 160 | hadm_id, 161 | icustay_id, 162 | round(median(val) over (partition by icustay_id),2) pp_med, 163 | round(min(val) over (partition by icustay_id),2) pp_min, 164 | round(max(val) over (partition by icustay_id),2) pp_max, 165 | round(stddev(val) over (partition by icustay_id),2) pp_std, 166 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) pp_slp 167 | from pulsep_raw 168 | ) 169 | --select * from pulsep; 170 | 171 | 172 | -- get heart rate for icustay 173 | , hr_raw as ( 174 | select fc.subject_id 175 | , fc.icustay_id 176 | , fc.hadm_id 177 | , fc.icustay_intime, 178 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 179 | ce.value1num val, 180 | ce.value1uom unit, 181 | '9' datatype 182 | from mimic2v26.chartevents ce 183 | join cohort fc 184 | on ce.icustay_id = fc.icustay_id 185 | where itemid = 211 --heart rate 186 | and ce.value1num <> 0 187 | and ce.value1num is not null 188 | and extract(day from ce.charttime - fc.icustay_intime) < 3 189 | order by subject_id, icustay_id, post_adm 190 | ) 191 | --select * from hr_raw; 192 | 193 | , hr as ( 194 | select distinct 195 | subject_id, 196 | hadm_id, 197 | icustay_id, 198 | round(median(val) over (partition by icustay_id),2) hr_med, 199 | round(min(val) over (partition by icustay_id),2) hr_min, 200 | round(max(val) over (partition by icustay_id),2) hr_max, 201 | round(stddev(val) over (partition by icustay_id),2) hr_std, 202 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) hr_slp 203 | from hr_raw 204 | ) 205 | --select count(*) from hr where hr = 0; 206 | 207 | -- get central venous pressure for icustay 208 | , cvp_raw as ( 209 | select fc.subject_id 210 | , fc.icustay_id 211 | , fc.hadm_id 212 | , fc.icustay_intime, 213 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 214 | ce.value1num val, 215 | ce.value1uom unit, 216 | '10' datatype 217 | from mimic2v26.chartevents ce 218 | join cohort fc 219 | on ce.icustay_id = fc.icustay_id 220 | where itemid in (113, 1103) --cvp 221 | and ce.value1num <> 0 222 | and ce.value1num is not null 223 | and extract(day from ce.charttime - fc.icustay_intime) < 3 224 | order by subject_id, icustay_id, post_adm 225 | ) 226 | --select * from cvp; 227 | 228 | , cvp as ( 229 | select distinct 230 | subject_id, 231 | hadm_id, 232 | icustay_id, 233 | round(median(val) over (partition by icustay_id),2) cvp_med, 234 | round(min(val) over (partition by icustay_id),2) cvp_min, 235 | round(max(val) over (partition by icustay_id),2) cvp_max, 236 | round(stddev(val) over (partition by icustay_id),2) cvp_std, 237 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) cvp_slp 238 | from hr_raw 239 | ) 240 | --select count(*) missing_cvp from cvp where cvp = 0; --32 241 | 242 | 243 | -- get peripheral oxygen saturation for icustay 244 | , spo2_raw as ( 245 | select fc.subject_id 246 | , fc.icustay_id 247 | , fc.hadm_id 248 | , fc.icustay_intime, 249 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 250 | ce.value1num val, 251 | ce.value1uom unit, 252 | '11' datatype 253 | from mimic2v26.chartevents ce 254 | join cohort fc 255 | on ce.icustay_id = fc.icustay_id 256 | where itemid in (646, 834) -- spo2 257 | and ce.value1num <> 0 258 | and ce.value1num is not null 259 | and extract(day from ce.charttime - fc.icustay_intime) < 3 260 | order by subject_id, icustay_id, post_adm 261 | ) 262 | --select * from spo2_raw; 263 | 264 | , spo2 as ( 265 | select distinct 266 | subject_id, 267 | hadm_id, 268 | icustay_id, 269 | round(median(val) over (partition by icustay_id),2) spo2_med, 270 | round(min(val) over (partition by icustay_id),2) spo2_min, 271 | round(max(val) over (partition by icustay_id),2) spo2_max, 272 | round(stddev(val) over (partition by icustay_id),2) spo2_std, 273 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) spo2_slp 274 | from hr_raw 275 | ) 276 | --select count(*) missing_spo2 from spo2 where spo2 = 0; --32 277 | 278 | 279 | -- get respiration/breathing rate for icustay 280 | , br_raw as ( 281 | select fc.subject_id 282 | , fc.icustay_id 283 | , fc.hadm_id 284 | , fc.icustay_intime, 285 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 286 | ce.value1num val, 287 | ce.value1uom unit, 288 | '12' datatype 289 | from mimic2v26.chartevents ce 290 | join cohort fc 291 | on ce.icustay_id = fc.icustay_id 292 | where itemid in (614, 615, 618, 1635, 1884, 3603, 3337) 293 | -- resp. rate (1635 only appears for one patient; 294 | -- 1884 values are crazy and only appears for 2 or 3 patients) 295 | -- 3603 values look somehow elevated (check if it corresponds to neonates) 296 | and ce.value1num <> 0 297 | and ce.value1num is not null 298 | and extract(day from ce.charttime - fc.icustay_intime) < 3 299 | order by subject_id, icustay_id, post_adm 300 | ) 301 | --select * from br_raw; 302 | 303 | , br as ( 304 | select distinct 305 | subject_id, 306 | hadm_id, 307 | icustay_id, 308 | round(median(val) over (partition by icustay_id),2) br_med, 309 | round(min(val) over (partition by icustay_id),2) br_min, 310 | round(max(val) over (partition by icustay_id),2) br_max, 311 | round(stddev(val) over (partition by icustay_id),2) br_std, 312 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) br_slp 313 | from br_raw 314 | ) 315 | --select count(*) missing_br from br where br = 0; --37 316 | 317 | 318 | , urine_output_raw as ( 319 | select fc.subject_id 320 | , fc.icustay_id 321 | , fc.hadm_id 322 | , fc.icustay_intime, 323 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 324 | ce.volume val, 325 | ce.volumeuom unit, 326 | '13' datatype 327 | from mimic2v26.ioevents ce 328 | join cohort fc 329 | on ce.icustay_id = fc.icustay_id 330 | where ce.itemid in (651, 715, 55, 56, 57, 61, 65, 69, 85, 94, 96, 288, 405, 331 | 428, 473, 2042, 2068, 2111, 2119, 2130, 1922, 2810, 2859, 332 | 3053, 3462, 3519, 3175, 2366, 2463, 2507, 2510, 2592, 333 | 2676, 3966, 3987, 4132, 4253, 5927) 334 | and extract(day from ce.charttime - fc.icustay_intime) < 3 335 | and volume is not null 336 | order by subject_id, icustay_id, post_adm 337 | ) 338 | --select * from urine_output_raw; 339 | 340 | , urine_output as ( 341 | select distinct 342 | subject_id, 343 | hadm_id, 344 | icustay_id, 345 | round(median(val) over (partition by icustay_id),2) uo_med, 346 | round(min(val) over (partition by icustay_id),2) uo_min, 347 | round(max(val) over (partition by icustay_id),2) uo_max, 348 | round(stddev(val) over (partition by icustay_id),2) uo_std, 349 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) uo_slp 350 | from urine_output_raw 351 | ) 352 | --select count(*) missing_uo from urine_output where uo = 0; --234 353 | 354 | 355 | -- get temperature for icustay (NOT CONVERTED) 356 | , temp_raw as ( 357 | select fc.subject_id 358 | , fc.icustay_id 359 | , fc.hadm_id 360 | , fc.icustay_intime, 361 | extract(day from ce.charttime - fc.icustay_intime)*1440 + extract(hour from ce.charttime - fc.icustay_intime)*60 + extract(minute from ce.charttime - fc.icustay_intime) post_adm, 362 | ce.value1num val, 363 | ce.value1uom unit, 364 | '14' datatype 365 | from mimic2v26.chartevents ce 366 | join cohort fc 367 | on ce.icustay_id = fc.icustay_id 368 | where itemid in (676, 677, 678, 679) -- temperature 369 | and extract(day from ce.charttime - fc.icustay_intime) < 3 370 | and ce.value1num <> 0 371 | and ce.value1num is not null 372 | order by subject_id, icustay_id, post_adm 373 | ) 374 | --select * from temp_raw; 375 | 376 | , temp as ( 377 | select distinct 378 | subject_id, 379 | hadm_id, 380 | icustay_id, 381 | round(median(val) over (partition by icustay_id),2) temp_med, 382 | round(min(val) over (partition by icustay_id),2) temp_min, 383 | round(max(val) over (partition by icustay_id),2) temp_max, 384 | round(stddev(val) over (partition by icustay_id),2) temp_std, 385 | round(regr_slope(post_adm,val) over (partition by icustay_id),2) temp_slp 386 | from temp_raw 387 | ) 388 | --select count(*) missing_temp from temp where temp = 0; --234 389 | 390 | /* 391 | 392 | -- finally, assemble 393 | select * from sysbp 394 | union 395 | select * from diabp 396 | union 397 | select * from mbp 398 | union 399 | select * from pulsep 400 | union 401 | select * from sysibp 402 | union 403 | select * from diaibp 404 | union 405 | select * from mibp 406 | union 407 | select * from pulsepi 408 | union 409 | select * from hr 410 | union 411 | select * from cvp 412 | union 413 | select * from spo2 414 | union 415 | select * from br 416 | union 417 | select * from urine_output 418 | union 419 | select * from temp; 420 | 421 | */ 422 | 423 | , final as ( 424 | select distinct 425 | fc.*, 426 | sbp.sysbp_med, 427 | sbp.sysbp_min, 428 | sbp.sysbp_max, 429 | sbp.sysbp_std, 430 | sbp.sysbp_slp, 431 | 432 | dbp.diabp_med, 433 | dbp.diabp_min, 434 | dbp.diabp_max, 435 | dbp.diabp_std, 436 | dbp.diabp_slp, 437 | 438 | mbp.mbp_med, 439 | mbp.mbp_min, 440 | mbp.mbp_max, 441 | mbp.mbp_std, 442 | mbp.mbp_slp, 443 | 444 | pp.pp_med, 445 | pp.pp_min, 446 | pp.pp_max, 447 | pp.pp_std, 448 | pp.pp_slp, 449 | 450 | hr.hr_med, 451 | hr.hr_min, 452 | hr.hr_max, 453 | hr.hr_std, 454 | hr.hr_slp, 455 | 456 | cvp.cvp_med, 457 | cvp.cvp_min, 458 | cvp.cvp_max, 459 | cvp.cvp_std, 460 | cvp.cvp_slp, 461 | 462 | spo2.spo2_med, 463 | spo2.spo2_min, 464 | spo2.spo2_max, 465 | spo2.spo2_std, 466 | spo2.spo2_slp, 467 | 468 | br.br_med, 469 | br.br_min, 470 | br.br_max, 471 | br.br_std, 472 | br.br_slp, 473 | 474 | uo.uo_med, 475 | uo.uo_min, 476 | uo.uo_max, 477 | uo.uo_std, 478 | uo.uo_slp, 479 | 480 | t.temp_med, 481 | t.temp_min, 482 | t.temp_max, 483 | t.temp_std, 484 | t.temp_slp 485 | 486 | from cohort fc 487 | join sysbp sbp on fc.icustay_id = sbp.icustay_id and sbp.sysbp_std <> 0 488 | join diabp dbp on fc.icustay_id = dbp.icustay_id and dbp.diabp_std <> 0 489 | join mbp on fc.icustay_id = mbp.icustay_id and mbp.mbp_std <> 0 490 | join pulsep pp on fc.icustay_id = pp.icustay_id and pp.pp_std <> 0 491 | join hr on fc.icustay_id = hr.icustay_id and hr.hr_std <> 0 492 | join cvp on fc.icustay_id = cvp.icustay_id and cvp.cvp_std <> 0 493 | join spo2 on fc.icustay_id = spo2.icustay_id and spo2.spo2_std <> 0 494 | join br on fc.icustay_id = br.icustay_id and br.br_std <> 0 495 | join urine_output uo on fc.icustay_id = uo.icustay_id and uo.uo_std <> 0 496 | join temp t on fc.icustay_id = t.icustay_id and t.temp_std <> 0 497 | ) 498 | select * from final; -------------------------------------------------------------------------------- /sql/sepsis_data_jan14_lab_vitalSigns_coomorbidities.sql: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Created on : Dec 2012 by Mornin Feng 4 | Last updated : August 2013 5 | Extract data for echo project and project 6 | 7 | */ 8 | 9 | --SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY()); 10 | 11 | --explain plan for 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------------------------------- 16 | -------------------------------------------------------------------------------------------------------- 17 | -------------------------- Data Extraction ----------------------------------------------------- 18 | -------------------------------------------------------------------------------------------------------- 19 | -------------------------------------------------------------------------------------------------------- 20 | drop table echo_sepsis_data_jan14; 21 | create table echo_sepsis_data_jan14 as 22 | with population_1 as 23 | (select * from mornin.SEPSIS_COHORT_JAN14 24 | ) 25 | 26 | --select count(distinct icustay_id) from population; 27 | --select * from population; 28 | 29 | 30 | -------------------------------------------------------------------------------------------------------- 31 | -------------------------------------------------------------------------------------------------------- 32 | -------------------------- Demographic and basic data ----------------------------------------------------- 33 | -------------------------------------------------------------------------------------------------------- 34 | -------------------------------------------------------------------------------------------------------- 35 | , population_2 as 36 | (select distinct 37 | pop.* 38 | , round(icud.icustay_los/60/24, 2) as icu_los_day 39 | , round(icud.hospital_los/60/24,2) as hospital_los_day 40 | , case when icud.icustay_admit_age>120 then 91.4 else icud.icustay_admit_age end as age 41 | --, icud.gender as gender 42 | , case when icud.gender is null then null 43 | when icud.gender = 'M' then 1 else 0 end as gender_num 44 | , icud.WEIGHT_FIRST 45 | , bmi.bmi 46 | , icud.SAPSI_FIRST 47 | , icud.SOFA_FIRST 48 | , elix.TWENTY_EIGHT_DAY_MORT_PT 49 | , elix.ONE_YEAR_SURVIVAL_PT 50 | , elix.TWO_YEAR_SURVIVAL_PT 51 | , icud.ICUSTAY_FIRST_SERVICE as service_unit 52 | , case when ICUSTAY_FIRST_SERVICE='SICU' then 1 53 | when ICUSTAY_FIRST_SERVICE='CCU' then 2 54 | when ICUSTAY_FIRST_SERVICE='CSRU' then 3 55 | else 0 --MICU & FICU 56 | end 57 | as service_num 58 | , icud.icustay_intime 59 | , icud.icustay_outtime 60 | , to_char(icud.ICUSTAY_INTIME, 'Day') as day_icu_intime 61 | , to_number(to_char(icud.ICUSTAY_INTIME, 'D')) as day_icu_intime_num 62 | , extract(hour from icud.ICUSTAY_INTIME) as hour_icu_intime 63 | , round((extract(day from d.dod-icud.icustay_intime)+extract(hour from d.dod-icud.icustay_intime)/24),2) as mort_day 64 | from population_1 pop 65 | left join mimic2v26.icustay_detail icud on pop.icustay_id = icud.icustay_id 66 | left join mimic2devel.obesity_bmi bmi on bmi.icustay_id=pop.icustay_id 67 | left join MIMIC2DEVEL.d_patients d on d.subject_id=pop.subject_id 68 | left join mimic2devel.ELIXHAUSER_POINTS elix on elix.hadm_id=pop.hadm_id 69 | ) 70 | 71 | --select distinct service_unit from population_2; 72 | --select max(hour_icu_intime) from population_2; 73 | --select * from population_2; 74 | 75 | 76 | , population as 77 | (select p.* 78 | , case when p.mort_day<=28 then 1 else 0 end as day_28_flg 79 | , case when p.mort_day <=730 then p.mort_day 80 | else 731 end as mort_day_censored 81 | , case when p.mort_day<=730 then 0 else 1 end as censor_flg from population_2 p where icu_los_day>=0.5 --- stayed in icu for more than 12 hours 82 | ) 83 | 84 | --select * from population; --6517 85 | 86 | 87 | -------------------------------------------------------------------------------------------------------- 88 | -------------------------------------------------------------------------------------------------------- 89 | -------------------------- Vent patients ----------------------------------------------------- 90 | -------------------------------------------------------------------------------------------------------- 91 | -------------------------------------------------------------------------------------------------------- 92 | 93 | ,vent_group_1 as 94 | (select distinct 95 | --pop.hadm_id 96 | pop.icustay_id 97 | , 1 as flg 98 | --, icud.icustay_id 99 | --, vent.end_time 100 | --, vent.begin_time 101 | --, (vent.end_time-vent.begin_time) as time_diff 102 | , sum(round((extract(day from (vent.end_time-vent.begin_time))+ 103 | extract(hour from (vent.end_time-vent.begin_time))/24+1/24+ 104 | extract(minute from (vent.end_time-vent.begin_time))/60/24), 3)) as time_total_day 105 | , pop.icu_los_day 106 | from population pop 107 | --join mimic2v26.icustay_detail icud on icud.icustay_id = pop.icustay_id 108 | join mimic2devel.ventilation vent on vent.icustay_id = pop.icustay_id 109 | group by pop.icustay_id, pop.icu_los_day 110 | order by 1 111 | ) 112 | 113 | --select * from vent_group_1; ---4161 114 | --select * from vent_group where hadm_id=2798; 115 | , vent_group as 116 | (select v.* 117 | , case when (icu_los_day-time_total_day)>0 then (icu_los_day-time_total_day) else 0 end as vent_free_day 118 | from vent_group_1 v 119 | ) 120 | 121 | --select * from vent_group order by vent_free_day asc; 122 | 123 | 124 | ------------ label vent patients at 1st 12 hour------------------------------- 125 | ,vent_12hr_group as 126 | (select distinct 127 | --pop.hadm_id 128 | pop.icustay_id 129 | , 1 as flg 130 | --, vent.begin_time 131 | --, icud.icustay_intime 132 | from population pop 133 | --join mimic2v26.icustay_detail icud on icud.hadm_id = pop.hadm_id and icud.icustay_seq=1 134 | join mimic2devel.ventilation vent 135 | on vent.icustay_id = pop.icustay_id 136 | --and vent.seq=1 137 | and vent.begin_time<=pop.icustay_intime+12/24 138 | order by 1 139 | ) 140 | 141 | --select * from vent_12hr_group; --3488 142 | 143 | -------------------------------------------------------------------------------------------------------- 144 | -------------------------------------------------------------------------------------------------------- 145 | -------------------------- Mediaction Dat: vasopressor & Anesthetic ----------------------------------------------------- 146 | -------------------------------------------------------------------------------------------------------- 147 | -------------------------------------------------------------------------------------------------------- 148 | 149 | 150 | ------------------------------------------- label vaso patients ---------------------------- 151 | --- a more accurate calculation of vaso time may be necessary!!!!! 152 | , vaso_group_1 as 153 | (select 154 | distinct 155 | --pop.hadm_id 156 | pop.icustay_id 157 | , pop.icustay_intime 158 | , pop.icustay_outtime 159 | --, pop.icustay_outtime-pop.icustay_intime as temp 160 | , pop.icu_los_day 161 | , first_value(med.charttime) over (partition by pop.icustay_id order by med.charttime asc) as begin_time 162 | , first_value(med.charttime) over (partition by pop.icustay_id order by med.charttime desc) as end_time 163 | , 1 as flg 164 | from population pop 165 | --join mimic2v26.icustay_detail icud on icud.hadm_id = pop.hadm_id 166 | join mimic2v26.medevents med on med.icustay_id=pop.icustay_id and med.itemid in (46,47,120,43,307,44,119,309,51,127,128) 167 | where med.charttime is not null 168 | ) 169 | 170 | --select extract(day from temp) as temp_day from vaso_group_1 where icustay_id=2613; 171 | --select count(distinct icustay_id) from vaso_group; 172 | 173 | , vaso_group_2 as 174 | (select 175 | --hadm_id 176 | icustay_id 177 | , round(extract(day from (end_time-begin_time)) 178 | + extract(hour from (end_time-begin_time))/24 +1/24 --- add additional 1 hour 179 | + extract(minute from (end_time-begin_time))/60/24, 2) as time_diff_day 180 | , icu_los_day 181 | --, round(extract(day from (icustay_outtime-icustay_intime)) 182 | -- + extract(hour from (icustay_outtime-icustay_intime))/24 183 | -- + extract(minute from (icustay_outtime-icustay_intime))/60/24, 2) as temp 184 | , flg 185 | from vaso_group_1 186 | ) 187 | 188 | 189 | --select * from vaso_group_2 where icustay_id=2613; 190 | 191 | , vaso_group as 192 | (select distinct 193 | icustay_id 194 | , flg 195 | , time_diff_day 196 | , icu_los_day 197 | , case when (icu_los_day-time_diff_day)<0 then 0 else (icu_los_day-time_diff_day) end as vaso_free_Day 198 | from vaso_group_2 199 | ) 200 | 201 | --select * from vaso_group_final order by vaso_free_day; ---2915 202 | 203 | -------------- label vaso patients for 1st 12 hours ---------------------------- 204 | --, vaso_group_12_hr_1 as 205 | --(select 206 | --distinct 207 | ----pop.hadm_id 208 | --pop.icustay_id 209 | --, first_value(med.charttime) over (partition by pop.icustay_id order by med.charttime asc) as begin_time 210 | --, pop.ICUSTAY_INTIME 211 | --from population pop 212 | ----join mimic2v26.icustay_detail icud on icud.hadm_id = pop.hadm_id and ICUSTAY_SEQ =1 213 | --join mimic2v26.medevents med on med.icustay_id=pop.icustay_id and med.itemid in (46,47,120,43,307,44,119,309,51,127,128) 214 | --where med.charttime is not null 215 | --order by 1 216 | --) 217 | 218 | --select count(distinct hadm_id) from vaso_group_12_hr_1; 219 | --select * from vaso_group_12_hr_1; --2991 220 | 221 | 222 | , vaso_12hr_group as 223 | (select distinct icustay_id 224 | , 1 as flg 225 | from vaso_group_1 226 | where begin_time <= ICUSTAY_INTIME+12/24 227 | ) 228 | 229 | --select * from vaso_12hr_group; --2016 230 | 231 | ------------------------------------------- label Anesthetic patients ---------------------------- 232 | , anes_group_1 as 233 | (select 234 | distinct 235 | --pop.hadm_id 236 | pop.icustay_id 237 | , pop.icustay_intime 238 | , pop.icustay_outtime 239 | --, pop.icustay_outtime-pop.icustay_intime as temp 240 | , pop.icu_los_day 241 | , first_value(med.charttime) over (partition by pop.icustay_id order by med.charttime asc) as begin_time 242 | , first_value(med.charttime) over (partition by pop.icustay_id order by med.charttime desc) as end_time 243 | , 1 as flg 244 | from population pop 245 | --join mimic2v26.icustay_detail icud on icud.hadm_id = pop.hadm_id 246 | join mimic2v26.medevents med on med.icustay_id=pop.icustay_id and med.itemid in (124,118,149,150,308,163,131) 247 | where med.charttime is not null 248 | ) 249 | 250 | --select * from anes_group_1; 251 | 252 | , anes_group_2 as 253 | (select 254 | --hadm_id 255 | icustay_id 256 | , round(extract(day from (end_time-begin_time)) 257 | + extract(hour from (end_time-begin_time))/24 + 1/24 -- add additional 1 hour for edge consideration 258 | + extract(minute from (end_time-begin_time))/60/24, 2) as time_diff_day 259 | , icu_los_day 260 | --, round(extract(day from (icustay_outtime-icustay_intime)) 261 | -- + extract(hour from (icustay_outtime-icustay_intime))/24 262 | -- + extract(minute from (icustay_outtime-icustay_intime))/60/24, 2) as temp 263 | , flg 264 | from anes_group_1 265 | ) 266 | 267 | --select * from anes_group_2; 268 | 269 | , anes_group as 270 | (select distinct 271 | icustay_id 272 | , flg 273 | , time_diff_day 274 | , icu_los_day 275 | --, (icu_los_day-time_diff_day) 276 | , case when (icu_los_day-time_diff_day)<0 then 0 else (icu_los_day-time_diff_day) end as anes_free_Day 277 | from anes_group_2 278 | ) 279 | 280 | --select * from anes_group_final order by 5; 281 | 282 | -------------- label anesthetic patients for 1st 12 hours ---------------------------- 283 | 284 | , anes_12hr_group as 285 | (select distinct icustay_id 286 | , 1 as flg 287 | from anes_group_1 288 | where begin_time <= ICUSTAY_INTIME+12/24 289 | ) 290 | 291 | --select * from anes_12hr_group; --2016 --2583 292 | 293 | ------------------------------------- dobutamine medication group ------------------- 294 | 295 | , dabu_group_1 as 296 | (select 297 | distinct 298 | --pop.hadm_id 299 | pop.icustay_id 300 | , pop.icustay_intime 301 | --, pop.icustay_outtime 302 | --, pop.icustay_outtime-pop.icustay_intime as temp 303 | , pop.icu_los_day 304 | , first_value(med.charttime) over (partition by pop.icustay_id order by med.charttime asc) as begin_time 305 | --, first_value(med.charttime) over (partition by pop.icustay_id order by med.charttime desc) as end_time 306 | , 1 as flg 307 | from population pop 308 | --join mimic2v26.icustay_detail icud on icud.hadm_id = pop.hadm_id 309 | join mimic2v26.medevents med on med.icustay_id=pop.icustay_id and med.itemid in (306,42) 310 | where med.charttime is not null 311 | ) 312 | 313 | , dabu_12hr_group as 314 | (select distinct icustay_id 315 | , 1 as flg 316 | from dabu_group_1 317 | where begin_time <= ICUSTAY_INTIME+12/24 318 | ) 319 | 320 | --select * from dabu_12hr_group; --123 321 | -------------------------------------------------------------------------------------------------------- 322 | -------------------------------------------------------------------------------------------------------- 323 | -------------------------- commorbidity variables ----------------------------------------------------- 324 | -------------------------------------------------------------------------------------------------------- 325 | -------------------------------------------------------------------------------------------------------- 326 | , icd9code as 327 | (select 328 | pop.icustay_id 329 | , pop.hadm_id 330 | , regexp_substr(code,'^\D') AS icd9_alpha 331 | , to_number(regexp_substr(code,'\d+$|\d+\.\d+$')) AS icd9_numeric 332 | from population pop 333 | join mimic2v26.icd9 icd on pop.hadm_id=icd.hadm_id 334 | ) 335 | 336 | --select * from icd9code; 337 | 338 | --endocarditis diagnosis group 339 | , endocarditis_group as 340 | (select distinct pop.hadm_id, pop.icustay_id, 1 as flg 341 | from population pop join mimic2v26.icd9 icd on pop.hadm_id=icd.hadm_id 342 | where icd.code in ('036.42','074.22','093.20','093.21','093.22','093.23','093.24','098.84','112.81','115.04','115.14','115.94','391.1','421.0','421.1','421.9','424.90','424.91','424.99') 343 | ) 344 | 345 | --select count(*) from endocarditis_group; --113 346 | --select adm.subject_id, adm.hadm_id, adm.admit_dt,dpat.dod from mimic2v26.admissions adm, mimic2devel.d_patients dpat where adm.subject_id=dpat.subject_id and adm.hadm_id = 9679; 347 | 348 | , chf_group as 349 | (select distinct pop.hadm_id,pop.icustay_id, 1 as flg 350 | from population pop 351 | join mimic2v26.icd9 icd9 on icd9.hadm_id=pop.hadm_id 352 | --where icd9.code in ('398.91','402.01','402.91','404.91', '404.13', '404.93','428.0','428.1','428.9') 353 | where icd9.code in ('398.91','402.01','402.91','404.91', '404.13', '404.93', '428.0', '428.1', '428.20', '428.21', '428.22', '428.23', '428.30', '428.31', '428.32', '428.33', '428.40', '428.41', '428.42', '428.43', '428.9', '428', '428.2', '428.3', '428.4') 354 | order by 1 355 | ) 356 | --select * from chf_group; --2518 357 | 358 | , afib_group as 359 | (select distinct pop.hadm_id, pop.icustay_id, 1 as flg 360 | from population pop 361 | join mimic2v26.icd9 icd9 on icd9.hadm_id=pop.hadm_id 362 | --where icd9.code in ('398.91','402.01','402.91','404.91', '404.13', '404.93','428.0','428.1','428.9') 363 | where icd9.code like '427.3%' 364 | order by 1 365 | ) 366 | 367 | --select count(*) from population; --6517 368 | --select count(*) from afib_group; --1896 369 | 370 | , renal_group as -- end stage or chronic renal disease 371 | (select distinct pop.hadm_id, pop.icustay_id, 1 as flg 372 | from population pop 373 | join mimic2v26.icd9 icd9 on icd9.hadm_id=pop.hadm_id 374 | --where icd9.code in ('398.91','402.01','402.91','404.91', '404.13', '404.93','428.0','428.1','428.9') 375 | where icd9.code like '585.%%' 376 | order by 1 377 | ) 378 | 379 | --select count(*) from renal_group; --539 380 | 381 | , liver_group as -- end stage liver disease 382 | (select distinct pop.hadm_id, pop.icustay_id, 1 as flg 383 | from population pop 384 | join mimic2v26.icd9 icd9 on icd9.hadm_id=pop.hadm_id 385 | --where icd9.code in ('398.91','402.01','402.91','404.91', '404.13', '404.93','428.0','428.1','428.9') 386 | where icd9.code like '571.%%' 387 | order by 1 388 | ) 389 | 390 | --select count(*) from liver_group; --478 391 | 392 | , copd_group as --- following definition of PQI5 paper 393 | (select distinct pop.hadm_id, pop.icustay_id, 1 as flg 394 | --, icd9.code 395 | from population pop 396 | join mimic2v26.icd9 icd9 on icd9.hadm_id=pop.hadm_id 397 | --where icd9.code in ('398.91','402.01','402.91','404.91', '404.13', '404.93','428.0','428.1','428.9') 398 | where icd9.code in ('466.0', '490', '491.0', '491.1', '491.20', '491.21', '491.8', '491.9', '492.0', '492.8', '494', '494.0', '494.1', '496') 399 | order by 1 400 | ) 401 | 402 | --select * from copd_group; --1091 403 | 404 | , cad_group as -- coronary artery disease 405 | (select distinct pop.hadm_id, pop.icustay_id, 1 as flg 406 | --, icd9.code 407 | from population pop 408 | join mimic2v26.icd9 icd9 on icd9.hadm_id=pop.hadm_id 409 | --where icd9.code in ('398.91','402.01','402.91','404.91', '404.13', '404.93','428.0','428.1','428.9') 410 | where icd9.code like '414.%' 411 | order by 1 412 | ) 413 | 414 | --select * from cad_group; --1289 415 | 416 | , stroke_group as 417 | (select distinct pop.hadm_id, pop.icustay_id, 1 as flg 418 | --, icd9.code 419 | --, icd9.code 420 | from population pop 421 | join mimic2v26.icd9 icd9 on icd9.hadm_id=pop.hadm_id 422 | --where icd9.code in ('398.91','402.01','402.91','404.91', '404.13', '404.93','428.0','428.1','428.9') 423 | where icd9.code like '430%%%' or icd9.code like '431%%%' or icd9.code like '432%%%' or icd9.code like '433%%%' or icd9.code like '434%%%' 424 | order by 1 425 | ) 426 | 427 | --select * from stroke_group; --616 428 | 429 | , malignancy_group as 430 | (select distinct icustay_id 431 | , hadm_id 432 | , 1 as flg 433 | --, icd9_alpha 434 | --, icd9_numeric 435 | from icd9code 436 | where icd9_alpha is null 437 | and icd9_numeric between 140 and 239 438 | ) 439 | 440 | --select * from malignancy_group; --1865 441 | 442 | -------------------------------------------------------------------------------------------------------- 443 | -------------------------------------------------------------------------------------------------------- 444 | -------------------------- vital signs variables ----------------------------------------------------- 445 | -------------------------------------------------------------------------------------------------------- 446 | -------------------------------------------------------------------------------------------------------- 447 | ---- MAP ---- 448 | , map_group_1 as 449 | (select pop.icustay_id 450 | , ch.charttime 451 | , ch.value1num as bp 452 | from population pop 453 | left join mimic2v26.chartevents ch 454 | on pop.icustay_id=ch.icustay_id 455 | and ch.itemid in (52,456) 456 | and ch.charttime <= pop.icustay_intime+12/24 457 | ) 458 | 459 | --select * from map_group; 460 | , map_group as 461 | (select distinct icustay_id 462 | , first_value(bp) over (partition by icustay_id order by charttime asc) as map_1st 463 | , first_value(bp) over (partition by icustay_id order by bp asc) as map_lowest 464 | , first_value(bp) over (partition by icustay_id order by bp desc) as map_highest 465 | from map_group_1 466 | ) 467 | 468 | --select * from map_group; 469 | 470 | -------- Temperature ------------- 471 | 472 | , t_group_1 as 473 | (select pop.icustay_id 474 | , ch.charttime 475 | , ch.value1num as temp 476 | from population pop 477 | left join mimic2v26.chartevents ch 478 | on pop.icustay_id=ch.icustay_id 479 | and ch.itemid in (678,679) 480 | and ch.charttime <= pop.icustay_intime+12/24 481 | ) 482 | 483 | --select * from map_group; 484 | , t_group as 485 | (select distinct icustay_id 486 | , first_value(temp) over (partition by icustay_id order by charttime asc) as temp_1st 487 | , first_value(temp) over (partition by icustay_id order by temp asc) as temp_lowest 488 | , first_value(temp) over (partition by icustay_id order by temp desc) as temp_highest 489 | from t_group_1 490 | ) 491 | 492 | --select * from t_group; 493 | 494 | 495 | -------- HR ------------- 496 | 497 | , hr_group_1 as 498 | (select pop.icustay_id 499 | , ch.charttime 500 | , ch.value1num as hr 501 | from population pop 502 | left join mimic2v26.chartevents ch 503 | on pop.icustay_id=ch.icustay_id 504 | and ch.itemid =211 505 | and ch.charttime <= pop.icustay_intime+12/24 506 | ) 507 | 508 | , hr_group as 509 | (select distinct icustay_id 510 | , first_value(hr) over (partition by icustay_id order by charttime asc) as hr_1st 511 | , first_value(hr) over (partition by icustay_id order by hr asc) as hr_lowest 512 | , first_value(hr) over (partition by icustay_id order by hr desc) as hr_highest 513 | from hr_group_1 514 | ) 515 | 516 | --select * from hr_group where hr_1st is not null; 517 | 518 | 519 | -------- HR ------------- 520 | 521 | ,cvp_group_1 as 522 | (select pop.icustay_id 523 | , ch.charttime 524 | , ch.value1num as cvp 525 | from population pop 526 | left join mimic2v26.chartevents ch 527 | on pop.icustay_id=ch.icustay_id 528 | and ch.itemid =113 529 | and ch.charttime <= pop.icustay_intime+12/24 530 | ) 531 | 532 | , cvp_group as 533 | (select distinct icustay_id 534 | , first_value(cvp) over (partition by icustay_id order by charttime asc) as cvp_1st 535 | , first_value(cvp) over (partition by icustay_id order by cvp asc) as cvp_lowest 536 | , first_value(cvp) over (partition by icustay_id order by cvp desc) as cvp_highest 537 | from cvp_group_1 538 | ) 539 | 540 | --select * from cvp_group where cvp_1st is not null; 541 | 542 | -------------------------------------------------------------------------------------------------------- 543 | -------------------------------------------------------------------------------------------------------- 544 | -------------------------- Lab data ------------------------------------------------------------------- 545 | -------------------------------------------------------------------------------------------------------- 546 | -------------------------------------------------------------------------------------------------------- 547 | 548 | --- WBC --- 549 | , lab_wbc_1 as 550 | (select pop.hadm_id 551 | , pop.icustay_id 552 | , pop.ICUSTAY_INTIME 553 | , lab.charttime 554 | , lab.valuenum as wbc 555 | , case when lab.valuenum between 4.5 and 10 then 0 else 1 end as abnormal_flg 556 | from population pop 557 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 558 | join mimic2v26.labevents lab 559 | on pop.hadm_id=lab.hadm_id 560 | and lab.itemid in (50316,50468) 561 | and lab.valuenum is not null 562 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 563 | order by 1 564 | ) 565 | 566 | --select * from lab_wbc_1; 567 | 568 | , lab_wbc as 569 | (select distinct icustay_id 570 | , first_value(wbc) over (partition by hadm_id order by charttime asc) as wbc_first 571 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 572 | , first_value(wbc) over (partition by hadm_id order by wbc asc) as wbc_lowest 573 | , first_value(wbc) over (partition by hadm_id order by wbc desc) as wbc_highest 574 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) wbc_abnormal_flg 575 | from lab_wbc_1 576 | order by 1 577 | ) 578 | 579 | --select * from lab_wbc; --6399 580 | 581 | --- hemoglobin ---- 582 | 583 | , lab_hgb_1 as 584 | (select pop.hadm_id 585 | , pop.icustay_id 586 | , pop.ICUSTAY_INTIME 587 | , lab.charttime 588 | , lab.valuenum as hgb 589 | , case when pop.gender_num=1 and lab.valuenum between 13.8 and 17.2 then 0 590 | when pop.gender_num=0 and lab.valuenum between 12.1 and 15.1 then 0 591 | --when pop.gender_num is null then null 592 | else 1 end as abnormal_flg 593 | from population pop 594 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 595 | join mimic2v26.labevents lab 596 | on pop.hadm_id=lab.hadm_id 597 | and lab.itemid in (50386,50007,50184) 598 | --(50377,50386,50388,50391,50411,50454,50054,50003,50007,50011,50184,50183,50387,50389,50390,50412) 599 | and lab.valuenum is not null 600 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 601 | and pop.gender_num is not null 602 | order by 1 603 | ) 604 | 605 | --select * from lab_hgb_1; 606 | 607 | , lab_hgb as 608 | (select distinct icustay_id 609 | , first_value(hgb) over (partition by hadm_id order by charttime asc) as hgb_first 610 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 611 | , first_value(hgb) over (partition by hadm_id order by hgb asc) as hgb_lowest 612 | , first_value(hgb) over (partition by hadm_id order by hgb desc) as hgb_highest 613 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) hgb_abnormal_flg 614 | from lab_hgb_1 615 | order by 1 616 | ) 617 | 618 | --select * from lab_hgb; --6457 619 | 620 | ---- platelets --- 621 | , lab_platelet_1 as 622 | (select pop.hadm_id 623 | , pop.icustay_id 624 | , pop.ICUSTAY_INTIME 625 | , lab.charttime 626 | , lab.itemid 627 | , lab.valueuom 628 | , lab.valuenum as platelet 629 | , case when lab.valuenum between 150 and 400 then 0 630 | else 1 end as abnormal_flg 631 | from population pop 632 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 633 | join mimic2v26.labevents lab 634 | on pop.hadm_id=lab.hadm_id 635 | and lab.itemid = 50428 636 | and lab.valuenum is not null 637 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 638 | --and pop.gender_num is not null 639 | order by 1 640 | ) 641 | --select distinct itemid from lab_platelet_1; 642 | --select * from lab_platelet_1; 643 | 644 | , lab_platelet as 645 | (select distinct icustay_id 646 | , first_value(platelet) over (partition by hadm_id order by charttime asc) as platelet_first 647 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 648 | , first_value(platelet) over (partition by hadm_id order by platelet asc) as platelet_lowest 649 | , first_value(platelet) over (partition by hadm_id order by platelet desc) as platelet_highest 650 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) platelet_abnormal_flg 651 | from lab_platelet_1 652 | order by 1 653 | ) 654 | 655 | --select * from lab_platelet; --6435 656 | 657 | --- sodium --- 658 | , lab_sodium_1 as 659 | (select pop.hadm_id 660 | , pop.icustay_id 661 | , pop.ICUSTAY_INTIME 662 | , lab.charttime 663 | , lab.itemid 664 | , lab.valueuom 665 | , lab.valuenum as sodium 666 | , case when lab.valuenum between 135 and 145 then 0 667 | else 1 end as abnormal_flg 668 | from population pop 669 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 670 | join mimic2v26.labevents lab 671 | on pop.hadm_id=lab.hadm_id 672 | and lab.itemid in (50159, 50012) ---- 50012 is for blood gas 673 | and lab.valuenum is not null 674 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 675 | --and pop.gender_num is not null 676 | order by 1 677 | ) 678 | --select distinct itemid from lab_platelet_1; 679 | --select * from lab_sodium_1; 680 | 681 | , lab_sodium as 682 | (select distinct icustay_id 683 | , first_value(sodium) over (partition by hadm_id order by charttime asc) as sodium_first 684 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 685 | , first_value(sodium) over (partition by hadm_id order by sodium asc) as sodium_lowest 686 | , first_value(sodium) over (partition by hadm_id order by sodium desc) as sodium_highest 687 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) sodium_abnormal_flg 688 | from lab_sodium_1 689 | order by 1 690 | ) 691 | 692 | --select * from lab_sodium; --6356 693 | 694 | --- potassium --- 695 | , lab_potassium_1 as 696 | (select pop.hadm_id 697 | , pop.icustay_id 698 | , pop.ICUSTAY_INTIME 699 | , lab.charttime 700 | --, lab.itemid 701 | , lab.valueuom 702 | , lab.valuenum as potassium 703 | , case when lab.valuenum between 3.7 and 5.2 then 0 704 | else 1 end as abnormal_flg 705 | from population pop 706 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 707 | join mimic2v26.labevents lab 708 | on pop.hadm_id=lab.hadm_id 709 | and lab.itemid in (50149, 50009) ---- 50009 is from blood gas 710 | and lab.valuenum is not null 711 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 712 | --and pop.gender_num is not null 713 | order by 1 714 | ) 715 | --select distinct itemid from lab_platelet_1; 716 | --select * from lab_potassium_1; 717 | 718 | , lab_potassium as 719 | (select distinct icustay_id 720 | , first_value(potassium) over (partition by hadm_id order by charttime asc) as potassium_first 721 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 722 | , first_value(potassium) over (partition by hadm_id order by potassium asc) as potassium_lowest 723 | , first_value(potassium) over (partition by hadm_id order by potassium desc) as potassium_highest 724 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) potassium_abnormal_flg 725 | from lab_potassium_1 726 | order by 1 727 | ) 728 | 729 | --select * from lab_potassium; --6371 730 | 731 | --- bicarbonate --- 732 | , lab_tco2_1 as 733 | (select pop.hadm_id 734 | , pop.icustay_id 735 | , pop.ICUSTAY_INTIME 736 | , lab.charttime 737 | --, lab.itemid 738 | , lab.valueuom 739 | , lab.valuenum as tco2 740 | , case when lab.valuenum between 22 and 28 then 0 else 1 end as abnormal_flg 741 | from population pop 742 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 743 | join mimic2v26.labevents lab 744 | on pop.hadm_id=lab.hadm_id 745 | and lab.itemid in (50025,50022,50172)--- (50025,50022) are from blood gas 746 | and lab.valuenum is not null 747 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 748 | --and pop.gender_num is not null 749 | order by 1 750 | ) 751 | 752 | --select * from lab_tco2_1; 753 | 754 | , lab_tco2 as 755 | (select distinct icustay_id 756 | , first_value(tco2) over (partition by hadm_id order by charttime asc) as tco2_first 757 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 758 | , first_value(tco2) over (partition by hadm_id order by tco2 asc) as tco2_lowest 759 | , first_value(tco2) over (partition by hadm_id order by tco2 desc) as tco2_highest 760 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) tco2_abnormal_flg 761 | from lab_tco2_1 762 | order by 1 763 | ) 764 | 765 | --select * from lab_tco2; --6400 766 | 767 | --- chloride --- 768 | , lab_chloride_1 as 769 | (select pop.hadm_id 770 | , pop.icustay_id 771 | , pop.ICUSTAY_INTIME 772 | , lab.charttime 773 | --, lab.itemid 774 | , lab.valueuom 775 | , lab.valuenum as chloride 776 | , case when lab.valuenum between 96 and 106 then 0 else 1 end as abnormal_flg 777 | from population pop 778 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 779 | join mimic2v26.labevents lab 780 | on pop.hadm_id=lab.hadm_id 781 | and lab.itemid in (50083,50004) --- 50004 is from blood gas 782 | and lab.valuenum is not null 783 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 784 | --and pop.gender_num is not null 785 | order by 1 786 | ) 787 | 788 | --select * from lab_chloride_1; 789 | 790 | , lab_chloride as 791 | (select distinct icustay_id 792 | , first_value(chloride) over (partition by hadm_id order by charttime asc) as chloride_first 793 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 794 | , first_value(chloride) over (partition by hadm_id order by chloride asc) as chloride_lowest 795 | , first_value(chloride) over (partition by hadm_id order by chloride desc) as chloride_highest 796 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) chloride_abnormal_flg 797 | from lab_chloride_1 798 | order by 1 799 | ) 800 | 801 | --select * from lab_chloride; --6400 802 | 803 | --- bun --- 804 | , lab_bun_1 as 805 | (select pop.hadm_id 806 | , pop.icustay_id 807 | , pop.ICUSTAY_INTIME 808 | , lab.charttime 809 | --, lab.itemid 810 | , lab.valueuom 811 | , lab.valuenum as bun 812 | , case when lab.valuenum between 6 and 20 then 0 else 1 end as abnormal_flg 813 | from population pop 814 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 815 | join mimic2v26.labevents lab 816 | on pop.hadm_id=lab.hadm_id 817 | and lab.itemid = 50177 818 | and lab.valuenum is not null 819 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 820 | --and pop.gender_num is not null 821 | order by 1 822 | ) 823 | 824 | --select * from lab_bun_1; 825 | 826 | , lab_bun as 827 | (select distinct icustay_id 828 | , first_value(bun) over (partition by hadm_id order by charttime asc) as bun_first 829 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 830 | , first_value(bun) over (partition by hadm_id order by bun asc) as bun_lowest 831 | , first_value(bun) over (partition by hadm_id order by bun desc) as bun_highest 832 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) bun_abnormal_flg 833 | from lab_bun_1 834 | order by 1 835 | ) 836 | 837 | --select * from lab_bun; --6438 838 | 839 | --- creatinine --- 840 | , lab_creatinine_1 as 841 | (select pop.hadm_id 842 | , pop.icustay_id 843 | , pop.ICUSTAY_INTIME 844 | , lab.charttime 845 | , lab.valueuom 846 | , lab.valuenum as creatinine 847 | , case when pop.gender_num=1 and lab.valuenum <= 1.3 then 0 848 | when pop.gender_num=0 and lab.valuenum <= 1.1 then 0 849 | else 1 end as abnormal_flg 850 | from population pop 851 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 852 | join mimic2v26.labevents lab 853 | on pop.hadm_id=lab.hadm_id 854 | and lab.itemid = 50090 855 | and lab.valuenum is not null 856 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 857 | and pop.gender_num is not null 858 | order by 1 859 | ) 860 | 861 | --select * from lab_creatinine_1; 862 | 863 | , lab_creatinine as 864 | (select distinct icustay_id 865 | , first_value(creatinine) over (partition by hadm_id order by charttime asc) as creatinine_first 866 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 867 | , first_value(creatinine) over (partition by hadm_id order by creatinine asc) as creatinine_lowest 868 | , first_value(creatinine) over (partition by hadm_id order by creatinine desc) as creatinine_highest 869 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) creatinine_abnormal_flg 870 | from lab_creatinine_1 871 | order by 1 872 | ) 873 | 874 | --select * from lab_creatinine; --6439 875 | 876 | 877 | --- Lactate --- 878 | , lab_lactate_1 as 879 | (select pop.hadm_id 880 | , pop.icustay_id 881 | , pop.ICUSTAY_INTIME 882 | , lab.charttime 883 | , lab.valueuom 884 | , lab.valuenum as lactate 885 | , case when lab.valuenum between 0.5 and 2.2 then 0 else 1 end as abnormal_flg 886 | from population pop 887 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 888 | join mimic2v26.labevents lab 889 | on pop.hadm_id=lab.hadm_id 890 | and lab.itemid =50010 -- 891 | and lab.valuenum is not null 892 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 893 | --and pop.gender_num is not null 894 | order by 1 895 | ) 896 | 897 | --select * from lab_lactate_1; 898 | 899 | , lab_lactate as 900 | (select distinct icustay_id 901 | , first_value(lactate) over (partition by hadm_id order by charttime asc) as lactate_first 902 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 903 | , first_value(lactate) over (partition by hadm_id order by lactate asc) as lactate_lowest 904 | , first_value(lactate) over (partition by hadm_id order by lactate desc) as lactate_highest 905 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) lactate_abnormal_flg 906 | from lab_lactate_1 907 | order by 1 908 | ) 909 | 910 | --select * from lab_lactate; --4455 911 | 912 | --- PH --- 913 | , lab_ph_1 as 914 | (select pop.hadm_id 915 | , pop.icustay_id 916 | , pop.ICUSTAY_INTIME 917 | , lab.charttime 918 | , lab.valueuom 919 | , lab.valuenum as ph 920 | , case when lab.valuenum between 7.38 and 7.42 then 0 else 1 end as abnormal_flg 921 | from population pop 922 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 923 | join mimic2v26.labevents lab 924 | on pop.hadm_id=lab.hadm_id 925 | and lab.itemid = 50018 926 | and lab.valuenum is not null 927 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 928 | --and pop.gender_num is not null 929 | order by 1 930 | ) 931 | 932 | --select * from lab_ph_1; 933 | 934 | , lab_ph as 935 | (select distinct icustay_id 936 | , first_value(ph) over (partition by hadm_id order by charttime asc) as ph_first 937 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 938 | , first_value(ph) over (partition by hadm_id order by ph asc) as ph_lowest 939 | , first_value(ph) over (partition by hadm_id order by ph desc) as ph_highest 940 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) ph_abnormal_flg 941 | from lab_ph_1 942 | order by 1 943 | ) 944 | --select * from lab_ph; --5054 945 | 946 | --- po2 --- 947 | , lab_po2_1 as 948 | (select pop.hadm_id 949 | , pop.icustay_id 950 | , pop.ICUSTAY_INTIME 951 | , lab.charttime 952 | , lab.valueuom 953 | , lab.valuenum as po2 954 | , case when lab.valuenum between 75 and 100 then 0 else 1 end as abnormal_flg 955 | from population pop 956 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 957 | join mimic2v26.labevents lab 958 | on pop.hadm_id=lab.hadm_id 959 | and lab.itemid = 50019 960 | and lab.valuenum is not null 961 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 962 | --and pop.gender_num is not null 963 | order by 1 964 | ) 965 | 966 | --select * from lab_po2_1; 967 | 968 | , lab_po2 as 969 | (select distinct icustay_id 970 | , first_value(po2) over (partition by hadm_id order by charttime asc) as po2_first 971 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 972 | , first_value(po2) over (partition by hadm_id order by po2 asc) as po2_lowest 973 | , first_value(po2) over (partition by hadm_id order by po2 desc) as po2_highest 974 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) po2_abnormal_flg 975 | from lab_po2_1 976 | order by 1 977 | ) 978 | 979 | --select * from lab_po2; --4948 980 | 981 | --- paco2 --- 982 | , lab_pco2_1 as 983 | (select pop.hadm_id 984 | , pop.icustay_id 985 | , pop.ICUSTAY_INTIME 986 | , lab.charttime 987 | , lab.valueuom 988 | , lab.valuenum as pco2 989 | , case when lab.valuenum between 35 and 45 then 0 else 1 end as abnormal_flg 990 | from population pop 991 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 992 | join mimic2v26.labevents lab 993 | on pop.hadm_id=lab.hadm_id 994 | and lab.itemid = 50016 995 | and lab.valuenum is not null 996 | and lab.charttime<=pop.ICUSTAY_INTIME+12/24 997 | --and pop.gender_num is not null 998 | order by 1 999 | ) 1000 | 1001 | --select * from lab_pco2_1; 1002 | 1003 | , lab_pco2 as 1004 | (select distinct icustay_id 1005 | , first_value(pco2) over (partition by hadm_id order by charttime asc) as pco2_first 1006 | --, first_value(abnormal_flg) over (partition by hadm_id order by chartime asc) as wbs_first_abn_flg 1007 | , first_value(pco2) over (partition by hadm_id order by pco2 asc) as pco2_lowest 1008 | , first_value(pco2) over (partition by hadm_id order by pco2 desc) as pco2_highest 1009 | , first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) pco2_abnormal_flg 1010 | from lab_pco2_1 1011 | order by 1 1012 | ) 1013 | 1014 | --select * from lab_pco2; --4946 1015 | 1016 | 1017 | 1018 | ----- SVO2 --- 1019 | --, lab_svo2_1 as 1020 | --(select pop.hadm_id 1021 | --, icud.ICUSTAY_INTIME 1022 | --, ch.charttime 1023 | --, ch.value1num as svo2 1024 | --, case when ch.value1num between 60 and 80 then 0 else 1 end as abnormal_flg 1025 | --from population pop 1026 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id and ICUSTAY_SEQ =1 1027 | --join mimic2v26.chartevents ch 1028 | -- on icud.icustay_id=ch.icustay_id 1029 | -- and ch.itemid in (664,838) 1030 | -- and ch.value1num is not null 1031 | -- and ch.charttime<=icud.ICUSTAY_INTIME+12/24 1032 | --order by 1 1033 | --) 1034 | -- 1035 | ----select * from lab_svo2_1; 1036 | -- 1037 | --, lab_svo2 as 1038 | --(select distinct hadm_id 1039 | --, first_value(svo2) over (partition by hadm_id order by svo2 asc) as svo2_lowest 1040 | --, first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) as abnormal_flg 1041 | --from lab_svo2_1 1042 | --order by 1 1043 | --) 1044 | -- 1045 | ----select * from lab_svo2; --471 (not to be included) 1046 | 1047 | ----- BNP --- should be excluded 1048 | --, lab_bnp_1 as 1049 | --(select pop.hadm_id 1050 | --, icud.ICUSTAY_INTIME 1051 | --, lab.charttime 1052 | --, lab.valuenum as bnp 1053 | --, case when lab.valuenum <= 100 then 0 1054 | -- else 1 end as abnormal_flg 1055 | --from population pop 1056 | --join mimic2v26.icustay_detail icud 1057 | -- on pop.hadm_id=icud.hadm_id 1058 | -- and icud.ICUSTAY_SEQ =1 1059 | ---- and icud.gender is not null 1060 | --join mimic2v26.labevents lab 1061 | -- on pop.hadm_id=lab.hadm_id 1062 | -- and lab.itemid in (50195) 1063 | -- and lab.valuenum is not null 1064 | -- and lab.charttime<=icud.ICUSTAY_INTIME+12/24 1065 | --order by 1 1066 | --) 1067 | -- 1068 | ----select * from lab_bnp_1; 1069 | -- 1070 | --, lab_bnp as 1071 | --(select distinct hadm_id 1072 | --, first_value(bnp) over (partition by hadm_id order by bnp desc) as bnp_highest 1073 | --, first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) as abnormal_flg 1074 | --from lab_bnp_1 1075 | --order by 1 1076 | --) 1077 | -- 1078 | ----select * from lab_bnp; --346 1079 | 1080 | ----- Troponin T--- 1081 | --, lab_troponin_1 as 1082 | --(select pop.hadm_id 1083 | --, icud.ICUSTAY_INTIME 1084 | --, lab.charttime 1085 | --, lab.valuenum as troponin 1086 | --, case when lab.valuenum <= 0.1 then 0 1087 | -- else 1 end as abnormal_flg 1088 | --from population pop 1089 | --join mimic2v26.icustay_detail icud 1090 | -- on pop.hadm_id=icud.hadm_id 1091 | -- and icud.ICUSTAY_SEQ =1 1092 | ---- and icud.gender is not null 1093 | --join mimic2v26.labevents lab 1094 | -- on pop.hadm_id=lab.hadm_id 1095 | -- and lab.itemid in (50189) 1096 | -- and lab.valuenum is not null 1097 | -- and lab.charttime<=icud.ICUSTAY_INTIME+12/24 1098 | --order by 1 1099 | --) 1100 | -- 1101 | ----select * from lab_troponin_1; 1102 | -- 1103 | --, lab_troponin_t as 1104 | --(select distinct hadm_id 1105 | --, first_value(troponin) over (partition by hadm_id order by troponin desc) as troponin_t_highest 1106 | --, first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) as abnormal_flg 1107 | --from lab_troponin_1 1108 | --order by 1 1109 | --) 1110 | -- 1111 | ----select * from lab_troponin_t; --2124 1112 | -- 1113 | ----- Troponin I--- 1114 | --, lab_troponin_i_1 as 1115 | --(select pop.hadm_id 1116 | --, icud.ICUSTAY_INTIME 1117 | --, lab.charttime 1118 | --, lab.valuenum as troponin 1119 | --, case when lab.valuenum <= 10 then 0 1120 | -- else 1 end as abnormal_flg 1121 | --from population pop 1122 | --join mimic2v26.icustay_detail icud 1123 | -- on pop.hadm_id=icud.hadm_id 1124 | -- and icud.ICUSTAY_SEQ =1 1125 | ---- and icud.gender is not null 1126 | --join mimic2v26.labevents lab 1127 | -- on pop.hadm_id=lab.hadm_id 1128 | -- and lab.itemid in (50188) 1129 | -- and lab.valuenum is not null 1130 | -- and lab.charttime<=icud.ICUSTAY_INTIME+12/24 1131 | --order by 1 1132 | --) 1133 | -- 1134 | ----select * from lab_troponin_1; 1135 | -- 1136 | --, lab_troponin_i as 1137 | --(select distinct hadm_id 1138 | --, first_value(troponin) over (partition by hadm_id order by troponin desc) as troponin_i_highest 1139 | --, first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) as abnormal_flg 1140 | --from lab_troponin_i_1 1141 | --order by 1 1142 | --) 1143 | -- 1144 | ----select distinct hadm_id from ( 1145 | ----select * from lab_troponin_i 1146 | ----union 1147 | ----select * from lab_troponin_t); --2552 1148 | -- 1149 | ----- CK test--- 1150 | --, lab_ck_1 as 1151 | --(select pop.hadm_id 1152 | --, icud.ICUSTAY_INTIME 1153 | --, lab.charttime 1154 | --, lab.valuenum as ck 1155 | --, case when lab.valuenum <= 120 then 0 1156 | -- else 1 end as abnormal_flg 1157 | --from population pop 1158 | --join mimic2v26.icustay_detail icud 1159 | -- on pop.hadm_id=icud.hadm_id 1160 | -- and icud.ICUSTAY_SEQ =1 1161 | ---- and icud.gender is not null 1162 | --join mimic2v26.labevents lab 1163 | -- on pop.hadm_id=lab.hadm_id 1164 | -- and lab.itemid in (50087) 1165 | -- and lab.valuenum is not null 1166 | -- and lab.charttime<=icud.ICUSTAY_INTIME+12/24 1167 | --order by 1 1168 | --) 1169 | -- 1170 | ----select * from lab_ck_1; 1171 | -- 1172 | --, lab_ck as 1173 | --(select distinct hadm_id 1174 | --, first_value(ck) over (partition by hadm_id order by ck desc) as ck_highest 1175 | --, first_value(abnormal_flg) over (partition by hadm_id order by abnormal_flg desc) as abnormal_flg 1176 | --from lab_ck_1 1177 | --order by 1 1178 | --) 1179 | -- 1180 | ----select * from lab_ck; --2948 1181 | 1182 | -------------------------------------------------------------------------------------------------------- 1183 | -------------------------------------------------------------------------------------------------------- 1184 | -------------------------- fluids data ------------------------------------------------------------------- 1185 | -------------------------------------------------------------------------------------------------------- 1186 | -------------------------------------------------------------------------------------------------------- 1187 | 1188 | , total_fluid_in_1st as 1189 | (select distinct pop.icustay_id 1190 | --, tb.charttime 1191 | , first_value(tb.CUMVOLUME) over (partition by pop.icustay_id order by tb.charttime asc) as total_fluid_1st 1192 | from population pop 1193 | join mimic2v26.totalbalevents tb on tb.icustay_id=pop.icustay_id and tb.itemid=1 1194 | where tb.cumvolume>100 1195 | order by 1 1196 | ) 1197 | --select * from total_fluid_in_1st; 1198 | 1199 | , total_fluid_in_1 as 1200 | (select distinct pop.icustay_id 1201 | --, pop.hadm_id 1202 | --, pop.icustay_intime 1203 | --, pop.icustay_outtime 1204 | --, count(distinct tb.charttime) as count 1205 | , tb.charttime 1206 | , tb.CUMVOLUME 1207 | from population pop 1208 | join mimic2v26.totalbalevents tb on tb.icustay_id=pop.icustay_id and tb.itemid=1 1209 | --and tb.charttime between pop.icustay_intime-1 and pop.icustay_outtime+1 1210 | --group by pop.icustay_id 1211 | ) 1212 | 1213 | --select * from total_fluid_in_1 order by 1; -- 2212 1214 | , total_fluid_count as 1215 | (select icustay_id 1216 | , count(distinct charttime) as count 1217 | from total_fluid_in_1 1218 | group by icustay_id 1219 | ) 1220 | 1221 | --select * from total_fluid_count; 1222 | , total_fluid as 1223 | (select icustay_id 1224 | , sum(cumvolume) as totalvolume 1225 | from total_fluid_in_1 1226 | group by icustay_id 1227 | ) 1228 | --select * from total_fluid; 1229 | 1230 | , normalized_fluid_in as 1231 | (select distinct t.icustay_id 1232 | , round(t.totalvolume/c.count, 2) as fluid_per_day 1233 | --, c.count 1234 | from total_fluid t 1235 | join total_fluid_count c on t.icustay_id=c.icustay_id 1236 | ) 1237 | 1238 | --select * from normalized_fluid_in; 1239 | 1240 | ----------- extract total fluid --------------- 1241 | --, total_fluid_in_24_echo as 1242 | --(select distinct pop.hadm_id 1243 | ----, icud.icustay_id 1244 | ----, icud.HOSPITAL_ADMIT_DT 1245 | ----, icud.HOSPITAL_DISCH_DT 1246 | ----, icud.icustay_intime 1247 | ----, icud.icustay_outtime 1248 | ----, tb.CHARTTIME 1249 | ----, tb.CUMVOLUME 1250 | --, first_value(tb.CUMVOLUME) over (partition by pop.hadm_id order by tb.charttime asc) as fluid_24hr 1251 | ----, tb.ACCUMPERIOD 1252 | ----, echo.flg 1253 | ----, echo.echo_time 1254 | --from population pop 1255 | ----join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id 1256 | --join mimic2v26.totalbalevents tb on tb.icustay_id=icud.icustay_id and tb.itemid=1 1257 | --and icud.icustay_outtime>=tb.charttime+1 1258 | --and tb.charttime>=icud.icustay_intime+1 1259 | --join echo_group echo on pop.hadm_id=echo.hadm_id and tb.charttime>=echo.echo_time 1260 | --) 1261 | -- 1262 | ----select * from total_fluid_in_24_echo order by 1; -- 2212 1263 | -- 1264 | --, total_fluid_in_24_noecho as 1265 | --(select distinct pop.hadm_id 1266 | ----, icud.icustay_id 1267 | ----, icud.HOSPITAL_ADMIT_DT 1268 | ----, icud.HOSPITAL_DISCH_DT 1269 | ----, tb.CHARTTIME 1270 | ----, icud.icustay_intime 1271 | ----, icud.icustay_outtime 1272 | ----, tb.CUMVOLUME 1273 | --, first_value(tb.CUMVOLUME) over (partition by pop.hadm_id order by tb.charttime asc) as fluid_24hr 1274 | ----, tb.ACCUMPERIOD 1275 | --from population pop 1276 | --join mimic2v26.icustay_detail icud on pop.hadm_id=icud.hadm_id 1277 | --join mimic2v26.totalbalevents tb on tb.icustay_id=icud.icustay_id 1278 | -- and tb.itemid=1 1279 | -- and tb.charttime>=icud.icustay_intime+1 1280 | -- and icud.icustay_outtime>=tb.charttime+1 1281 | --) 1282 | -- 1283 | ----select * from total_fluid_in_24_noecho order by 1; --4511 1284 | -- 1285 | --, total_fluid_in as 1286 | --(select distinct pop.hadm_id 1287 | --, case when pop.hadm_id in (select hadm_id from echo_group_final) then e.fluid_24hr else ne.fluid_24hr end as fluid_24hr 1288 | --from population pop 1289 | --left join total_fluid_in_24_echo e on pop.hadm_id=e.hadm_id 1290 | --left join total_fluid_in_24_noecho ne on pop.hadm_id=ne.hadm_id 1291 | --) 1292 | -- 1293 | ----select * from total_fluid_in order by 1; 1294 | -- 1295 | ----, total_fluid_in as 1296 | ----(select distinct hadm_id 1297 | ----, count(distinct charttime) as cnt 1298 | ----, sum(cumvolume) as total_vol 1299 | ----, round(sum(cumvolume)/count(distinct charttime),2) as vol_per_day 1300 | ----from total_fluid_in_1 1301 | ----group by hadm_id) 1302 | 1303 | --select * from total_fluid_in; 1304 | 1305 | -------------------------------------------------------------------------------------------------------- 1306 | -------------------------------------------------------------------------------------------------------- 1307 | -------------------------- Raw data ------------------------------------------------------------------- 1308 | -------------------------------------------------------------------------------------------------------- 1309 | -------------------------------------------------------------------------------------------------------- 1310 | 1311 | 1312 | , echo_data as 1313 | (select distinct 1314 | pop.* 1315 | 1316 | , coalesce(vent.flg,0) as vent_flg 1317 | , case when vent.vent_free_day is null then pop.icu_los_day else vent.vent_free_day end as vent_free_day 1318 | , coalesce(vent12.flg,0) as vent_12hr_flg 1319 | , coalesce(vaso.flg,0) as vaso_flg 1320 | , case when vaso.vaso_free_day is null then pop.icu_los_day else vaso.vaso_free_day end as vaso_free_day 1321 | , coalesce(vaso12.flg,0) as vaso_12hr_flg 1322 | , coalesce(anes.flg,0) as anes_flg 1323 | , case when anes.anes_free_day is null then pop.icu_los_day else anes.anes_free_day end as anes_free_day 1324 | , coalesce(anes12.flg,0) as anes_12hr_flg 1325 | 1326 | , coalesce(chf.flg,0) as chf_flg 1327 | , coalesce(afib.flg,0) as afib_flg 1328 | , coalesce(renal.flg,0) as renal_flg 1329 | , coalesce(liver.flg,0) as liver_flg 1330 | , coalesce(copd.flg,0) as copd_flg 1331 | , coalesce(cad.flg,0) as cad_flg 1332 | , coalesce(stroke.flg,0) as stroke_flg 1333 | , coalesce(mal.flg,0) as mal_flg 1334 | 1335 | , m.map_1st 1336 | , m.map_lowest 1337 | , m.map_highest 1338 | , hr.hr_1st 1339 | , hr.hr_lowest 1340 | , hr.hr_highest 1341 | , t.temp_1st 1342 | , t.temp_lowest 1343 | , t.temp_highest 1344 | --, cvp.cvp_1st 1345 | --, cvp.cvp_lowest 1346 | --, cvp.cvp_highest 1347 | 1348 | , wbc.wbc_first 1349 | , wbc.wbc_lowest 1350 | , wbc.wbc_highest 1351 | , wbc.wbc_abnormal_flg 1352 | , hgb.hgb_first 1353 | , hgb.hgb_lowest 1354 | , hgb.hgb_highest 1355 | , hgb.hgb_abnormal_flg 1356 | , platelet.platelet_first 1357 | , platelet.platelet_lowest 1358 | , platelet.platelet_highest 1359 | , platelet.platelet_abnormal_flg 1360 | , sodium.sodium_first 1361 | , sodium.sodium_lowest 1362 | , sodium.sodium_highest 1363 | , sodium.sodium_abnormal_flg 1364 | , potassium.potassium_first 1365 | , potassium.potassium_lowest 1366 | , potassium.potassium_highest 1367 | , potassium.potassium_abnormal_flg 1368 | , tco2.tco2_first 1369 | , tco2.tco2_lowest 1370 | , tco2.tco2_highest 1371 | , tco2.tco2_abnormal_flg 1372 | , chloride.chloride_first 1373 | , chloride.chloride_lowest 1374 | , chloride.chloride_highest 1375 | , chloride.chloride_abnormal_flg 1376 | , bun.bun_first 1377 | , bun.bun_lowest 1378 | , bun.bun_highest 1379 | , bun.bun_abnormal_flg 1380 | , lactate.lactate_first 1381 | , lactate.lactate_lowest 1382 | , lactate.lactate_highest 1383 | , lactate.lactate_abnormal_flg 1384 | , creatinine.creatinine_first 1385 | , creatinine.creatinine_lowest 1386 | , creatinine.creatinine_highest 1387 | , creatinine.creatinine_abnormal_flg 1388 | , ph.ph_first 1389 | , ph.ph_lowest 1390 | , ph.ph_highest 1391 | , ph.ph_abnormal_flg 1392 | , po2.po2_first 1393 | , po2.po2_lowest 1394 | , po2.po2_highest 1395 | , po2.po2_abnormal_flg 1396 | , pco2.pco2_first 1397 | , pco2.pco2_lowest 1398 | , pco2.pco2_highest 1399 | , pco2.pco2_abnormal_flg 1400 | 1401 | , f1.total_fluid_1st 1402 | , nf.fluid_per_day 1403 | 1404 | from population pop 1405 | left join vent_group vent on vent.icustay_id=pop.icustay_id 1406 | left join vent_12hr_group vent12 on vent12.icustay_id=pop.icustay_id 1407 | left join vaso_group vaso on vaso.icustay_id=pop.icustay_id 1408 | left join vaso_12hr_group vaso12 on vaso12.icustay_id=pop.icustay_id 1409 | left join anes_group anes on anes.icustay_id=pop.icustay_id 1410 | left join anes_12hr_group anes12 on anes12.icustay_id=pop.icustay_id 1411 | 1412 | left join chf_group chf on chf.hadm_id=pop.hadm_id 1413 | left join afib_group afib on afib.hadm_id=pop.hadm_id 1414 | left join renal_group renal on renal.hadm_id=pop.hadm_id 1415 | left join liver_group liver on liver.hadm_id=pop.hadm_id 1416 | left join copd_group copd on copd.hadm_id=pop.hadm_id 1417 | left join cad_group cad on cad.hadm_id=pop.hadm_id 1418 | left join stroke_group stroke on stroke.hadm_id=pop.hadm_id 1419 | left join malignancy_group mal on mal.hadm_id=pop.hadm_id 1420 | 1421 | left join map_group m on m.icustay_id=pop.icustay_id 1422 | left join hr_group hr on hr.icustay_id=pop.icustay_id 1423 | left join t_group t on t.icustay_id=pop.icustay_id 1424 | --left join cvp_group cvp on cvp.icustay_id=pop.icustay_id 1425 | 1426 | left join lab_wbc wbc on wbc.icustay_id=pop.icustay_id 1427 | left join lab_hgb hgb on hgb.icustay_id=pop.icustay_id 1428 | left join lab_platelet platelet on platelet.icustay_id=pop.icustay_id 1429 | left join lab_sodium sodium on sodium.icustay_id=pop.icustay_id 1430 | left join lab_potassium potassium on potassium.icustay_id=pop.icustay_id 1431 | left join lab_tco2 tco2 on tco2.icustay_id=pop.icustay_id 1432 | left join lab_chloride chloride on chloride.icustay_id=pop.icustay_id 1433 | left join lab_bun bun on bun.icustay_id=pop.icustay_id 1434 | left join lab_creatinine creatinine on creatinine.icustay_id=pop.icustay_id 1435 | left join lab_lactate lactate on lactate.icustay_id=pop.icustay_id 1436 | left join lab_ph ph on ph.icustay_id=pop.icustay_id 1437 | left join lab_po2 po2 on po2.icustay_id=pop.icustay_id 1438 | left join lab_pco2 pco2 on pco2.icustay_id=pop.icustay_id 1439 | 1440 | left join total_fluid_in_1st f1 on f1.icustay_id=pop.icustay_id 1441 | left join normalized_fluid_in nf on nf.icustay_id=pop.icustay_id 1442 | ) 1443 | 1444 | select * from echo_data; 1445 | 1446 | 1447 | -------------------------------------------------------------------------------------------------------- 1448 | -------------------------------------------------------------------------------------------------------- 1449 | -------------------------- Clean version of the data ------------------------------------------------------------------- 1450 | -------------------------------------------------------------------------------------------------------- 1451 | -------------------------------------------------------------------------------------------------------- 1452 | drop table echo_sepsis_clean_dec13; 1453 | create table echo_sepsis_clean_dec13 as 1454 | select 1455 | SUBJECT_ID 1456 | ,HADM_ID 1457 | ,ICU_LOS_DAY 1458 | ,HOSPITAL_LOS_DAY 1459 | ,AGE 1460 | ,HOSP_EXP_FLG 1461 | ,GENDER_MALE 1462 | ,WEIGHT 1463 | ,SAPS 1464 | ,SOFA 1465 | ,TWENTY_EIGHT_DAY_MORT_PT 1466 | ,ONE_YR_MORT_PT 1467 | ,ONE_YEAR_SURVIVAL_PT 1468 | ,VENT_FLG 1469 | ,VENT_TIME_DAY 1470 | ,VASO_FLG 1471 | ,VASO_TIME_DAY 1472 | ,VENT_12HR_FLG 1473 | ,VASO_12HR_FLG 1474 | ,DOBUTAMINE_FLG 1475 | ,ENDOCARDITIS_FLG 1476 | ,CHF_FLG 1477 | ,AFIB_FLG 1478 | ,RENAL_FLG 1479 | ,LIVER_FLG 1480 | ,COPD_FLG 1481 | ,CAD_FLG 1482 | ,STROKE_FLG 1483 | ,WBC_HIGHEST 1484 | ,WBC_FLG 1485 | ,LACTATE_HIGHEST 1486 | ,LAC_FLG 1487 | ,PH_LOWEST 1488 | ,PH_FLG 1489 | ,PO2_LOWEST 1490 | ,PO2_FLG 1491 | ,CREATININE_HIGHEST 1492 | ,CR_FLG 1493 | ,TCO2_LOWEST 1494 | ,TCO2_FLG 1495 | ,CK_HIGHEST 1496 | ,CK_FLG 1497 | ,SERVICE_UNIT 1498 | ,SERVICE_NUM 1499 | ,FLUID_24HR 1500 | ,DAY_28_FLG 1501 | ,DAY_365_FLG 1502 | ,DAY_365_CENSOR 1503 | ,SURVIVE_DAY 1504 | ,ECHO_FLG 1505 | ,ECHO_TIME_DAY 1506 | --,ECHO_1DAY_FLG 1507 | --,ECHO_2DAY_FLG 1508 | from mornin.echo_sepsis_raw_dec13 1509 | where SUBJECT_ID is not null 1510 | and HADM_ID is not null 1511 | and ICU_LOS_DAY is not null 1512 | and HOSPITAL_LOS_DAY is not null 1513 | and AGE is not null 1514 | and HOSP_EXP_FLG is not null 1515 | and GENDER_MALE is not null 1516 | and WEIGHT is not null 1517 | and SAPS is not null 1518 | and SOFA is not null 1519 | and TWENTY_EIGHT_DAY_MORT_PT is not null 1520 | and ONE_YR_MORT_PT is not null 1521 | and ONE_YEAR_SURVIVAL_PT is not null 1522 | 1523 | and VENT_FLG is not null 1524 | --and VENT_TIME_DAY is not null 1525 | and VASO_FLG is not null 1526 | --and VASO_TIME_DAY is not null 1527 | and VENT_12HR_FLG is not null 1528 | and VASO_12HR_FLG is not null 1529 | 1530 | and DOBUTAMINE_FLG is not null 1531 | and ENDOCARDITIS_FLG is not null 1532 | and CHF_FLG is not null 1533 | and AFIB_FLG is not null 1534 | and RENAL_FLG is not null 1535 | and LIVER_FLG is not null 1536 | and COPD_FLG is not null 1537 | and CAD_FLG is not null 1538 | and STROKE_FLG is not null 1539 | 1540 | and WBC_HIGHEST is not null 1541 | and WBC_FLG is not null 1542 | --and LACTATE_HIGHEST is not null 1543 | --and LAC_FLG is not null 1544 | and PH_LOWEST is not null 1545 | and PH_FLG is not null 1546 | and PO2_LOWEST is not null 1547 | and PO2_FLG is not null 1548 | and CREATININE_HIGHEST is not null 1549 | and CR_FLG is not null 1550 | and TCO2_LOWEST is not null 1551 | and TCO2_FLG is not null 1552 | --and CK_HIGHEST is not null --exclude 1553 | --and CK_FLG is not null 1554 | 1555 | and SERVICE_UNIT is not null 1556 | and SERVICE_NUM is not null 1557 | ----and FLUID_24HR is not null 1558 | and DAY_28_FLG is not null 1559 | and DAY_365_FLG is not null 1560 | and DAY_365_CENSOR is not null 1561 | and SURVIVE_DAY is not null 1562 | and ECHO_FLG is not null 1563 | --and ECHO_TIME_DAY is not null 1564 | --and ECHO_1DAY_FLG is not null 1565 | --and ECHO_2DAY_FLG is not null; 1566 | ; 1567 | 1568 | 1569 | 1570 | -------------------------------------------------------------------------------------------------------- 1571 | -------------------------------------------------------------------------------------------------------- 1572 | -------------------------- PS score matching ------------------------------------------------------------------- 1573 | -------------------------------------------------------------------------------------------------------- 1574 | -------------------------------------------------------------------------------------------------------- 1575 | 1576 | 1577 | select hadm_id, echo_flg 1578 | from echo_ps_dec13 1579 | where echo_flg=1 and weight is not null; 1580 | 1581 | select hadm_id 1582 | from echo_ps_dec13 1583 | where echo_flg=0 and weight is not null; 1584 | 1585 | 1586 | 1587 | 1588 | 1589 | 1590 | 1591 | --------------------------------------------------------------------------------