-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example5-PROC_LOGISTIC_with_FREQ-PharmaSUG2022-Simple_and_Efficient_Bootstrap_Validation.sas
193 lines (145 loc) · 5.39 KB
/
Example5-PROC_LOGISTIC_with_FREQ-PharmaSUG2022-Simple_and_Efficient_Bootstrap_Validation.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
A Framework for Simple and Efficient Bootstrap Validation in SAS®, with Examples
Example 1: PROC LOGISTIC - PharmaSUG 2022
This notebook contains an executable version of the example in Section 2 of the paper at https://github.com/saspy-bffs/pharmasug-2022-bootstrap-validation.
*/
/*
Pre-example Setup Part 1
The code below creates macro variables encapsulating model parameters. Only SAS log output should be created.
**Note**: You may also wish to change the values of the macro variables in order to explore bootstrap validation for different models. This example is adapted from Module 10 at https://wwwn.cdc.gov/nchs/nhanes/tutorials/samplecode.aspx
*/
%let response_variable_condition = bpxsar >= 140 OR bpxdar >= 90 OR bpq050a = 1;
%let response_variable = hyper;
%let outcome = &response_variable.(EVENT='1');
%let class_variables = bpq100d dmq051 dmd110;
%let predictor_variables = lbxtc bpq100d bmxbmi ridageyr lbxtr dmq051 dmd110 indhhinc indfmpir;
/*
Pre-example Setup Part 2
The code below downloads the example dataset for Module 10 at https://wwwn.cdc.gov/nchs/nhanes/tutorials/samplecode.aspx. Only SAS log output should be created.
*/
filename tempfile "%sysfunc(pathname(work))/analysis_data.sas7bdat";
proc http
url='https://wwwn.cdc.gov/nchs/data/tutorials/analysis_data.sas7bdat'
method='get'
out=tempfile
;
run;
quit;
/*
Pre-example Setup Part 3
The code below subsets the dataset downloaded above to rows with no missing values for the predicator variables, as well as creating a response variable. Only SAS log output should be created.
*/
data example_dataset;
set work.analysis_data;
* Create outcome variable;
if (&response_variable_condition.) then &response_variable. = 1;
else &response_variable. = 0;
* Subset to observations with no missing values for outcome variable or predictor variables;
if nmiss(&response_variable., %sysfunc(tranwrd(&predictor_variables.,%str( ),%str(,)))) = 0;
keep &response_variable. &predictor_variables.;
run;
/*
Step 1: Train a Model
See page 4 of the paper at https://github.com/saspy-bffs/pharmasug-2022-bootstrap-validation. Only PROC LOGISTIC output should be created.
*/
ods output Association=model_association_table(
where=(Label2='c')
keep=Label2 nValue2
rename=(nValue2=original_model_c_statistic)
);
proc logistic data=example_dataset;
class &class_variables.;
model &outcome. = &predictor_variables.;
run;
/*
Step 2: Generate Bootstrap Samples
See page 18 of the paper at https://github.com/saspy-bffs/pharmasug-2022-bootstrap-validation. Only PROC SURVEYSELECT output should be created.
*/
proc surveyselect
data=example_dataset
out=bootstrap_samples
seed=1354687
method=urs
rep=500
samprate=1
;
run;
/*
Step 3: Train Models in Each Bootstrap
See page 18 of the paper at https://github.com/saspy-bffs/pharmasug-2022-bootstrap-validation. Only SAS log output should be created.
*/
* Turn off all output;
ods graphics off;
ods exclude all;
ods noresults;
* Trains models on all 500 bootstrap samples, capturing the resulting C-statistics;
ods output Association=bootstrap_association_table(
where=(Label2='c')
keep=Replicate Label2 nValue2
rename=(nValue2=c_statistic_value)
);
proc logistic data=bootstrap_samples outmodel=bootstap_models;
by Replicate;
freq numberhits;
class &class_variables.;
model &outcome. = &predictor_variables.;
run;
/*
Step 4: Test Bootstrap Models
See page 7 of the paper at https://github.com/saspy-bffs/pharmasug-2022-bootstrap-validation. Only SAS log output should be created.
*/
* Score original dataset with bootstap models;
ods output Scorefitstat=bootstrap_scores(
keep=Replicate AUC
rename=(AUC=c_statistic_value)
);
proc logistic inmodel=bootstap_models;
score
data=example_dataset
out=_null_
fitstat
;
by Replicate;
run;
* Turn output back on;
ods results;
ods select all;
ods graphics on;
/*
Step 5: Estimate Optimism
See page 8 of the paper at https://github.com/saspy-bffs/pharmasug-2022-bootstrap-validation. Only SAS log output should be created.
*/
proc sql;
create table model_optimism as
select
avg(A.c_statistic_value - B.c_statistic_value) as optimism
from
bootstrap_association_table as A
inner join
bootstrap_scores as B
on A.Replicate = B.Replicate
;
quit;
/*
Step 6: Adjust Performance with Optimism
See page 9 of the paper at https://github.com/saspy-bffs/pharmasug-2022-bootstrap-validation. Only PROC PRINT output should be created.
*/
* Assemble C-statistic for original model, optimism, and corrected C-statistic into a 1x3 table;
data corrected_model_evaluation;
set model_association_table;
set model_optimism;
corrected_c_statistic = original_model_c_statistic - optimism;
label
original_model_c_statistic = 'Naive C-Statistic'
optimism = 'Optimism'
corrected_c_statistic = 'Optimism-Corrected C-Statistic'
;
keep original_model_c_statistic optimism corrected_c_statistic;
run;
* Print final results;
proc print
data=corrected_model_evaluation
noobs
label
;
run;