********************************************************************** Attached below are the following ado files: dlogit2.ado dprobit2.ado dmlogit2.ado dlog_dpr.ado dlog_at.ado And help files: dlogit2.hlp dprobit2.hlp dmlogit2.hlp ********************************************************************** dlogit2.ado Instructions for use: Cut out the following program and put it in a file called "dlogit2.ado". Be sure that the file is saved as a plain text (ASCII) file and that it has a hard return at the end of the last line in the file. Put dlogit2.ado in your C:\ADO directory (you may have to create this directory) or your Stata working directory (i.e., your current directory). Note: Do not put it in the C:\STATA\ADO directory. Only official Stata ado and hlp files should be placed in this directory. For more information, see [2] ado in Vol. 1 of the Reference manuals. ---------------------------------cut here---start-of-dlogit2.ado------- *! version 1.0.0 18dec1995 program define dlogit2 version 4.0 dlog_dpr logit `*' end ---------------------------------cut here---end-of-dlogit2.ado--------- ********************************************************************** dprobit2.ado Instructions for use: Cut out the following program and put it in a file called "dprobit2.ado". Be sure that the file is saved as a plain text (ASCII) file and that it has a hard return at the end of the last line in the file. Put dprobit2.ado in your C:\ADO directory (you may have to create this directory) or your Stata working directory (i.e., your current directory). Note: Do not put it in the C:\STATA\ADO directory. Only official Stata ado and hlp files should be placed in this directory. For more information, see [2] ado in Vol. 1 of the Reference manuals. ---------------------------------cut here---start-of-dprobit2.ado------- *! version 1.0.0 18dec1995 program define dprobit2 version 4.0 dlog_dpr probit `*' end ---------------------------------cut here---end-of-dprobit2.ado--------- ********************************************************************** dmlogit2.ado Instructions for use: Cut out the following program and put it in a file called "dmlogit2.ado". Be sure that the file is saved as a plain text (ASCII) file and that it has a hard return at the end of the last line in the file. Put dmlogit2.ado in your C:\ADO directory (you may have to create this directory) or your Stata working directory (i.e., your current directory). Note: Do not put it in the C:\STATA\ADO directory. Only official Stata ado and hlp files should be placed in this directory. For more information, see [2] ado in Vol. 1 of the Reference manuals. ---------------------------------cut here---start-of-dmlogit2.ado------- *! version 1.0.3 31jan1996 program define dmlogit2 version 4.0 local options "Level(integer $S_level) noDISPat" if substr("`1'",1,1)=="," | "`*'"=="" { if "$S_E_cmd"!="dmlogit2" { error 301 } parse "`*'" } else { local varlist "req ex" local weight "fweight aweight" local if "opt" local in "opt" local options "`options' AT(string) LOG noConstant *" parse "`*'" if "`log'"=="" { local log "quietly" } else local log tempname b V x tempvar doit mark `doit' [`weight'`exp'] `if' `in' markout `doit' `varlist' parse "`varlist'", parse(" ") local y "`1'" /* Run mlogit. */ `log' mlogit `varlist' [`weight'`exp'] if `doit', /* */ `options' `constan' mat `b' = get(_b) mat `V' = get(VCE) local nobs = _result(1) local ll = _result(2) local pr2 = _result(7) /* Use colnames for indepvars since some may have been dropped. */ local colname : colnames(`b') parse "`colname'", parse(" ") local dim = colsof(`b') if "``dim''"=="_cons" { local `dim' /* erase "_cons" */ } /* Get x. */ if "`at'"!="" { local at "at(`at')" } dlog_at `doit' `*' [`weight'`exp'], x(`x') `at' `constan' /* Compute dP/dx and its covariance. */ dPdx `x' `b' `V' /* new b and V returned */ local sing "$S_1" /* Post results. */ mat post `b' `V', depn(`y') obs(`nobs') /* Compute test of indepvars = 0. */ if "`constan'"=="" & "`*'"!="" & "`sing'"=="" { qui test `*' global S_E_mdf = _result(3) global S_E_chi2 = _result(6) } else if "`sing'"!="" { global S_E_mdf "." global S_E_chi2 "." } matrix S_E_x = `x' global S_E_nobs "`nobs'" global S_E_ll "`ll'" global S_E_pr2 "`pr2'" global S_E_depv "`y'" global S_E_cmd "dmlogit2" } /* Display results. */ #delimit ; di _n in gr "Marginal effects from multinomial regression" _col(54) "Number of obs = " in ye %7.0g $S_E_nobs ; if "$S_E_mdf"!="" { ; di _col(54) in gr "chi2(" in ye $S_E_mdf in gr ")" _col(70) "= " in ye %7.2f $S_E_chi2 _n _col(54) in gr "Prob > chi2 = " in ye %7.4f chiprob($S_E_mdf, $S_E_chi2) _c ; } ; di _n in gr "Log Likelihood = " in ye %10.0g $S_E_ll _col(54) in gr "Pseudo R2 = " in ye %7.4f $S_E_pr2 _n ; #delimit cr mat mlout, level(`level') if "`dispat'"=="" { di in gr "Marginal effects evaluated at" mat list S_E_x, noheader format(%9.0g) } end program define dPdx version 4.0 local x "`1'" /* Input: row vector (1 x k) of x values */ local b "`2'" /* Input: beta (m x k) from mlogit */ /* Output: dP/dx marginal effects row vector */ local V "`3'" /* Input: covariance matrix (mk x mk) of beta */ /* Output: covariance of dP/dx */ tempname A P s sum I r D C E local m = rowsof(`b') local k = colsof(`b') mat `A' = `b'*`x'' mat `P' = J(`m',`m',0) scalar `sum' = 1 local i 1 while `i' <= `m' { scalar `s' = exp(`A'[`i',1]) scalar `sum' = `sum' + `s' mat `P'[`i',`i'] = `s' local i = `i' + 1 } scalar `sum' = 1/`sum' mat `P' = `sum'*`P' mat `A' = J(`m',`m',1) mat `A' = `A'*`P' mat `A' = `A'*`b' mat `A' = `b' - `A' mat `I' = I(`k') mat `r' = J(1,`m',0) local mk = `m'*`k' mat `D' = J(`mk',`mk',0) local i 1 while `i' <= `m' { local ki = `k'*(`i'-1) + 1 /* Make new b a row vector. */ mat `C' = `A'[`i',.] scalar `s' = `P'[`i',`i'] mat `C' = `s'*`C' if `i' == 1 { mat `b' = `C' } else mat `b' = `b' , `C' /* Do j = i. */ mat `C' = `A'[`i',.] scalar `s' = 1 - 2*`P'[`i',`i'] mat `C' = `s'*`C' mat `C' = `C''*`x' scalar `s' = 1 - `P'[`i',`i'] mat `E' = `s'*`I' mat `C' = `E' + `C' scalar `s' = `P'[`i',`i'] mat `C' = `s'*`C' mat sub `D'[`ki',`ki'] = `C' /* Do j < i. */ mat `r'[1,`i'] = 1 local j 1 while `j' < `i' { mat `r'[1,`j'] = 1 mat `C' = `r'*`A' mat `C' = `C''*`x' mat `C' = `I' + `C' scalar `s' = -`P'[`i',`i']*`P'[`j',`j'] mat `C' = `s'*`C' local kj = `k'*(`j'-1) + 1 mat sub `D'[`ki',`kj'] = `C' mat sub `D'[`kj',`ki'] = `C' mat `r'[1,`j'] = 0 local j = `j' + 1 } mat `r'[1,`i'] = 0 local i = `i' + 1 } /* If `V' is singular, zero corresponding row of D. */ mat `r' = J(1, `mk', 0) global S_1 /* erase macro */ local i 1 while `i' <= `mk' { if `V'[`i',`i'] == 0 { mat sub `D'[`i',1] = `r' global S_1 "singular" } local i = `i' + 1 } /* Make new V. */ mat `E' = `V' mat `V' = `D'*`V' mat `V' = `V'*`D'' /* Symmetrize V (it may be slightly off due to numerical error). */ mat `D' = `V'' mat `V' = `V' + `D' scalar `s' = 0.5 mat `V' = `s'*`V' /* Label new b and V. We do it in this strange fashion in case equation names contain spaces (which is possible since names can come from value labels). */ mat `r' = `E'[1,.] scalar `s' = 0 mat `r' = `s'*`r' mat `E' = `s'*`E' mat `b' = `b' + `r' mat `V' = `V' + `E' end ---------------------------------cut here---end-of-dmlogit2.ado--------- ********************************************************************** dlog_dpr.ado Instructions for use: Cut out the following program and put it in a file called "dlog_dpr.ado". Be sure that the file is saved as a plain text (ASCII) file and that it has a hard return at the end of the last line in the file. Put dlog_dpr.ado in your C:\ADO directory (you may have to create this directory) or your Stata working directory (i.e., your current directory). Note: Do not put it in the C:\STATA\ADO directory. Only official Stata ado and hlp files should be placed in this directory. For more information, see [2] ado in Vol. 1 of the Reference manuals. ---------------------------------cut here---start-of-dlog_dpr.ado------- *! version 1.0.1 10jan1996 program define dlog_dpr version 4.0 local cmd "`1'" macro shift local options "Level(integer $S_level) noDISPat" if substr("`1'",1,1)=="," | "`*'"=="" { if "$S_E_cmd"!="d`cmd'2" { error 301 } parse "`*'" } else { local varlist "req ex" local weight "aweight fweight" local if "opt" local in "opt" local options "`options' AT(string) LOG noCONstant *" parse "`*'" tempname b V x tempvar doit z mark `doit' [`weight'`exp'] `if' `in' markout `doit' `varlist' parse "`varlist'", parse(" ") local y "`1'" /* Run `cmd'. */ if "`log'"=="" { `cmd' `varlist' [`weight'`exp'] if `doit', /* */ `options' `constan' nocoef nolog } else { `cmd' `varlist' [`weight'`exp'] if `doit', /* */ `options' `constan' } mat `b' = get(_b) mat `V' = get(VCE) local nobs = _result(1) local ll = _result(2) local pr2 = _result(7) /* Use colnames for indepvars since some may have been dropped. */ local colname : colnames(`b') parse "`colname'", parse(" ") local dim = colsof(`b') if "``dim''"=="_cons" { local `dim' /* erase "_cons" */ } /* Check for dropped observations. */ qui predict `z' if `doit' markout `doit' `z' /* Get x. */ if "`at'"!="" { local at "at(`at')" } dlog_at `doit' `*' [`weight'`exp'], x(`x') `at' `constan' /* Compute dF/dx and its covariance */ dFdx `cmd' `x' `b' `V' /* new b and V returned */ local sing "$S_1" /* Post results. */ mat post `b' `V', depn(`y') obs(`nobs') /* Compute test of indepvars = 0. */ if "`constan'"=="" & "`*'"!="" & "`sing'"=="" { qui test `*' global S_E_mdf = _result(3) global S_E_chi2 = _result(6) } else if "`sing'"!="" { global S_E_mdf "." global S_E_chi2 "." } matrix S_E_x = `x' global S_E_nobs "`nobs'" global S_E_ll "`ll'" global S_E_pr2 "`pr2'" global S_E_depv "`y'" global S_E_cmd "d`cmd'2" } /* Display results. */ #delimit ; di _n in gr "Marginal effects from `cmd'" _col(54) "Number of obs = " in ye %7.0g $S_E_nobs ; if "$S_E_mdf"!="" { ; di _col(54) in gr "chi2(" in ye $S_E_mdf in gr ")" _col(70) "= " in ye %7.2f $S_E_chi2 _n _col(54) in gr "Prob > chi2 = " in ye %7.4f chiprob($S_E_mdf, $S_E_chi2) _c ; } ; di _n in gr "Log Likelihood = " in ye %10.0g $S_E_ll _col(54) in gr "Pseudo R2 = " in ye %7.4f $S_E_pr2 _n ; #delimit cr mat mlout, level(`level') if "`dispat'"=="" { di in gr "Marginal effects evaluated at" mat list S_E_x, noheader format(%9.0g) } end program define dFdx version 4.0 local cmd "`1'" /* Input: "probit" or "logit" */ local x "`2'" /* Input: row vector of x values */ local b "`3'" /* Input: beta */ /* Output: dF/dx marginal effects */ local V "`4'" /* Input: covariance matrix of beta */ /* Output: covariance of dF/dx */ tempname z df f p D I mat `z' = `b'*`x'' if "`cmd'"=="probit" { scalar `df' = -`z'[1,1] scalar `f' = exp(-`z'[1,1]*`z'[1,1]/2)/sqrt(2*_pi) } else { /* "logit" */ scalar `p' = exp(`z'[1,1])/(1 + exp(`z'[1,1])) scalar `df' = 1 - 2*`p' scalar `f' = `p'*(1 - `p') } mat `D' = `b''*`x' mat `D' = `df'*`D' local dim = colsof(`b') mat `I' = I(`dim') mat `D' = `I' + `D' mat `D' = `f'*`D' mat `b' = `f'*`b' /* If `V' is singular, zero corresponding row of D. */ mat `z' = J(1, `dim', 0) global S_1 /* erase macro */ local i 1 while `i' <= `dim' { if `V'[`i',`i'] == 0 { mat sub `D'[`i',1] = `z' global S_1 "singular" } local i = `i' + 1 } local colname : colnames(`V') mat `V' = `D'*`V' mat `V' = `V'*`D'' mat colnames `b' = `colname' mat colnames `V' = `colname' mat rownames `V' = `colname' end ---------------------------------cut here---end-of-dlog_dpr.ado--------- ********************************************************************** dlog_at.ado Instructions for use: Cut out the following program and put it in a file called "dlog_at.ado". Be sure that the file is saved as a plain text (ASCII) file and that it has a hard return at the end of the last line in the file. Put dlog_at.ado in your C:\ADO directory (you may have to create this directory) or your Stata working directory (i.e., your current directory). Note: Do not put it in the C:\STATA\ADO directory. Only official Stata ado and hlp files should be placed in this directory. For more information, see [2] ado in Vol. 1 of the Reference manuals. ---------------------------------cut here---start-of-dlog_at.ado------- *! version 1.0.1 18dec1995 program define dlog_at /* Utility program for dlogit2.ado, dmlogit2.ado, dprobit2.ado; program returns x vector based on at() option. Syntax: dlog_at markvar indepvars [weight], x(matname) [ at(string) noConstant ] */ version 4.0 local varlist "req ex" local weight "fweight aweight" local options "x(string) at(string) noConstant" parse "`*'" parse "`varlist'", parse(" ") local doit "`1'" macro shift if "`constan'" == "" { local cons 1 } else local cons 0 if "`at'" != "" { cap confirm integer number `at' if _rc == 0 { /* make x from obs # = `at' */ obs_at `at' `x' `cons' `*' exit } cap di `at'[1,1] if _rc == 0 { /* order `x' to match indepvars */ order_at `at' `x' `cons' `*' exit } cap count if `doit' & (`at') if _rc == 0 & _result(1) == 0 { di in red "at(expression) yields no observations" /* */ " in the estimation sample" exit 2000 } if _rc { di in red "syntax error in at() option" exit 198 } local at "& (`at')" /* valid expression */ } if "`*'"!="" { qui mat vecaccum `x' = `doit' `*' [`weight'`exp'] /* */ if `doit' `at', `constan' tempname s if "`weight'"=="fweight" { scalar `s' = 1/_result(2) } else scalar `s' = 1/_result(1) mat `x' = `s'*`x' } else { mat `x' = (1) mat colname `x' = _cons } mat rowname `x' = x end program define obs_at version 4.0 local at "`1'" if `at' < 1 | `at' > _N { di in red "obs. no. specified by at() out of range" exit 198 } local x "`2'" local cons "`3'" macro shift 3 local nvars : word count `*' local xdim = `nvars' + `cons' mat `x' = J(1,`xdim',1) local i 1 capture { while `i' <= `nvars' { mat `x'[1,`i'] = ``i''[`at'] local i = `i' + 1 } } if _rc == 504 { di in red "observation specified by at() has missing values" exit 504 } if _rc != 0 { error _rc } if `cons' { mat colnames `x' = `*' _cons } else mat colnames `x' = `*' mat rowname `x' = x end program define order_at version 4.0 local at "`1'" local x "`2'" local cons "`3'" macro shift 3 local nvars : word count `*' local xdim = `nvars' + `cons' mat `x' = J(1,`xdim',1) local i 1 capture { while `i' <= `nvars' { mat `x'[1,`i'] = `at'[1,colnumb(matrix(`at'),"``i''")] local i = `i' + 1 } } if _rc == 504 { di in red "matrix specified by at() does not match indepvars" exit 198 } if _rc != 0 { error _rc } if `cons' { cap mat `x'[1,`xdim'] = `at'[1,colnumb(matrix(`at'),"_cons")] mat colnames `x' = `*' _cons } else mat colnames `x' = `*' mat rowname `x' = x end ---------------------------------cut here---end-of-dlog_at.ado--------- ********************************************************************** dlogit2.hlp Instructions for use: Cut out the following help file and put it in a file called "dlogit2.hlp". Be sure that the file is saved as a plain text (ASCII) file and that it has a hard return at the end of the last line in the file. Put dlogit2.hlp in your C:\ADO directory (you may have to create this directory) or your Stata working directory (i.e., your current directory). Note: Do not put it in the C:\STATA\ADO directory. Only official Stata ado and hlp files should be placed in this directory. ---------------------------------cut here---start-of-dlogit2.hlp------- .h dmlogit2 ---------------------------------cut here---end-of-dlogit2.hlp--------- ********************************************************************** dprobit2.hlp Instructions for use: Cut out the following help file and put it in a file called "dprobit2.hlp". Be sure that the file is saved as a plain text (ASCII) file and that it has a hard return at the end of the last line in the file. Put dprobit2.hlp in your C:\ADO directory (you may have to create this directory) or your Stata working directory (i.e., your current directory). Note: Do not put it in the C:\STATA\ADO directory. Only official Stata ado and hlp files should be placed in this directory. ---------------------------------cut here---start-of-dprobit2.hlp------- .h dmlogit2 ---------------------------------cut here---end-of-dprobit2.hlp--------- ********************************************************************** dmlogit2.hlp Instructions for use: Cut out the following help file and put it in a file called "dmlogit2.hlp". Be sure that the file is saved as a plain text (ASCII) file and that it has a hard return at the end of the last line in the file. Put dmlogit2.hlp in your C:\ADO directory (you may have to create this directory) or your Stata working directory (i.e., your current directory). Note: Do not put it in the C:\STATA\ADO directory. Only official Stata ado and hlp files should be placed in this directory. ---------------------------------cut here---start-of-dmlogit2.hlp------- .- help for ^dlogit2^, ^dprobit2^, ^dmlogit2^ (STB-??: sqv???) .- Marginal effects for logit, probit, and multinomial logit --------------------------------------------------------- ^dlogit2^ depvar [indepvars] [weight] [^if^ exp] [^in^ range] [ ^,^ ^at(^{expression|matname|#}^)^ ^log^ ^logit_options^ ] ^dprobit2^ depvar [indepvars] [weight] [^if^ exp] [^in^ range] [ ^,^ ^at(^{expression|matname|#}^)^ ^log^ ^probit_options^ ] ^dmlogit2^ depvar [indepvars] [weight] [^if^ exp] [^in^ range] [ ^,^ ^at(^{expression|matname|#}^)^ ^log^ ^mlogit_options^ ] ^fweight^s and ^aweight^s are allowed; see help @weights@. These commands share the features of all estimation commands; see help @est@. ^dlogit2^, ^dprobit2^, or ^dmlogit2^ typed without arguments redisplays previous results. To reset problem-size limits, see help @matsize@. Description ----------- ^dlogit2^, ^dprobit2^, and ^dmlogit2^ compute the marginal effects dP/dx from, respectively, logistic regression, probit regression, and multinomial logistic regression; see help for @logit@ and @mlogit@. Note: Stata contains a ^dprobit^ command which treats the dummy variables and the constant term in a different manner; see [5s] logit for details. Options ------- ^at(^{expression|matname|#}^)^ specifies the point x at which the marginal effects are to be computed. If ^at()^ is not specified, the marginal effects are computed at the means of the indepvars. ^at(^expression^)^ can be used to compute the marginal effects at the means for a subgroup. If ^at(^expression^)^ is specified, x is set to the means of the indepvars for those observations in the estimation sample that satisfy "expression". ^at(^matname^)^ sets x to the row vector "matname". The columns of the row vector must be labeled with the names of the indepvars. ^at(^#^)^ sets x to the values of the indepvars for observation number #. This observation does not have to be part of the estimation sample. ^log^ displays the coefficients of the logistic, probit, or multinomial logistic regression (i.e., the output from ^logit^, ^probit^, or ^mlogit^) before displaying the marginal effects. logit_options are any of the options allowed with ^logit^; see help @logit@. probit_options are any of the options allowed with ^probit^; see help @probit@. mlogit_options are any of the options allowed with ^mlogit^; see help @mlogit@. Examples -------- . ^dlogit2 employ educ age race1 race2 sex^ . ^dlogit2^ . ^dprobit2 employ educ age race1 race2 sex, log^ . ^dmlogit2 job educ age race1 race2 sex, basecategory(1)^ To compute the marginal effects at the means for black men: . ^dlogit2 employ educ age race1 race2 sex, at(race1==1 & sex==1)^ . ^dprobit2 employ educ age race1 race2 sex, at(race1==1 & sex==1)^ . ^dmlogit2 job educ age race1 race2 sex, at(race1==1 & sex==1)^ To compute the marginal effects at educ==12, age==40, race1==1, race2==0, and sex==1: . ^matrix x = (12, 40, 1, 0, 1)^ . ^matrix colnames x = educ age race1 race2 sex^ . ^dlogit2 employ educ age race1 race2 sex, at(x)^ . ^dprobit2 employ educ age race1 race2 sex, at(x)^ . ^dmlogit2 job educ age race1 race2 sex, at(x)^ To compute the marginal effects at the values of the indepvars for observation 501: . ^dmlogit2 job educ age race1 race2 sex in 1/500, at(501)^ To compute the marginal effects at the values of the indepvars for the person with id==173: . ^dmlogit2 job educ age race1 race2 sex, at(id==173)^ Author ------ Bill Sribney Stata Corporation 702 University Drive East College Station, TX 77840 Phone: 409-696-4600 800-782-8272 Fax: 409-696-4601 email: tech_support@@stata.com Also see -------- STB: STB-?? sqv??? Manual: [5s] logit, [5s] mlogit On-line: help for @logit@, @mlogit@ ---------------------------------cut here---end-of-dmlogit2.hlp---------