Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: How to add Q stats from -wntestq- to -esttab- table (hopefully with -estadd-)

 From Richard Herron <[email protected]> To [email protected] Subject Re: st: How to add Q stats from -wntestq- to -esttab- table (hopefully with -estadd-) Date Fri, 16 Sep 2011 15:51:16 -0400

```@Stas -- Thanks for the pointers! I used -estadd- because I wanted to
quickly estimate the models and generate the table, although after
each estimation I added several lines to find Q, which made it very
clunky.

I learned enough Stata programming to write my own -estadd- function to add Q.

* ----- begin code -----
* my -estadd- function to calculate Box-Ljung Q
tempname residuals
predict `residuals', residuals
wntestq `residuals'
ereturn scalar Q = r(stat)
end

* get data
webuse friedman2, clear
generate ln_m1 = ln(m1)

* make some models
eststo clear
eststo: quietly arima DS4.ln_m1, ar(1) ma(2)
eststo: quietly arima DS4.ln_m1, ar(1) ma(1/2)
eststo: quietly arima DS4.ln_m1, ar(1/2) ma(2)
eststo: quietly arima DS4.ln_m1, ar(1/2) ma(1/2)

* and create tables with -esttab-
esttab, stats(aic bic Q) noobslast nomtitles
* ----- end code -----

It works! Any other suggestions? Thanks!

On Fri, Sep 16, 2011 at 10:06, Stas Kolenikov <[email protected]> wrote:
> I don't see much advantage of -eststo- over the official -estimates
> store-, with which you may not be familiar. -estimates- are as close
> as Stata gets to R objects, with -e(whatever)-
> being essentially results\$whatever. -estadd-, on the other hand, is
> immensely helpful. In your R to Stata work, you would also find that
> you would need to declare temporary variables explicitly with
> -tempvar-, and use local macro names for these. (It actually provides
> a better locality control than R objects that can seep through the
> program interfaces and produce unpredictable results. In Stata,
> variables in the data set are ALWAYS global, and the macros in a
> program are ALWAYS local.) Hence your programmatic solution would look
> like
>
> program define arima_plus_wntestq
>  version 11
>  syntax varlist , estname( name ) [ * ]
>  quietly {
>    arima `varlist', `options'
>    tempvar res
>    predict `res', residuals
>    wntestq `res'
>    estadd scalar Q = r(stat)
>    estadd scalar df_Q = r(df)
>    estadd scalar p_Q = r(p)
>    estimates store `estname'
>  }
> end // of arima_plus_wntestq
>
> Another thing about R to Stata change is that you can rely on the
> official Stata commands doing good job with backwards compatibility,
> but you should rarely expect such of the user-written command. In
> other words, in your own programs, you would want to use as little
> user-written stuff as needed for them to work. (There is no built-in
> mechanisms to resolve dependencies, but you as the author of the
> program may build rudimentary tools for that. More typically, I put
> whatever -ssc install- packages I need at the top of my do-file,
> rather than in ado-files and programs themselves.)
>
> On Fri, Sep 16, 2011 at 8:39 AM, Richard Herron
> <[email protected]> wrote:
>> I found a hackish solution.
>>
>> * ----- begin code -----
>> webuse friedman2, clear
>> generate ln_m1 = ln(m1)
>> eststo clear
>> eststo: quietly arima DS4.ln_m1, ar(1) ma(2)
>> quietly predict res_1, residuals
>> quietly wntestq res_1
>> estadd scalar Q = r(stat)
>>
>> eststo: quietly arima DS4.ln_m1, ar(1) ma(1/2)
>> quietly predict res_2, residuals
>> quietly wntestq res_2
>> estadd scalar Q = r(stat)
>>
>> eststo: quietly arima DS4.ln_m1, ar(1/2) ma(2)
>> quietly predict res_3, residuals
>> quietly wntestq res_3
>> estadd scalar Q = r(stat)
>>
>> eststo: quietly arima DS4.ln_m1, ar(1/2) ma(1/2)
>> quietly predict res_4, residuals
>> quietly wntestq res_4
>> estadd scalar Q = r(stat)
>>
>> * and create tables with -esttab-
>> esttab, stats(aic bic Q) noobslast nomtitles
>> * ----- end code -----
>>
>> Programming is the next task in my R-to-Stata switch. I will update
>> when I learn how to code a -estadd_wntestq- solution.
>>
>> On Wed, Sep 14, 2011 at 20:32, Richard Herron
>> <[email protected]> wrote:
>>>
>>> I have an -esttab- table with multiple -arima- models to which I would
>>> because Q stats are not part of -arima- objects. Is there a way that I
>>> can chain together -predict, residuals- and -wntestq- to add Q stats
>>> to my -esttab- table? Thanks!
>>>
>>> Here is some code (including how I would find Q stats "manually"):
>>>
>>> * ----- begin code -----
>>> * I know how to combine regressions with -eststo-
>>> webuse friedman2, clear
>>> generate ln_m1 = ln(m1)
>>> eststo clear
>>> eststo: quietly arima DS4.ln_m1, ar(1) ma(2)
>>> eststo: quietly arima DS4.ln_m1, ar(1) ma(1/2)
>>> eststo: quietly arima DS4.ln_m1, ar(1/2) ma(2)
>>> eststo: quietly arima DS4.ln_m1, ar(1/2) ma(1/2)
>>>
>>> * and create tables with -esttab-
>>> esttab, aic bic noobslast nomtitles
>>>
>>> * I would like to add Q stats to each model with -estadd-, but I can't
>>> figure out how
>>>
>>> * here's how I find Q stats "manually"
>>> quietly arima DS4.ln_m1, ar(1/2) ma(1/2)
>>> predict res, residuals
>>> wntestq res, lags(8)
>>> * ----- end code -----
>
> --
> Stas Kolenikov, also found at http://stas.kolenikov.name
> Small print: I use this email account for mailing lists only.
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```

• Follow-Ups:
• References: