-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcr_pc_lf.ado
143 lines (111 loc) · 3.94 KB
/
cr_pc_lf.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
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
*! v1.2.0, S Bauldry, 18sep2017
capture program drop cr_pc_lf
program cr_pc_lf
version 14
* creating arguments based on number of categories of Y stored in globals
local arguments "lnf xb_c xb_p"
forval i = 2/$nCatm1 {
local arguments "`arguments' f`i'"
}
args `arguments'
forval i = 2/$nCatm1 {
tempvar phi`i'
qui gen double `phi`i'' = `f`i''
}
* setting values for y
forval j = 1/$nCat {
local y_`j' ${y_`j'}
}
local M $nCat
*** likelihood function for logit link
if ( "$Link" == "logit" ) {
* equation for first value of Y
qui replace `lnf' = ln(invlogit(-`xb_c' - `xb_p')) if $ML_y == `y_1'
* build equations for middle values of Y
if ( $nCat == 3 ) {
qui replace `lnf' = ln(1 - invlogit(-`xb_c' - `xb_p')) + ///
ln(invlogit(-`xb_c' - `xb_p'*`phi2')) if $ML_y == `y_2'
}
if ( $nCat > 3 ) {
forval k = 2/$nCatm1 {
local meqn_a `" ln(1 - invlogit(-`xb_c' - `xb_p')) + "'
local meqn_c `" ln(invlogit(-`xb_c' - `xb_p'*`phi`k'')) "'
local meqn_b ""
local m = `k' - 1
forval n = 2/`m' {
local meqn_b `" `meqn_b' ln(1 - invlogit(-`xb_c' - `xb_p'*`phi`n'')) + "'
}
local meqn `" `meqn_a' `meqn_b' `meqn_c' "'
qui replace `lnf' = `meqn' if $ML_y == `y_`k''
}
}
* build equation for last value of Y
local eqn `" ln(1 - invlogit(-`xb_c' - `xb_p')) "'
forval o = 2/$nCatm1 {
local eqn `" `eqn' + ln(1 - invlogit(-`xb_c' - `xb_p'*`phi`o'')) "'
}
qui replace `lnf' = `eqn' if $ML_y == `y_`M''
}
*** likelihood function for probit link
if ( "$Link" == "probit" ) {
* equation for first value of Y
qui replace `lnf' = ln(normal(-`xb_c' - `xb_p')) if $ML_y == `y_1'
* build equations for middle values of Y
if ( $nCat == 3 ) {
qui replace `lnf' = ln(1 - normal(-`xb_c' - `xb_p')) + ///
ln(normal(-`xb_c' - `xb_p'*`phi2')) if $ML_y == `y_2'
}
if ( $nCat > 3 ) {
forval k = 2/$nCatm1 {
local meqn_a `" ln(1 - normal(-`xb_c' - `xb_p')) + "'
local meqn_c `" ln(normal(-`xb_c' - `xb_p'*`phi`k'')) "'
local meqn_b ""
local m = `k' - 1
forval n = 2/`m' {
local meqn_b `" `meqn_b' ln(1 - normal(-`xb_c' - `xb_p'*`phi`n'')) + "'
}
local meqn `" `meqn_a' `meqn_b' `meqn_c' "'
qui replace `lnf' = `meqn' if $ML_y == `y_`k''
}
}
* build equation for last value of Y
local eqn `" ln(1 - normal(-`xb_c' - `xb_p')) "'
forval o = 2/$nCatm1 {
local eqn `" `eqn' + ln(1 - normal(-`xb_c' - `xb_p'*`phi`o'')) "'
}
qui replace `lnf' = `eqn' if $ML_y == `y_`M''
}
*** likelihood function for complementary log-log link
if ( "$Link" == "cloglog" ) {
* equation for first value of Y
qui replace `lnf' = ln(1 - exp(-exp(-`xb_c' - `xb_p'))) if $ML_y == `y_1'
* build equations for middle values of Y
if ( $nCat == 3 ) {
qui replace `lnf' = ln(exp(-exp(-`xb_c' - `xb_p'))) + ///
ln(1 - exp(-exp(-`xb_c' - `xb_p'*`phi2'))) if $ML_y == `y_2'
}
if ( $nCat > 3 ) {
forval k = 2/$nCatm1 {
local meqn_a `" ln(exp(-exp(-`xb_c' - `xb_p'))) + "'
local meqn_c `" ln(1 - exp(-exp(-`xb_c' - `xb_p'*`phi`k''))) "'
local meqn_b ""
local m = `k' - 1
forval n = 2/`m' {
local meqn_b `" `meqn_b' ln(exp(-exp(-`xb_c' - `xb_p'*`phi`n''))) + "'
}
local meqn `" `meqn_a' `meqn_b' `meqn_c' "'
qui replace `lnf' = `meqn' if $ML_y == `y_`k''
}
}
* build equation for last value of Y
local eqn `" ln(exp(-exp(-`xb_c' - `xb_p'))) "'
forval o = 2/$nCatm1 {
local eqn `" `eqn' + ln(exp(-exp(-`xb_c' - `xb_p'*`phi`o''))) "'
}
qui replace `lnf' = `eqn' if $ML_y == `y_`M''
}
end
/* History
1.0.0 11.22.16 initial likelihood program for arbitrary number of categories
1.1.0 08.25.17 generalized program for unlimited number of categories
1.2.0 09.18.17 fixed bug with non-standard values for Y