[原创]stata存储变量、输出变量

这里只有作者精心编写的研究经历!
回复
头像
hellohappy
网站管理员
网站管理员
帖子: 282
注册时间: 2018年11月18日, 14:27
Been thanked: 2 time

#1 [原创]stata存储变量、输出变量

未读文章 hellohappy » 2019年4月13日, 07:45

前言:
    如果我们需要某个命令的某个输出结果,但是在stata的这条命令内部,并没有返回我们要的内容,怎么办?这时可能要用到这条命令 ereturn。
    注意,这个命令只能用于返回类型为e类型的命令,同理,别的类型,如r类型,返回内容是r()相关命令为return;s类型返回s(),相关命令为sreturn。具体程序类型,请自行help program

入门:

    我以 regress 命令举例(这个就是最基础的ols回归命令reg),说明这里是什么意思。
    首先,查看 reg 命令的帮助,然后我们看到这个东西。
reg命令的help.png

    这里面的变量都是被reg命令存储下来方便我们调用的。当我们回归完一个命令以后,直接 display (简写就是di)命令,就可以输出或者调用它。
输出其中的一个变量.png
输出其中的一个变量.png (852 Bytes) 查看 300 次
输出其中的一个变量.png
输出其中的一个变量.png (852 Bytes) 查看 300 次

    那么,如果你要的变量,或者输出的结果中,并没有存储这些东西,怎么办?

    当然是,自己改写内部命令来保存它啦。但这里有一定的难度,因为你必须能简单的读懂命令,才能修改。命令一般保存在c盘的ado文件夹下的plus下的字母开头文件夹里面。

命令介绍:

    ereturn 一般用于内部命令保存变量,以方便输出调用,可以存储标量、矩阵等等。具体help文件后文附上。
    这个命令最常用的应该是: ereturn scalar name = exp
    这个是存储一个标量name,表达式exp的运算结果。
    另外两个常用的是:ereturn local name ...  和 ereturn matrix name [=] matname [, copy]
    分别用于存储的宏(也就是暂元)和矩阵。
    下面给出其help命令:
help ereturn
Show

Title

    [P] ereturn -- Post the estimation results

Syntax

    Set macro returned by estimation command
        ereturn local name ...                                                                                                                                            (see [P] macro)  

    Set scalar returned by estimation command
        ereturn scalar name = exp

    Set matrix returned by estimation command
        ereturn matrix name [=] matname [, copy]

    Clear e() stored results
        ereturn clear

    List e() stored results
        ereturn list [, all]

    Store coefficient vector and variance-covariance matrix into e()
        ereturn post [b [V [Cns]]] [weight] [, depname(string) obs(#) dof(#) esample(varname) properties(string)]

    Change coefficient vector and variance-covariance matrix
        ereturn repost [b = b] [V = V] [Cns = Cns] [weight] [, esample(varname) properties(string) rename]

    Display coefficient table
        ereturn display [, eform(string) first neq(#) plus level(#) display_options]

    where name is the name of the macro, scalar, or matrix that will be returned in e(name) by the estimation program; matname is the name of an existing matrix; b is a 1 x p coefficient vector (matrix); V is a p x p covariance matrix; and Cns is a c x (p+1) constraint matrix.

    fweights, aweights, iweights, and pweights are allowed; see weight.

Description

    ereturn local, ereturn scalar, and ereturn matrix set e() macros, scalars, and matrices other than b, V, and Cns returned by estimation commands.  See [R] return for more discussion on returning results.

    ereturn clear clears the e() stored results.

    ereturn list lists the names and values of the macros and scalars stored in e(), and the names and sizes of the matrices stored in e() by the last estimation command.

    ereturn post clears all existing e-class results and stores the coefficient vector (b), variance-covariance matrix (V), and constraint matrix (Cns) in Stata's system areas, making available all the postestimation features described in postest.  b, V, and Cns are optional for ereturn post; some commands (such as factor) do not have a b, V, or Cns but do set the estimation sample, e(sample), and properties, e(properties).  You must use ereturn post before setting other e() macros, scalars, and matrices.

    ereturn repost changes the b, V, or Cns matrix (allowed only after estimation commands that posted their results with ereturn post) or changes the declared estimation sample or e(properties).  The specified matrices cease to exist after post or repost; they are moved into Stata's system areas.  The resulting b, V, and Cns matrices in Stata's system areas can be retrieved by reference to e(b), e(V), and e(Cns).  ereturn post and repost deal with only the coefficient and variance-covariance matrices, whereas ereturn matrix is used to store other matrices associated with the estimation command.

    ereturn display displays or redisplays the coefficient table corresponding to results that have been previously posted using ereturn post or repost.

    For a discussion of posting results with constraint matrices (Cns in the syntax diagram above), see [P] makecns, but only after reading this entry.


Link:
Hide post links
Show post links


回复