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

# RE: st: RE: Looping with -qreg-

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject RE: st: RE: Looping with -qreg- Date Thu, 19 Jan 2012 18:48:35 +0000

```I now understand less about what you want, as I thought you wanted to constrain your -qreg-s to have the same intercept, but now you say the intercepts will be different.

Regardless of that:

1. I checked and -qreg- does not allow -constraint-s. This isn't surprising as it uses a quite distinct algorithm which I imagine doesn't mesh with constraints.

2. Your syntax is very confused. Somehow you are confusing constraints with local macros; constraints are not applied by using -if-; you are also confusing scalars with locals.

The help for -constraint- does give clear examples.

Nick
n.j.cox@durham.ac.uk

STANEVA A. (497186)

The intercept should be different of course; I haven't copy this bit of the code, as I think is working ok.  I defined it by every percentile in the following way following someone in the literature:

forval i = 1/99 {
local q = `i'/100 /* quantile to be estimated */
quietly qreg \$y \$x if public==0, quantile(`q') nolog
predict uresid, resid
preserve
keep if uresid~=. /* keep valid predictions */
keep uresid /* throw away other vars */
save uresid`i', replace
restore
scalar u99=sum(ws_g99*uresid) /*derive the intercept by every percentile, ws_g99 is the weights of each individual residual defined in my sample*/
drop uresid
}

I'm not quite sure how the constraint option works. I did try to constraint the qreg by defining the constraint being u99.

constraint define 1 [_cons]_cons=`u99'
if `"`constraint'"'!="" {
quietly qreg \$y \$x psm1 psm2 if `constraint (1)' & public==0, quantile(`q') nolog
predict xb if public==0
However, the syntax wasn't seems to be correct.
It is a bit complicated, so I really will appreciate your help.
Best
Anita

________________________________________
From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Nick Cox [njcoxstata@gmail.com]
Sent: 19 January 2012 13:59
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: RE: Looping with -qreg-

So it seems you want a set of quantile regressions for fractions 0.01
(0.01)0.99 but with the same intercept. Wow!

I would suggest being clearer on what you tried with -constraint- and
why it didn't solve the problem.

Nick

On 19 Jan 2012, at 12:03, "STANEVA A. (497186)" <A.V.STANEVA.497186@swansea.ac.uk
> wrote:

> I did try with constraint option but didn't get what I need.
> Basically my problem is to estimate the last qreg with the new
> constant term  (u99) 'plug' in the model. There is not noncons
> option in qreg.
> Best wishes
> Anita
>
> ________________________________________
> From: owner-statalist@hsphsun2.harvard.edu [owner-
> statalist@hsphsun2.harvard.edu] on behalf of Nick Cox
> [njcoxstata@gmail.com]
> Sent: 19 January 2012 11:52
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: Looping with -qreg- [was: Re: st: Thread-Index:
> AczWnG9bfd8mYxtIRa2sdn3BOpo1xA==]
>
> Also, it sounds as if you want to constrain your -qreg-, which makes
> me feel queasy even contemplating the idea from a distance, but if so
> that implies trying -constraint-.
>
> Nick
>
> On Thu, Jan 19, 2012 at 11:45 AM, Nick Cox <njcoxstata@gmail.com>
> wrote:
>> people delete posts unread if they have weird titles.
>>
>> There are problems at various levels with your code.
>>
>> 1. The line
>>
>> local `b' = scalar(u99)
>>
>> looks like a mistake. If local b _was_ previously defined, then you
>> need to refer to what this line defines as ``b''.
>>
>> 2. But you don't refer either to `b' or to ``b'' after defining it,
>> so
>> all you have done is defined something but never used it.
>>
>> 3. If local b was not previously defined, this line would not work.
>> But that is not what you are reporting, so my guess is that a "local
>> b" is defined earlier in your code.
>>
>> 4. If you have a scalar, there is no obvious need to put it into a
>> local for what you want to do.
>>
>> 5. I don't understand what you want to do, but you are changing
>> results, not instructions. Later runs of -qreg- pay no attention to
>> e(b) produced by earlier runs of -qreg-.
>>
>> 6. -matrix b- and -local b- are completely different entities.
>>
>> Nick
>>
>> On Thu, Jan 19, 2012 at 11:21 AM, STANEVA A. (497186)
>> <A.V.STANEVA.497186@swansea.ac.uk> wrote:
>>
>>> Can someone help me with this loop in a qreg?
>>> This first line works ok!
>>>
>>> forval i = 1/99 {
>>> local q = `i'/100
>>> quietly qreg \$y \$x if public==0, quantile(`q') nolog
>>> matrix b = e(b)
>>> matrix b[1, colnumb(b,"_cons")] = scalar(u99) /*I get the constant
>>> and replace it with the scalar u99, that I want/
>>> }
>>>
>>> The problem comes here. I want the replaced u99 to be included in
>>> my qreg and then to get the predictions based on that regression:
>>>
>>> forval i = 1/99 {
>>> local q = `i'/100
>>> local `b'= scalar (u99)
>>> quietly qreg \$y \$x if public==0, quantile (`q') nolog
>>> predict xb if public==0
>>> preserve
>>> keep if xb~=.
>>> restore
>>> }
>>> However, there is a problem in the line local`b'=scalar(u99), it
>>> seems stata doesn't replace the _cons with the u99. It runs norm
>>> al qreg, ignoring all this manipulations.
>>>

*
*   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/
```