The package is intended to compute marginal effects and elasticities after any native or user specified model that can be described as: (i) E(y) = f(x,beta) where beta is a vector of regression parameters, and the model yields estimates of beta and vcov(beta); (ii) E(y) = f(x,beta,gamma) where gamma is a vector of additional parameters, e.g. $sigma for tobit, and the model yields estimates of beta, gamma and the overall covariance matrix: vcov(beta|gamma) The effects can be computed at any user-specified values of regressors. Standard errors are always accessible. Also, an independent variable could enter in several forms in a regression, say x1, x1^2, x1*x2 etc. This package allows to compute effects with respect to both functionally independent variables as well as the different transformation terms. General note: Never manually construct a constant regressor term, always use gretl's standard "const" variable for that. -------------------------------------- DESCRIPTIONS OF FUNCTIONS The public functions for computing effects and predictions are: * margeff() * elast() * predict_y() * predict2_y() The first two are complemented by printing functions: * print_margeff() * print_elast() Also there are some input helper functions: * make_formula() * make_formula0() [deprecated] * make_formula1 * dummylist() margeff ======== Computes marginal effects. Arguments: - string s "model" (optional) - string link "link function" (optional) - matrix v "x-values to calc. at" - matrix b "model coefficients" - matrix vcv "coeff. covariance matrix" - string nlspars "parameters; after nls only" (optional) - matrix dumlist (optional) Returns bundle; contains marginal effects, their s.e., covariance matrix, and several string fields which are mostly input for the printing functions The "model" argument 's': In simple cases omitting s is allowed and is equivalent to passing an empty string "", but then the model variables will be auto-named as v1 and so forth. If it happens that you simply want to find the marginal effects w.r.t. the RHS terms of the regression equation (i.e., not the underlying functionally independent variables), you can simply pass that RHS representation like: s = flatten(varnames($xlist), 1) The 'dumlist' argument: This tells the function which of the relevant variables is a dummy. If for example you have a list X = x1 x2 x3, of which x2 is a dummy, you can either set the dumlist argument manually as dumlist = {0,1,0}, or you can use the input helper function dummylist (see below): dumlist = dummylist(X) If there are no dummies, the argument can be omitted (which internally translates into dumlist = {0}). Cases OLS, WLS, HSK, TSLS etc. ------------------------------ The "model" string argument is similar to a gretl style xlist, but there can also be expressions, for example string s = "0 x1 x1^2 log(x1) x1*x2 exp(x1)" (In this example it is clear that only two different variables appear, and so the v argument of input values would have to have two elements.) After linear models with E(y) = x'b, e.g. after ols/wls/hsk/tsls etc., the link input argument can be omitted (null) or set to link = "" or link = "@". Case NLS --------- Consider for example the following nls estimation (after suitable initialization of the parameters a, b, g): nls C = a + b * Y^g params a b g end nls --vcv Here the "link function" argument must be set to: string link = "nls" in combination with the "model" input being simply the right hand side of the equation as a string: string s = "a + b * Y^g" The nlspars string argument must contain the scalar parameters, separated by spaces: string nlspars = "a b g" (The argument nlspars is ignored for other model types and can be omitted there.) Other cases ------------ After models of the form E(y) = f(x'b) you should specify the function with the pattern link = "f(@)", always indicating the linear combination x'b with the special literal character "@". Some examples: + link = "exp(@)" e.g. after poisson + link = "exp(@) / (1+exp(@)" e.g. after logit or logistic + link = "@^-1" print_margeff ============== Prints the results of margeff. Returns: nothing Arguments: - bundle pointer *b "marg. effects s.e." elast ====== Returns bundle; contains elasticities, their s.e., covariance matrix, and several string fields which are mostly input for the associated printing function. Arguments: - string s "model" (optional) - string link "link function" (optional) - matrix v "x-values to calc. at" - matrix b "model coefficients" - matrix vcv "coeff. covariance matrix" - string nlspars "parameters; after nls only" (optional) - matrix dumlist (optional) For the usage of the function arguments see the margeff function. print_elast ============ Prints the results of elast. Returns nothing. Argument: - bundle pointer *b "marg. effects, s.e." make_formula ============= In case of large models, retyping the formula may be very tedious, the make_formula function is intended to automatize the procedure in most cases. Returns string. Arguments: - string s "names of model xlist" - bool defaultfun[1] "make/not def. subst" (optional, default TRUE) - string userfun "user-spec. fun subst" (optional) - string userdiv "division subst" (optional) - string undscd "_ subst in names" (optional) - bool crossterms[1] "subst/no _ for *" (optional, default TRUE) A typical value for the first argument is varname($xlist). The transformation is divided into 5 steps: 1) Revert (?) gretl default prefixes for functions (l_ for log, exp_ for exp, sq_ for ^2). For example, the following: make_formula("LRM l_LRM exp_LRY sq_LRY",,,,, 0) returns: "LRM log(LRM) exp(LRY) LRY^2" 2) Consider user-specified prefixes for functions, for example: make_formula("cos_LRM pow_2_5_LRM", 0, "cos_=cos pow_2_5_=2.5",,, 0) returns: "cos(LRM) LRM^2.5" 3) Ratios of variables. If you have ratios and name them x1_d_x2, x1_d_x3 and so on, you just tell the function userdiv = "_d_". Example: make_formula("x1_d_x2 l_x1 pow_0_5_x1", 1, "pow_0_5_=0.5", "_d_",, 0) returns: "x1/x2 log(x1) x1^0.5" 4) Furthermore, if you have variable names with the underscore "_" character which don't reflect any functions, say x_1, x_2, you can set undscd = "x1_1 x_2". Thus: make_formula("x1_d_x2 l_x1 pow_0_5_x1 x_1", 1, "pow_0_5_=0.5", "_d_",\ "x_1", 0) returns: "x1/x2 log(x1) x1^0.5 xundscd1" So, x_1 is relabeled as xundscd1. In the context of this package it is advisable to not have underscores without functional meaning, see the next point. 5) Finally, after the other appearances of "_" in names are removed, setting crossterms = 1 replaces all "_" characters by "*". (Sometimes you might need to run the function first with crossterms = 0 and make some editing adjustments by hand.) See also the sample script. make_formula0 ============== Just a shortcut for: make_formula(s). [FIXME: Redundant now with the defaults in make_formula.] Returns string. Argument: - string s "names of model xlist" make_formula1 ============== Similar to make_formula(s), but instead of a constructed input string s with the names of the relevant regressors it takes a list and automatically uses the variable names from that. Typically you would simply pass $xlist to this function. Returns string. Argument: - list xlist "model xlist" dummylist ========== Returns matrix; a vector with 0/1 elements, namely 1 if the corresponding variable from the input list is a dummy variable. The input list should not include the const variable, and should consist only of functionally independent variables from $xlist (i.e. not be transformations of one another). Argument: - list XL predict_y ========== Should not be seen as an alternative to gretl's fcast command, but it is rather analogous to the tobit_y package by Allin Cottrell. Returns matrix. Arguments: - string s "model" - string link "link function" - matrix newx "x-values to calc. at" - matrix b "model coefficients" - matrix vcv "coeff. covariance matrix" - string nlspars "parameters; after nls only" (optional) - scalar cl[NA] "confidence level NA=.95" (optional) - scalar sgma[NA] "s.e. of errors" (optional) - scalar n[NA] "obs n. for estimation" (optional) For the usage of the first arguments, see the margeff function. When sgma is supplied (normally by passing $stderr), confidence intervals for y are computed, otherwise intervals for the conditional mean. A typical input for n is $T. Suppose we have estimated: ols y 0 x1 x2 Then, for example, one could pass: newx = {2.34,3.5} for a single point forecast, or: newx = {2.34,3.5; 2.8,3.9} for two points, and so forth. See also the sample script. predict2_y =========== Returns matrix. Arguments: - string s "model" - string link "link function" - matrix newx "x-values to calc. at" - matrix b "model coefficients, better if named" - matrix vcv "coeff. covariance matrix" - scalar sgma[NA] "s.e. of errors" - scalar df[NA] "model df" - scalar cl[NA] "confidence level NA=.95" - string nlspars[null] "parameters; after nls only" A version with somewhat different syntax. Here the special code "exp(@@)" for the link argument is recognized, which means that the dependent variable is log(y). Suitable corrections as described in the POE-4 book, ch.4, for regressions with log(y) as the dependent variable are applied. Here only calculations for a single input point are possible. Example (datasets from POE-4 required): open stockton4.gdt ols log(sprice) 0 livarea square(livarea) age square(age) beds fla = make_formula1($xlist) newx = {20,10,3} cnameset(newx, "livarea age beds") eval predict2_y(fla, "exp(@)", newx, $coeff, $vcv, $sigma, $df) eval predict2_y(fla, "exp(@@)", newx, $coeff, $vcv, $sigma, $df) --------- Changelog (only after version 0.6) v0.7, February 2021 (the gretl team): fix outdated syntax (isnull, sprintf, row/colnames), edit help text, possibly fix typo bug in make_formula; deprecate make_formula0 which is now redundant;