help for flexfitVersion 1.4, 22Nov2017

Title

flexfit -- Compute goodness-of-fit measures after
an estimation using a transformed dependent variable.

Syntax

flexfit
[varname]
[if] [in],
chainlhs(string)
[
depvar(newvarname)
lambda(real 0)
double
replace
*
]

Options

varname Name of an unwound version of depvar(varname)
to be created by flexfit
and added to the existing data. Not required.

chainlhs(string) Specifies the transformation of the dependent variable.
Allowable transformation options are ln, log10, boxcox (or bc)
and sqrt. A required option.

depvar(varname) The name of the transformed dependent variable used in the preceding
estimation command. When not specified, flexfit attempts to recover the name of the dependent
variable from the -e(depvar)- macro. This variable must already exist in the data in memory.

lambda(real 0) Specifies the value of lambda for the -boxcox- transformation.

double Computations performed in double precision.

replace If depvar(varname) already exists, replace it.

* Any additional options are passed through to the predict command.

Description

Standard measures of goodness-of-fit such as the R^2 compare
the actual values of a dependent variable to the predicted values from the
estimation. Suppose the dependent variable has been transformed prior to estimation,
so that the predicted values are of, for exmple, the natural logarithm (ln)
of the dependent variable.
In this situation, flexfit computes the inverse of the transformation
(i.e. "unwinds" the transformation) and compares the "unwound" predicted values
to the "unwound" actual values.

Remarks

When fitting a regression with a skewed dependent variable
such as the observed total cost of production in a sample of firms,
it is frequently both useful and plausible to apply a convex transformation
to the dependent variable prior to estimation.
The suite of programs in the -flexcost- package
(e.g. flexmake, flexconstraints, etc.)
are designed to fit total cost data that has been transformed by
any of the transformations: ln, log10, sqrt and boxcox.

Example

. preserve

. sysuse auto, clear
(1978 Automobile Data)

. regress price displacement weight turn i.foreign

Source | SS df MS Number of obs = 74

+
F(4, 69) = 20.66
Model | 346122759 4 86530689.8 Prob > F = 0.0000
Residual | 288942637 69 4187574.45 R-squared = 0.5450

+
Adj R-squared = 0.5186
Total | 635065396 73 8699525.97 Root MSE = 2046.4

+

--------

price | Coef. Std. Err. t P>|t| [95% Con f. Interval]

+

--------

displacement | 9.894825 6.009805 1.65 0.104 -2.094408 21.88406
weight | 3.306988 .8480406 3.90 0.000 1.615193 4.998782
turn | -223.6961 110.8203 -2.02 0.047 -444.7765 -2.615644
|
foreign |
Foreign | 3485.589 695.2744 5.01 0.000 2098.554 4872.623
_cons | 2060.711 3335.541 0.62 0.539 -4593.511 8714.933

+

--------

. est store price

. gen ln_price = ln(price)

. regress ln_price displacement weight turn i.foreign

Source | SS df MS Number of obs = 74

+
F(4, 69) = 24.44
Model | 6.57936376 4 1.64484094 Prob > F = 0.0000
Residual | 4.64416932 69 .067306802 R-squared = 0.5862

+
Adj R-squared = 0.5622
Total | 11.2235331 73 .153747029 Root MSE = .25944

+

--------

ln_price | Coef. Std. Err. t P>|t| [95% Con f. Interval]

+

--------

displacement | .0011256 .0007619 1.48 0.144 -.0003944 .0026456
weight | .0004702 .0001075 4.37 0.000 .0002557 .0006847
turn | -.0281069 .0140497 -2.00 0.049 -.0561353 -.0000785
|
foreign |
Foreign | .5132284 .0881464 5.82 0.000 .3373812 .6890756
_cons | 7.960573 .4228774 18.82 0.000 7.116956 8.804191

+

--------

. flexfit , chainlhs(ln)

Over 74 observations, the correlation between the predicted
and actual dependent variable is: 0.7656
and its squared value , -r2_hat-, is: 0.5862

Over 74 observations, the root mean squared deviation
of the unwound predicted from the unwound actual dependent variable is: 1,853.4847

Over 74 observations, the correlation between the unwound predicted
and the unwound actual dependent variable, -flex_r-, is: 0.8044
and its squared value, -flex_r2-, is: 0.6470

. est store ln_price

. est table price ln_price, stat(N r2 rmse flex_r2 flex_rmsd)

-+-
---
--
Variable | price ln_price

-+-
---
--
displacement | 9.8948245 .00112559
weight | 3.3069876 .00047022
turn | -223.69608 -.0281069
|
foreign |
Foreign | 3485.5889 .51322842
|
_cons | 2060.711 7.9605733

-+-
---
--
N | 74 74
r2 | .54501908 .58621146
rmse | 2046.3564 .25943554
flex_r2 | .64703046
flex_rmsd | 1853.4847

-+-
---
--

.

. restore

Authors

Mead Over,
Center for Global Development, Washington, DC 20036 USA. Email:
MOver@CGDev.Org if you observe any
problems.

Reference

Book . . . . . . . . . . . . . . . . . . . Health Econometrics Using Stata
. . . . . . . . . Partha Deb, Edward C. Norton, and Willard G. Manning
http://www.stata-press.com/books/health-econometrics-using-stata

program define flexfit, eclass
*!	Version 1.4	22Nov2017 by Mead Over
*	This program is a post-estimation command for Stata estimation programs.
*	The program takes as a required option -chainlhs(string) which specifies
*	one of four eligible monotonic transformations that were applied to the
*	dependent variable prior to estimation.

*	Then this program:
* a.	Computes the predicted values over the estimation sample
* from the estimated equation
* (1)	Computes the squared correlation of -depvar- and -dephat- as e(r2_hat)
* b.	Passes the predicted values through the inverse of the
* transformation function
* c.	Computes the square root of the mean of the sum of squared deviations
* between the actual values of the dependent variable and
* the unwound predicted values and appends it to the -ereturn-
* space as the scaler
* -e(flex_rmsd)-
* d.	Computes the correlation coefficient between the actual and
* predicted values of the untransformed dependent variable
* and its square and adds them to the ereturn space as scalars:
* -e(flex_r)-
* -e(flex_r2)-
* e.	The -atspec- option is not yet implemented.
*	Any other options are passed through to the predict command

syntax [name] [if] [in], chainlhs(string) [ lambda(real 0) ///
double replace depvar(varname) /* atspec(string) */ * ]

*	Version 1.0	13Feb2017 by Mead Over
*	Version 1.1	17Feb2017 the macro e(depvar) contains multiple dependent variables,
* choose the first one.
*	Version 1.2	20Feb2017 Fix bug in above change.
*	Version 1.3	21Oct2017 Add output on the squared correlation between -depvar- and -dephat-: e(r2_hat)
* Same as r2 for -regress-, but not computed by many other estimators
*	Version 1.4 22Nov2017 Small edits and bug fixes.
*	Ideas for the future:
* Allow -atspec- so that predicted value is for specified values
* of some right-hand-side variables. Perhaps replace -predict- with
* -margins-, at() with asobserved on all but the named variables,
* which is set to the -at- specific values.
* Adapt to perform same calculations on an equation other than the first
* of a multi-equation system estimator like -sureg-.