Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Question about scalars
From
Sergiy Radyakin <[email protected]>
To
[email protected]
Subject
Re: st: Question about scalars
Date
Mon, 9 Aug 2010 15:53:48 -0400
On Mon, Aug 9, 2010 at 3:26 PM, Jeph Herrin <[email protected]> wrote:
> Yes, but look closely at
>
>> . scalar e = d + a
>> . scalar dir a d e // "e" is wrong
>> a = 10
>> d = 4.9458042
>> e = 131
>
> You'll see that 4.94+10 = 131. Which is incorrect.
That's the value of the variable "displacement" in the first
observation {d[isplacement][1]=121}+10=131
as Nick has already pointed out. The code works (there is no Stata
error), but it produces not exactly what you expect.
Stata sometimes warns you about the assumptions it makes to resolve
the naming conflicts, such as in the case:
. scatter price weight, lw(r)
(note: named style r not found in class linewidth, default attributes used
(note: linewidth not found in scheme, default attributes used)
(note: named style r not found in class linewidth, default attributes used
(note: linewidth not found in scheme, default attributes used)
but not always. It would be better if there were more possibilities to
enforce strict mode.
See help set_varabbrev for your case (see below for trace of execution
with variable abbreviation switched off).
Switching to longer (and more readable) names for all your variables
and scalars (macros) will help you avoid
this problem and make the program easier to read and understand.
Best, Sergiy Radyakin
. do "R:\TEMP\STD0h000000.tmp"
. clear
. set varabbrev off
. scalar drop _all
. scalar a = 10
. scalar b = 20
. scalar c = b + a
. scalar dir
c = 30
b = 20
a = 10
.
. sysuse auto, clear
(1978 Automobile Data)
. reg mpg foreign
Source | SS df MS Number of obs = 74
-------------+------------------------------ F( 1, 72) = 13.18
Model | 378.153515 1 378.153515 Prob > F = 0.0005
Residual | 2065.30594 72 28.6848048 R-squared = 0.1548
-------------+------------------------------ Adj R-squared = 0.1430
Total | 2443.45946 73 33.4720474 Root MSE = 5.3558
------------------------------------------------------------------------------
mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
foreign | 4.945804 1.362162 3.63 0.001 2.230384 7.661225
_cons | 19.82692 .7427186 26.70 0.000 18.34634 21.30751
------------------------------------------------------------------------------
. scalar d = _b[foreign]
. scalar e = d + a
. scalar dir a d e // "e" is wrong
a = 10
d = 4.9458042
e = 14.945804
.
. tempname dd
. scalar `dd' = _b[foreign]
. scalar e = `dd' +a
. scalar dir a `dd' e // "e" is /okay
a = 10
__000000 = 4.9458042
e = 14.945804
.
end of do-file
.
>
> cheers,
> Jeph
>
> On 8/9/2010 2:15 PM, Sergiy Radyakin wrote:
>>
>> Hi, Steve,
>>
>> there might be something else in your program that prevents it from
>> running. Both code fragments that you compare work fine in my Stata 9
>> and Stata 11.
>>
>> Best, Sergiy
>>
>> . do "R:\TEMP\STD0h000000.tmp"
>>
>> . clear
>>
>> . scalar drop _all
>>
>> . scalar a = 10
>>
>> . scalar b = 20
>>
>> . scalar c = b + a
>>
>> . scalar dir
>> c = 30
>> b = 20
>> a = 10
>>
>> .
>> . sysuse auto, clear
>> (1978 Automobile Data)
>>
>> . reg mpg foreign
>>
>> Source | SS df MS Number of obs =
>> 74
>> -------------+------------------------------ F( 1, 72) =
>> 13.18
>> Model | 378.153515 1 378.153515 Prob> F =
>> 0.0005
>> Residual | 2065.30594 72 28.6848048 R-squared =
>> 0.1548
>> -------------+------------------------------ Adj R-squared =
>> 0.1430
>> Total | 2443.45946 73 33.4720474 Root MSE =
>> 5.3558
>>
>>
>> ------------------------------------------------------------------------------
>> mpg | Coef. Std. Err. t P>|t| [95% Conf.
>> Interval]
>>
>> -------------+----------------------------------------------------------------
>> foreign | 4.945804 1.362162 3.63 0.001 2.230384
>> 7.661225
>> _cons | 19.82692 .7427186 26.70 0.000 18.34634
>> 21.30751
>>
>> ------------------------------------------------------------------------------
>>
>> . scalar d = _b[foreign]
>>
>> . scalar e = d + a
>>
>> . scalar dir a d e // "e" is wrong
>> a = 10
>> d = 4.9458042
>> e = 131
>>
>> .
>> . tempname dd
>>
>> . scalar `dd' = _b[foreign]
>>
>> . scalar e = `dd' +a
>>
>> . scalar dir a `dd' e // "e" is /okay
>> a = 10
>> __000000 = 4.9458042
>> e = 14.945804
>>
>> .
>> end of do-file
>>
>>
>> On Mon, Aug 9, 2010 at 1:57 PM, Steve Samuels<[email protected]> wrote:
>>>
>>> In the code below, I generate a scalar "d" from a regression result;
>>> it looks okay, but trying to add it to another scalar doesn't work.
>>> If, however I use a -tempname- , I get the right answer. Could
>>> someone explain to me why the first approach doesn't work and if
>>> there's another approach that doesn't involve a -tempname-?
>>>
>>> Thanks,
>>>
>>> Steve
>>>
>>> ******************************
>>> clear
>>> scalar drop _all
>>> scalar a = 10
>>> scalar b = 20
>>> scalar c = b + a
>>> scalar dir
>>>
>>> sysuse auto, clear
>>> reg mpg foreign
>>> scalar d = _b[foreign]
>>> scalar e = d + a
>>> scalar dir a d e // "e" is wrong
>>>
>>> tempname dd
>>> scalar `dd' = _b[foreign]
>>> scalar e = `dd' +a
>>> scalar dir a `dd' e // "e" is /okay
>>> ****************************
>>>
>>> --
>>> Steven Samuels
>>> [email protected]
>>> 18 Cantine's Island
>>> Saugerties NY 12477
>>> USA
>>> Voice: 845-246-0774
>>> Fax: 206-202-4783
>>>
>>> *
>>> * 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/
>>
> *
> * 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/