📄 mfx.ado
字号:
cap drop ``j''
rename `junk' ``j''
}
}
local j = `j' + 1
}
tempvar Y
$T_mfver qui predict double `Y', `predopt'
scalar `y' = `Y'[1]
restore
end
*----------------------------------------------------------
program define NL_dfdx, rclass
args at ism isv dummy total predict discret trace
if (`trace'>=1) {
di as text _n "calculating dydx (nonlinear method)"
}
tempname dfdx B names
mat `B' = e(b)
local cnum = colsof(`B')
local names : colnames `B'
tokenize `names'
mat `dfdx' = J(1,`total', 0)
mat colnames `dfdx' = `xscol'
if (`trace'>=1) {
di in smcl in gr _n /*
*/ "{hline 9}{c TT}{hline 15}" _n /*
*/ %8s "variable" " {c |}" _col(17) /*
*/ "dy/dx" _n "{hline 9}{c +}{hline 15}"
}
local i 1
local j 1
while `i' <= `cnum' {
if `ism'[1, `i'] != 0 {
if `isv'[1,`i'] {
local isdum : word `j' of `dummy'
if `isdum' & ("`discret'" == "") {
dfdx_discrete `at' `i' "`predict'" 0 `trace'
}
else {
dfdx_cts `at' `i' "`predict'" 0 `trace'
}
mat `dfdx'[1,`j'] = r(dfdx)
if (`trace'>=1) {
di in smcl in gr %8s /*
*/ abbrev(`"``i''"',8) " {c |}" /*
*/_col(17) as result %07.5g `dfdx'[1,`j']
}
}
local j = `j' + 1
}
local i = `i' + 1
}
if (`trace'>=1) {
di in smcl in gr "{hline 9}{c BT}{hline 15}" _n
}
return matrix dfdx `dfdx'
end
*----------------------------------------------------------
program define L_dfdx, rclass
args at ism isv dummy total predict discret trace
if (`trace'>=1){
di as text _n "calculating dydx (linear method)"
}
tempname dfdx se_dfdx B names
mat `B' = e(b)
local cnum = colsof(`B')
local names : colnames `B'
tokenize `names'
mat `dfdx' = J(1,`total', 0)
mat `se_dfdx' = J(1, `total', 0)
L_dfdxb `at' "`predict'" 0 `trace'
tempname dfdxb
mat `dfdxb' = r(dfdxb)
if (`trace'>=1) {
di in smcl in gr _n /*
*/ "{hline 9}{c TT}{hline 15}" _n /*
*/ %8s "variable" " {c |}" _col(17) /*
*/ "dy/dx" _n "{hline 9}{c +}{hline 15}"
}
local i = 1
local j = 1
while `i' <= `cnum' {
if `ism'[1,`i']!=0 {
if `isv'[1,`i'] {
local isdum : word `j' of `dummy'
if `isdum' & ("`discret'" == "" ) {
dfdx_discrete `at' `i' "`predict'" 0
mat `dfdx'[1,`j'] = r(dfdx)
}
else {
mat `dfdx'[1,`j'] = /*
*/ `dfdxb'[1,`ism'[1,`i']]*`B'[1,`i']
}
if (`trace'>=1) {
di in smcl in gr /*
*/ %8s abbrev(`"``i''"',8) " {c |}"/*
*/ _col(17) as result %07.5g /*
*/`dfdx'[1,`j']
}
}
local j = `j' + 1
}
local i = `i' + 1
}
if (`trace'>=1) {
di in smcl in gr "{hline 9}{c BT}{hline 15}" _n
}
return matrix dfdx `dfdx'
return matrix dfdxb `dfdxb'
end
*----------------------------------------------------------
program define NL_se, rclass
args at ism isv dummy total predict discret lny trace
if (`trace'>=1) {
di as text _n "calculating standard errors " /*
*/ "(nonlinear method)"
}
tempname se_dfdx B
mat `B' = e(b)
local cnum = colsof(`B')
local names : colnames `B'
tokenize `names'
mat `se_dfdx' = J(1, `total', 0)
local i = 1
local j = 1
while `i' < = `cnum' {
if `ism'[1,`i'] != 0 {
if `isv'[1,`i'] {
if (`trace'>=1) {
di _n as text "``i'' ... " _c
}
local isdum : word `j' of `dummy'
if `isdum' & ("`discret'" == "") {
if (`trace'>=1) {
di as text "discrete"
}
Delta_meth `at' `i' 1 "`predict'" /*
*/ `lny' `trace'
}
else {
if (`trace'>=1) {
di as text "continuous"
}
Delta_meth `at' `i' 0 "`predict'" /*
*/ `lny' `trace'
}
mat `se_dfdx'[1,`j'] = r(se_dfdx)
if (`trace'==1) {
di
}
if (`trace'>=1) {
di as text "``i'': "/*
*/"Std. Err. = " as result /*
*/r(se_dfdx)
}
}
local j = `j' + 1
}
local i = `i' + 1
}
return matrix se_dfdx `se_dfdx'
end
*----------------------------------------------------------
program define L_se, rclass
args at ism isv dummy total predict discret lny dfdxb trace
if (`trace'>=1) {
di as text _n "calculating standard errors "/*
*/"(linear method)"
}
tempname se_dfdx B eqnum dmdxb names
mat `B' = e(b)
local cnum = colsof(`B')
local names : colnames `B'
tokenize `names'
if `lny'!=0 {
L_dfdxb `at' "`predict'" `lny' `trace'
tempname dfdxb
mat `dfdxb' = r(dfdxb)
}
mat `se_dfdx' = J(1, `total', 0)
L_dmdb `at' "`predict'" `lny' `trace'
mat `dmdxb' = r(dmdxb)
mat `eqnum' = r(eqnum)
local i = 1
local j = 1
while `i' <= `cnum' {
if `ism'[1,`i'] {
if `isv'[1,`i'] {
if (`trace'>=1) {
di _n as text "``i'' ... " _c
}
local isdum : word `j' of `dummy'
if `isdum' & ("`discret'" == "") {
if (`trace'>=1) {
di as text "discrete"
}
if (`trace'==1) {
di as text _col(4) "Step: " _c
}
Delta_meth `at' `i' 1 "`predict'" /*
*/ `lny' `trace'
mat `se_dfdx'[1,`j'] = r(se_dfdx)
}
else {
if (`trace'>=1) {
di as text "continuous"
}
if (`trace'==1) {
di as text _col(4) "Step: " _c
}
L_Delta_meth `at' `i' `ism' /*
*/ `eqnum' `dfdxb' /*
*/ `dmdxb' `lny' `trace'
mat `se_dfdx'[1,`j'] = r(se_dfdx)
}
if (`trace'==1) {
di
}
if (`trace'>=1) {
di as text "``i'':"/*
*/" Std. Err. = " as result r(se_dfdx)
}
}
local j = `j' + 1
}
local i = `i' + 1
}
return matrix se_dfdx `se_dfdx'
end
*----------------------------------------------------------
program define Delta_meth, rclass
args X i isdum predopt lny trace
tempname dmdb DM se B V
mat `B' = e(b)
mat `V' = e(V)
local m = colsof(`B')
local cname : colname `B'
tokenize `cname'
mat `DM' = J(1, `m', 0)
local j = 1
while `j' <= `m' {
if (`trace'>=1) {
display as text " `j' " _c
}
NL_dmdb `X' `i' `j' `isdum' "`predopt'" `lny' `trace'
scalar `dmdb' = r(dmdb)
if (`trace'>=2) {
di as text " d^2f/dxdb = " as result `dmdb'
}
if `dmdb'>=. {
di in gr in smcl _n "warning: derivative missing;"/*
*/ " try {help j_mfxscale##|_new:rescaling}" /*
*/ " variable " as result "``j''" _n
exit
}
mat `DM'[1,`j'] = `dmdb'
local j = `j'+1
}
mat `V'=`DM'*`V'*`DM''
scalar `se' = sqrt(`V'[1,1])
return scalar se_dfdx = `se'
end
*----------------------------------------------------------
program define L_Delta_meth, rclass
args at i ism eqnum dfdxb dmdxb lny trace
tempname B V DM dmdb eqnm se
mat `B' = e(b)
local eqname : coleq `B'
mat `V' = e(V)
local ncol = colsof(`B')
mat `DM' = J(1,`ncol', 0)
local j = 1
while `j' <= `ncol' {
if (`trace'>=1) {
di as text " `j' " _c
}
scalar `dmdb' = `dmdxb'[`ism'[1,`i'], `eqnum'[1,`j']] /*
*/ *`B'[1,`i'] * `at'[1,`j']
if `i' == `j' {
scalar `dmdb' = `dmdb' + `dfdxb'[1,`ism'[1,`i']]
}
if (`trace'>=2) {
di as text " d^2f/dxdb = " as result `dmdb'
}
mat `DM'[1,`j'] = `dmdb'
local j = `j' + 1
}
mat `V' = `DM'*`V'*`DM''
scalar `se' = sqrt(`V'[1,1])
return scalar se_dfdx = `se'
end
*----------------------------------------------------------
program define NL_dmdb, rclass
args X i j isdum predopt lny trace
tempname est0 B hb dmdb
est hold `est0', copy
preserve
mat `B' = e(b)
local epsf 1e-3
scalar `hb' = (abs(`B'[1, `j']) + `epsf')*`epsf'
if (`trace'>=4) {
display as text _n "initial hb = " as result `hb'
}
GetHb `hb' `X' `i' `B' `j' `isdum' "`predopt'" `lny' `trace'
if `hb' == 0 {
scalar `dmdb' = 0
}
else {
if `hb' >= . {
scalar `dmdb' = .
}
else {
tempname B1 m1 m2
mat `B1' = `B'
mat `B1'[1, `j'] = `B'[1,`j'] + `hb'/2
if (`trace'>=4) {
di " " _c
}
GetM `m1' `X' `i' `B1' `isdum' "`predopt'" /*
*/ `lny' `trace'
if (`trace'>=4) {
di " " _c
}
mat `B1'[1, `j'] = `B'[1,`j'] - `hb'/2
GetM `m2' `X' `i' `B1' `isdum' "`predopt'" /*
*/ `lny' `trace'
scalar `dmdb' = (`m1'-`m2')/`hb'
}
}
if (`trace'>=4) {
di as text "hb = " as result `hb' _c
}
return scalar dmdb = `dmdb'
restore
est unhold `est0'
end
program define GetHb
args hb X i B j isdum predopt lny trace
tempname m0 m1 B1 goal0 goal1 diff lh uh
local cnum = colsof(`B')
local names : colnames `B'
tokenize `names'
GetM `m0' `X' `i' `B' `isdum' "`predopt'" `lny' `trace'
if (`trace'>=4) {
di " " _c
}
mat `B1' = `B'
mat `B1'[1,`j'] = `B'[1,`j'] + `hb'
GetM `m1' `X' `i' `B1' `isdum' "`predopt'" `lny' `trace'
scalar `diff' = abs(`m0' - `m1')
if `diff' < abs(`m0' + 1e-7)*1e-7 {
scalar `hb' = 0
}
else {
local ep0 1e-5
local ep1 1e-3
scalar `goal0' = abs(`m0'+`ep0')*`ep0'
scalar `goal1' = abs(`m0'+`ep1')*`ep1'
local flag1 1
local flag2 1
local loop 0
while ((`diff' < `goal0') | `diff' > `goal1') {
if (`trace'>=3 & `loop'==0) {
di as text "Iteration (db): " _c
}
if `diff' < `goal0' {
scalar `lh' = `hb'
local flag1 0
if `flag2' {
scalar `uh' = 2* `hb'
}
}
else {
scalar `uh' = `hb'
local flag2 0
if `flag1' {
scalar `lh' = 0.5*`hb'
}
}
scalar `hb' = (`lh' + `uh')/2
local loop = `loop'+1
if (`trace'>=3) {
di as text "`loop' " _c
}
if (`trace'>=4) {
di as text " hb = " as result `hb' " "
}
if (abs(`uh' - `lh') < abs(`B'[1, `j']+1e-8)*1e-8) /*
*/ | (abs(`uh' - `lh') > abs(`B'[1, `j']+1e-5)*1e5) /*
*/ | `loop'>50 {
scalar `hb'=.
exit
}
else {
mat `B1'[1, `j']=`B'[1, `j'] + `hb'
GetM `m1' `X' `i' `B1' `isdum' /*
*/ "`predopt'" `lny' `trace'
scalar `diff' = abs(`m0' - `m1')
}
}
}
end
program define GetM, eclass
args m X i B isdum predopt lny trace
tempname V C
mat `C' = `B'
mat `V'= e(V)
est post `C' `V', noclear
if `isdum' {
dfdx_discrete `X' `i' "`predopt'" `lny' `trace'
}
else {
dfdx_cts `X' `i' "`predopt'" `lny' `trace'
}
scalar `m'=r(dfdx)
end
*----------------------------------------------------------
program define dfdx_cts, rclass
args X i predopt lny trace
preserve
AddX `X'
tempname h dfdx
local epsf 1e-6
scalar `h' = (abs(`X'[1,`i'])+`epsf')*`epsf'
GetH_cts `h' `X' `i' "`predopt'" `lny' `trace'
if `h' == 0 {
scalar `dfdx' = 0
}
else if `h' >= . {
scalar `dfdx' = .
}
else {
tempname X1 f1 f2
mat `X1' = `X'
mat `X1'[1,`i'] = `X'[1,`i'] + `h'/2
GetF `f1' `X1' `i' "`predopt'" `lny' `trace'
mat `X1'[1,`i'] = `X'[1,`i'] - `h'/2
GetF `f2' `X1' `i' "`predopt'" `lny' `trace'
scalar `dfdx' = (`f1'-`f2')/`h'
}
return scalar dfdx = `dfdx'
return scalar h= `h'
if (`trace'>=3) {
di as text "h= " `h'
}
restore
end
program define AddX
args X
local cname : colnames `X'
tokenize "`cname'"
local j=1
while "``j''" != "" {
if "``j''" != "_cons" {
cap confirm var ``j''
local rc1 = _rc
cap confirm new var ``j''
local rc2 = _rc
if `rc1' == 0 | `rc2' == 0 {
tempvar junk
qui gen double `junk' = `X'[1,`j']
cap drop ``j''
rename `junk' ``j''
}
}
local j = `j' + 1
}
end
program define GetF
args f X i predopt lny
local varlist : colnames `X'
tempvar y
local iname : word `i' of `varlist'
tempvar junk
qui gen double `junk' = `X'[1,`i'] in 1
drop `iname'
rename `junk' `iname'
$T_mfver qui predict double `y' , `predopt'
if `lny' {
scalar `f' = ln(`y'[1])
}
else scalar `f' = `y'[1]
end
program define GetH_cts
args h X i predopt lny trace
tempname f0 f1 X1 goal0 goal1 diff lh uh
GetF `f0' `X' `i' "`predopt'" `lny'
mat `X1' = `X'
mat `X1'[1,`i'] = `X'[1,`i'] + `h'
GetF `f1' `X1' `i' "`predopt'" `lny'
scalar `diff' = abs(`f0' - `f1')
if `diff' == 0 {
scalar `h' = 0
}
else {
local ep0 1e-7
local ep1 1e-6
scalar `goal0' = abs(`f0'+`ep0')*`ep0'
scalar `goal1' = abs(`f0'+`ep1')*`ep1'
local flag1 1
local flag2 1
local loop 0
while (`diff' < `goal0' | `diff' > `goal1' ) {
if (`trace'>=3 & `loop'==0) {
di as text _n "Iteration (dx): " _c
if `trace'>=4{
di as text "initial h = " `h'
}
}
if `diff' < `goal0' {
scalar `lh' = `h'
local flag1 0
if `flag2' {
scalar `uh' = 2*`h'
}
}
else {
scalar `uh' = `h'
local flag2 0
if `flag1' {
scalar `lh' = 0.5*`h'
}
}
local loop=`loop'+1
scalar `h' = (`lh'+`uh')/2
if (`trace'>=3) {
di as text "`loop' " _c
if `trace'>=4{
di as text "h= " `h'
}
}
if abs(`uh'-`lh') < 1e-15 | `loop'>100 {
scalar `h' =.
exit
}
else {
if (abs(`uh'-`lh') > 1e5) {
scalar `h' =0
exit
}
}
mat `X1'[1,`i'] = `X'[1,`i'] + `h'
GetF `f1' `X1' `i' "`predopt'" `lny'
scalar `diff' = abs(`f0' - `f1')
}
}
end
*----------------------------------------------------------
program define dfdx_discrete, rclass
args X i predopt
preserve
tempname dfdx f1 f2 X1
AddX `X'
mat `X1'=`X'
mat `X1'[1,`i'] = 1
dGetF `f1' `X1' `i' "`predopt'"
mat `X1'[1,`i'] = 0
dGetF `f2' `X1' `i' "`predopt'"
scalar `dfdx' = `f1' - `f2'
return scalar dfdx = `dfdx'
return scalar h = 1
restore
end
program define dGetF
args f X i predopt
tempname y Z
local varlist : colnames `X'
local iname : word `i' of `varlist'
tempvar junk
qui gen double `junk' = `iname'
qui replace `junk' = `X'[1,`i'] in 1
drop `iname'
rename `junk' `iname'
$T_mfver qui predict double `y', `predopt'
scalar `f' = `y'[1]
end
*----------------------------------------------------------
program define L_dfdxb, rclass
args X predopt lny trace
tempname B eqnum dfdxbi dfdxb
mat `B' = e(b)
local ncol = colsof(`B')
mat `eqnum' = J(1, `ncol', 1)
local ename : coleq `B'
local cname : colname `B'
if "`ename'" != "" {
tokenize `ename'
local j = 1
local i = 1
while "`1'" != "" {
mat `eqnum'[1,`i'] = `j'
if "`2'" != "`1'" {
local j = `j' + 1
}
mac shift
local i = `i' + 1
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -