-
Notifications
You must be signed in to change notification settings - Fork 2
/
classify.ado
115 lines (78 loc) · 3.15 KB
/
classify.ado
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
/*******************************************************************************
* *
* Handles classification results to ensure classes are returned *
* *
*******************************************************************************/
*! classify
*! v 0.0.6
*! 08mar2024
// Drop program from memory if already loaded
cap prog drop classify
// Define program
prog def classify,
// Version statement
version 15
// Syntax
syntax anything(name = classes id = "Number of Classes") [if] , ///
PStub(string asis) [ THReshold(real 0.5) PMethod(string asis) ///
POpts(string asis)]
// Mark the sample that will be used
marksample touse, strok
// Set default prediction method if missing
if mi(`"`pmethod'"') loc pmethod pr
// Ensure classes is an integer
if mod(`classes', 1) != 0 {
// Display an error message
di as err "The number of classes for a classification model must be" ///
" an integer. You specified `classes' number of classes."
// Return error code and exit
error 126
} // End of IF Block to handle invalid number of classes
// Ensure classes has a value that is >= 2
if `classes' < 2 {
// Display an error message
di as err "The number of classes specified for your model is < 2."
// Return error code and exit
error 125
} // End IF Block for invalid number of classes
// Test that the threshold value is valid
if (`threshold' >= 1 | `threshold' <= 0) {
// Display an error message
di as err "The classification threshold must be in (0, 1). You " ///
"specified a value of `threshold'."
// Return error code and exit
error 125
} // End IF Block to handle invalid threshold values
// Test the number of classes:
if `classes' == 2 {
// Generate predicted values
predict double `pstub' if `touse', `pmethod' `popts'
// Replace predicted values with classes
replace `pstub' = cond(`pstub' < `threshold' & !mi(`pstub'), 0, ///
cond(`pstub' >= `threshold' & !mi(`pstub'), 1, .))
} // End IF Block for binary classification problems
// For values > 2
else {
// Generate predicted values
predict double `pstub'_* if `touse', `pmethod' `popts'
// Get the names of the variables that were just generated
qui: ds `pstub'_*
// Store varlist
loc pvars `r(varlist)'
// Identifies the highest probability value
egen double `pstub' = rowmax(`pvars')
// Loop over the predicted variable names
foreach v in `pvars' {
// Remove all letters from the variable name
loc clsval `= ustrregexra("`v'", "^.*_", "")'
// Replace the value of pstub with the class value if that is the
// highest probability
replace `pstub' = `clsval' if `pstub' == `v' & !mi(`pstub')
} // End of Loop over the predicted variables
// Remove the individual predicted variables
drop `pvars'
} // End ELSE Block for multinomial/ordinal classification problems
// Compress the classification variable
qui: compress `pstub'
// End definition of the command
end