
stata中极大似然估计方法 评分:
stata中极大似然估计方法，很不错的资源，可以下载下来学习下。
3. ml check*: This command checks the program syntax for mistakes. while optional, it is cxtrcmcly uscful for debugging Mle routines. Beginnin programmers arc advised to usc this command starting valucs for thc numcrical optimization algorithm earch for better 4. ml search": This optional command causes Stata to 5. ml maximize: This command starts thc exccution of thc estimation com mands and generates the output 6. ml graph*: This is an optional command that produces a graph showin the iteration path of the numcrical optimization algorithm. I recommend using this command so that one can monitor convergence. 3.1 Program Instructions In most cases, writing and mle program requires only a couple of lines of syntax. At least, this is the case if (1)the log li kelihood functio on meets linear form restrictioni. e, the observations are independentand(2) Stata derives the first and second (partial) derivatives numerically (these derivatives are needed for the NewtonRaphson algorithm). In this document, I assume that these two conditions are met The program is startcd by entering program define name where name is any name up to 32 characters in length.(It is preferable to choose a descriptive name. The user may abbreviate program define to pr de. To end the program, one should type In between these key words, the user has to declare the parameters and the loglikelihood funct First, the log likelihood function and its parameter have to be labeled. This is done through the command args (which is an abbreviation for the computer term "arguments?"). Next, the loglikelihood function has to be defined; this is done using the quietly replace command.3 In addition to these specifications, it is often useful to declare the program versiOn, especially if you are plalllling to Illake changes to the prograln over 2That is, the maximization method is lf(see the discussion of ml model below).Please note that if is the easiest approach in Stata but not always the Ilost accurate. How 已ver my programming experience i have never encountered an instance in which the results from lf wcrc mislcading 3Replace' indicates that the user is substituting a new expression. "Quietly"implies that Stata does not echo this substitutioni.e, it is not displayed on the screen or in the output Example 1: To show the use of these commands, consider the simple example of the poisson distribution f(yu) e Here u is the parameter that we want to estimate. For a sample of n independent observa tions, t his distribution produces the following loglikelihood function l(ul31 To program this function, we could use the following syntax: program define poisson version 1.0 args lnf mu quietly replace lnf,=$MLy1*ln(mu)'mu'Infact(SMLy1) end Let us analyze what this program does. In the first line we define the pro gram, calling it poisson. In the second line, we show the version of the program (version 1.0). The third line provides a name for the loglikelihood function (lnf)inld its one parameter(mu). The fourth line specifies the loglikelihood function alld the fifth line ends the progralll The action, of course, is in the fourth line. This line is based on the argu mcnts spccificd in args. Bccausc we arc referring to arguments, thcy should be placed in apostrophes. (In fact, the leading apostrophe is backward leanin and is typically located on the same key as the tilde; the second apostrophe is straight and is typically located on the same key as the double apostrophe. The fourth line also contains the variable $MLy1, which is the internal label for the (first) dependent variable. Stata will replace this with all appropriate variable from the data set after the ml model command has been specified Finally, the fourth line specifies a function.(The last term in this expression Infact(SML_y1), sta A careful inspection of the fourth line of code shows that it looks a lot like the loglikelihood function, except that it, does not include summations. In fact this line gives the loglikelihood function for a single observation 7(41y2)=yln()41n(2) As long as the observations are independent (i.e., the linear form restriction on the loglikelihood function is met), this is all you have to specify. Stata knows that it should cvaluatc this function for cach obscrvation in the data and then sum the results. This greatly simplifies programming loglikelihood functions.5 A By not referring to a spccific variable namc, thc program can bc usod for any data sct This is quite useful, as you do not have to go back into the program to change variable names Ke Inlind, however, that this will only work if the observations are independent and the linear form restriction is met Example 2: As a second example, consider the normal probability density function ∫(y.a2) o(a) where z o and y一 denotes the standard Nornal distribution. Iilagine that we draw a salnlple of n independent observations froIn the IlorIllal distrib ution, then the loglikelihood function is given by l(p,o21y13,y2…yn) )+∑l(a) We can program this function using the following syntax program define normal version 1.0 quietly replace 'lnf=ln (normd((SMl y1'mu')/sigma) end Here norm is()and(綁MLy1mu’)/ sIga’isx;, Again, we ouly have Igma> to specify the loglikelihood function for a single observation. Stata will evalu atc this function for all obscrvations and accumulate thc results to obtain tho overall loglikelihood 6 To derive the second line of this equation, we proceed as follows. First, we substitute z in the formula for the morinal distributionl f(you,a) Next, we compare this result to the standard normal distribution o(z) We see that the generic normal distribution is almost identical to the standard normal distri bution: t he only difference is a term in Factoring this term out, we get f(ylu,o) o(a) d(z) 3.2 MI Model To apply a program to a data set, you need to issue the ml model command This command also controls t he met hod of maximization that is used. but i will assume that this method is 1fie. the linear form restrictions hold and the derivatives are obtained numerically The syntax for ml model is ml model lf name equations if, options Ilere ml model may be abbreviated to ml mod, name is the name of the Mle program(e.g, poisson, and equations specifies the model that should be esti inlate ough the progralll. A subset of the data illay be selected through the if statement. It is also possible to specify various options, as will be discussed elow 3.2.1 Equations To perform MlE, Stata needs to know the model that you want to estimate That is, it needs to know the dependent and, if relevant, the predictor variables These variables are declared by specifying one ore more equations. The user call specify these equations before running ml model by using all alias. It is also possible to specify the equations in the ml model command. placing each equation in parentheses. The gencral rulc in Stata is that a scparatc cquation is spccificd for cach mcan model and for each (co)variance model. For example, if we wanted to estimate the mean and variance of a normal distribution. we would need an equation for the mean and an equation for the variance. In a linear regression model,we would need an equation for the conditional mean of y (i.e, Eyilx b) and for the variance(the latter Inodel would include only a constant, unless we specify a heteroskedastic regression model). In a simultaneous equations model. there would be as many equations as endogenous variables, plus additional equations to specify the covariance structurc Aliasing. Wc can specify cquations bcforc thc ml model command, giving distribution we could specify the following equative. r example, for the Poisson them an alias that can be used in the command. Fo mean: y= The name if is an abbreviation for linear form if the linear form rest rictions dlo not hold, then the user may choose from three other maximization methods: do, 1, and d2 The diffcrcncc bctwccn thosc mcthods lics in the way in which thc first and sccond (partial) derivatives are obtained. Both the first and second derivative are obtained numerically with do. With d1, the user has to derive the first derivative analyticaly (i.e, through calculus) while the second derivative is obtained numerically. With d2, both derivatives are obtained analytically; this is gencrally thc most accuratc approach. You should notc that the usc of do d1, and d2 necessitates additional lines of code in the program to specify the loglikelihood function more completely and, if necessary, to specify the first and second derivatives(see ould and Sribney 1999: Gould, Pitblado, and Sril We have now generated an equation by the name or alias of"mean"that specifies the modcl for u in thc Poisson distribution. The cquation specifics the dependent variable on thc lefthand sidc this is thc namc of a variable in the data sct The righthand side is empty because there are no predictors of p(other tha the constant).8 To estimate this model we type ml model lf poisson mean Here lf is the maximization method. poisson is the name of the maximum likelihood program, and mean is the alias for the equation specifying the mean modcl. The alias will appear in the output and can makc it casicr to rcad Dircct Spccification. Wc can also specify the cquation directly in the m1 model command. For the poisson distribution we would do this as follows ml model lf poisson (y=) Since we have not named the equation, it will be labeled in the output as eq followed by a munber 3.2.2 Additional Examples Example 1: Earlier we wrote a program, titled normal, to estimate the para mctcrs of a normal density function. Imagine we want to apply this to a variable named Y. Then we need the following specification of the ml model command ml model lf normal (Y=)(Y=) Notice that we now have specified two equations. The first equation calls for the estimation of u. The second equation calls for the estination of o Example 2: Now imagine that the nornal density describes the conditional distribution of y given two predictors, X and Z. We assume that the conditional variance of y is constant and given by o. We also assume that the conditional mcan of y is given by Bo t 61X +62Z. In other words, we arc considcring the classical linear regression model under the assumption of normality. To estimate this model, one should issue the following command ml model lf normal (Y=X Z)(Y=) The first equatiOn calls for the estination of the conditional mean of Y. which is a function of the predictors X and Z. The second equation pertains to the estimation of o, which is constant so that no predictors arc spccificd Onc sccs that the estimation of a normal regression modcl rcquires no ad ditional programming compared to the estimation of the mean and variance of 8If there are predictor variables, these should be specified after the equal sign g The direct and aliasing Inethods Imlay be combined. For details see Gould and Sribney (1999) and Gould, Pitblado, and Sribney(2003) a normal distribution. This minimizes the burden of programming and gives MLE routines a grcat dcal of"portability. Both of thesc fcaturcs arc important bencfits of stat a 3.2.3 Options There are two options that can be specified with ml model, both of which produce robust variance estimates (or HuberWhite or sandwhich estimates (1) robust generates heteroskedasticitycorrected standard errors (This may be abbreviated as rob. (2 )cluster (varname) generates clustercorrected standard errors where var'nuTle is the nane of the clustering variable. (This may be abreviated as cl) Both of these commands may be specified with the lf maximization method.I0 a discussion of robust variance estimation can be found in Gould and Sribney (1999), Gould, Pitblado, and Sribney(2003), and in advanced econometric textbooks(Davidson and MacKinnon 1993; Grccnc 2000 3.3 Ml Check It is useful to check an MlE program for errors. In Stata, you can do this by issuing the command ml check. This command evaluates if the program can compute the loglikelihood function and its first, and second derivatives. If there is a problem with th or with its derivatives. m1 check will let the user know. Stata will not be able to estimate the model before these problems are Fixed 3.4 MI Search The NewtoIlRaphison algorithIn needs all initial guess of the parameter esti mates to begin the iterations. These initial guesses are the socalled starting values. In Stata, thc uscr has two options: (1) usc a default proccdurc for starting values or(2)do a more extensive search The default procedure in Stata is to set the initial values to 0. If the log likelihood function cannot be evaluated for this choice of starting values. then Stata uses a pseudorandom number generator to obtain the starting values. (It will regenerate numbers until the loglikelihood functiOn call be evaluated. This procedure is a quickanddirty way to start the NewtonRaphson algorithm Through ml search(which may be abbreviated as ml sea) the selection of starting valucs can be improved. The ml search command searches for starting valucs bascd on cquations. A nicc fcaturc hcrc is that the uscr can specify boundaries on the starting values. For example, before estimating th Poisson distribution, we could specif 10 However, other InaxiInization tmethods Ilay not allow these options or Inlay require ad program. m1 search 1 3 This causes the search command to pick starting values for u that lie between 1 and 3. If the ML estimate lies within these bounds, beginning the iterations there can speed up estimation considerably. Thus, I recommend using the ml search command (cven if you do not specify bounds), although it can be by passed 3.5 MI maximize None of the commands discussed so far actually causes Stata to generate a table of parameter estimates. To do this, the mLe program has to be executed and this is done through the ml maximize command. You simply type ml maximize options which may be abbreviated to ml max) and the estimation process commences at least, when the program is correct and there are no", ings in the dat It is possible to add several options to the ml maximize colllnand that coll trol the output and convergence. In general, I recommend against tinkering with these features, but for the sake of completeness I will list the most important options (1)nolog suppresses the iteration log. This reduces the length of the output, in particular if convcrgcncc problcms arc cncountcred (see below). Since the iteration log contains important information about convergence, one should not suppress it too quickly (2)iterate(##)sets the maximum number of iterations(##). The default value is 16000. Since this default largeit could take hours to reach this liinitthere is usually Io reason to change it (3) Tolerance(##)sets the e por at which Stata cuts off the iteratie Specificall declared whe 1l1 tole where li is the loglikelihood fund for the set of estimates erated in the jth iteration and lj1 is the loglikelihood function for the set of estimate The default of tolerance is le7. By increasing this value, convergence can be pccded. Howcvcr, this is a risky approach bccausc the resulting cstimates may not be truc maximum likelihood estimates. Hencc the decision to change tolerance should not be made lightly (4)difficult forces Stata. to put in extra effort to estimate dif ficult loglikelihood functions. Such functions tend to have man ridges, fat areas. anld saddle points so that they are not concave 9 This makes it pute the direction vector, which is uscd to update cstimates. The difficult option prompts Stata to dctcrminc if the direction vcctor exists. If not, then the pro Raphson algorithm with a on the steepest ascent. met hod to obtain new estimates (see gould and Sribney 1999: Gould, Pitblado, and Sribney 2003). Specifying difficult increases estiMation time, so I do not suggest using it by default. However, if Stata generates many warnings about non concavity, especially on later iterations, it may be worthwhile to repeat estimation using this option. 3.6 Monitoring Convergence and MI grap Even when the prograln produces output, it is useful to check the convergence of the NewtonRaphson algorithm. One should pay attention to three pieces of information. First, the algorithm should converge relatively quickly. a dozen or so itcrations would generally not worry us, especially not for complcx estimation problems. But if the algorithm requires a large number of iterations(e. g, in the hundreds), then this could indicate a serious problem econd, one should pay attention to warning messages about the concavity of the loglikelihood functiOn. The lnlessage not concave. which follows the value of the loglikelihood function in the iteration log indicates that the loglikelihood function is essentially flat at a particular iteration. This means that stata cannot cstablish a direction vcctor to guidc ncw parameter cstimates. if this warning message occurs early in the iteration process, it can be safely ignored However, if this message appears on the last iteration, then this indicates a serious problem. Or trust the parameter estimates in this case and should consider reestimating the model with the difficult option(see above) Third, one should Monitor the convergence path. If we place the iteratiOns oIl the horizontal axis and the corresponding values of l on the vertical axis then we should ideally see a concave(marginally declining) function. That is. initially the changes in the log likelihood function should bc large bctwccn itcrations However, nearing the end of the iterations, these changes should be relatively tiny. Small perturbations of this pattern need not worry us. llowever, if the iteration function is convex(or a straight line), then we should be worried. This could indicate a bug in the program or an illbehaved loglikelihood function Plotting the loglikelihood against the iterations in Stata is easy All one has to do is to issue the command mI grap which may be abbreviated as ml gr)aftcr running ml maximize. I recommend that you always crcatc this plot, as it reveals a lot about convcrgence 4 Output After running a mle program, Stata will produce the following output 1020180610 上传 大小：202KB
评论 下载该资源后可以进行评论 共1条

20190618