Bookmark and Share

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: Forvalues to make a moving average on non timeseries data.


From   "William Gould, StataCorp LP" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Forvalues to make a moving average on non timeseries data.
Date   Mon, 17 Mar 2014 14:52:26 -0500

Kevin Michael Snapp <[email protected]> wrote, 

> I have to make a disclosure, this is for a homework assignment.  I've
> emailed the professor but I haven't had a response yet, as its spring
> break.  We are working through the Cameron and Trivedi [...]

Kevin then went on to ask his question. 

Nick Cox <[email protected]> wrote, 

> Having declared this honestly as homework, you have placed this beyond
> the scope of the list. Naturally, I don't have, and don't want, the
> power to forbid or prevent anyone answering, but we do have a policy.

Unfair! 

Sometimes rules need to be broken, and I know Nick agrees with that
statement.  Why isn't this one of them?  Kevin isn't lazy.  He wrote a
solution -- I'm not saying it's right or wrong-- but he wrote one and
he spent time writing to us on Statalist.  He certainly spent time,
and, even better, what he wrote demonstrates both thought and
understanding.

And he ran into a stupid error message, which indicates a mechanical, 
technical detail mistake, and he just can't see it. 

I'm a really experienced programmer.  And yet, there are times when I
stare at five lines of code, knowing there's a mistake buried in them,
and I can't see it.  I know there's an error because of an error
message, but I don't see how.

I'm tempted to just point the error out to him.  In deference 
to Nick's interpreation of our rule, however, I'll write a long-winded
explanation so that Kevin can find it for himself.

Kevin showed his solution to a moving average problem, 

> generate MA = .
> forvalues i = 2037/2053 {
>     forvalues j -12/12 {
>     quietly replace MA[`i'] =( MA[`i'] + lnearns[`j' + `i'])
>     }
> }

and he says he got the error message "weights not allowed". 

Somtimes, Kevin, error messages are misleading.  Whenever a computer
program tells you there's an error, you can be certain that there is
indeed an error (assuming you trust the software, meaning among other 
things, you didn't write it).  Sometimes, however, the error is not 
due to the cause that the computer states. 

That error messages are sometimes mistaken as to reason usually isn't a
problem because the message is right up against the statement that has
the error.  Because of the braces and looping, however, Stata showed
all the statements before issuing the error.

In cases like these, you need to look back and ask yourself, which line
is Stata complaining about?  You know it's one of them.  You need to
look at each lne carefully, thinking about weights, but also
understanding that the computer-identified cause can be misleading, so
it might be something else.

So find the line Stata is complaining about. 

The first line is, 

        generate MA = . 

That line looks right to me.  You could try it in isolation, but it
would work.

Now look at the next statement.  Is it right?  I'm not asking whether 
the line is substantively right, I'm asking whether the line contains 
a problem that a stupid computer program to spot?  Compare the line 
to the syntax in -help forvalues-.  Is it right?  The syntax is 

            forvalues <lname> = <range> { 
                    ...
            }

Your second line is 

            forvalues i = 2037/2053 {
                    ...
            }

Well, -foravlues- matches -forvalues-.  -i- matches <lname>. Equal sign 
matches equal sign.  -2037/2053/ matches <range>.  Open brace matches 
open brace. 

Is it right?  If so, continue to the next line. 

Once you find the mechanical error, fix it.  Then determine if your 
program is giving the correct answers. 

Best of luck. 

-- Bill
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index