-
Notifications
You must be signed in to change notification settings - Fork 0
/
gencrm_lf_c.ado
125 lines (93 loc) · 3.2 KB
/
gencrm_lf_c.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
*! v2.0.1, S Bauldry, 21dec2017
capture program drop gencrm_lf_c
program gencrm_lf_c
version 15
* creating arguments based on number of categories of Y stored in globals
local arguments "lnf xb_c"
forval i = 1/$nCatm1 {
local arguments "`arguments' t`i'"
}
args `arguments'
* tempvars for thresholds
forval j = 1/$nCatm1 {
tempvar tau`j'
qui gen double `tau`j'' = `t`j''
}
* 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(`tau1' - `xb_c')) if $ML_y == `y_1'
* build equations for middle values of Y
forval k = 2/$nCatm1 {
local meqn_b `" ln(invlogit(`tau`k'' - `xb_c')) "'
local meqn_a ""
local m = `k' - 1
forval n = 1/`m' {
local meqn_a `" `meqn_a' ln(1 - invlogit(`tau`n'' - `xb_c')) + "'
}
local meqn `" `meqn_a' `meqn_b' "'
qui replace `lnf' = `meqn' if $ML_y == `y_`k''
}
* build equation for last value of Y
local eqn `" ln(1 - invlogit(`tau1' - `xb_c')) "'
forval o = 2/$nCatm1 {
local eqn `" `eqn' + ln(1 - invlogit(`tau`o'' - `xb_c')) "'
}
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(`tau1' - `xb_c')) if $ML_y == `y_1'
* build equations for middle values of Y
forval k = 2/$nCatm1 {
local meqn_b `" ln(normal(`tau`k'' - `xb_c')) "'
local meqn_a ""
local m = `k' - 1
forval n = 1/`m' {
local meqn_a `" `meqn_a' ln(1 - normal(`tau`n'' - `xb_c')) + "'
}
local meqn `" `meqn_a' `meqn_b' "'
qui replace `lnf' = `meqn' if $ML_y == `y_`k''
}
* build equation for last value of Y
local eqn `" ln(1 - normal(`tau1' - `xb_c')) "'
forval o = 2/$nCatm1 {
local eqn `" `eqn' + ln(1 - normal(`tau`o'' - `xb_c')) "'
}
qui replace `lnf' = `eqn' if $ML_y == `y_`M''
}
*** likelihood function for clogog link
if ( "$Link" == "cloglog" ) {
* equation for first value of Y
qui replace `lnf' = ln(1 - exp(-exp(`tau1' - `xb_c'))) if $ML_y == `y_1'
* build equations for middle values of Y
forval k = 2/$nCatm1 {
local meqn_b `" ln(1 - exp(-exp(`tau`k'' - `xb_c'))) "'
local meqn_a ""
local m = `k' - 1
forval n = 1/`m' {
local meqn_a `" `meqn_a' ln(exp(-exp(`tau`n'' - `xb_c'))) + "'
}
local meqn `" `meqn_a' `meqn_b' "'
qui replace `lnf' = `meqn' if $ML_y == `y_`k''
}
* build equation for last value of Y
local eqn `" ln(exp(-exp(`tau1' - `xb_c'))) "'
forval o = 2/$nCatm1 {
local eqn `" `eqn' + ln(exp(-exp(`tau`o'' - `xb_c'))) "'
}
qui replace `lnf' = `eqn' if $ML_y == `y_`M''
}
end
/* History
1.0.0 11.15.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
2.0.0 12.18.17 new program name and fixed likelihood functions
2.0.1 12.21.17 added probit and cloglog