Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Mata st_store problem with selectvar (using mm_root)


From   Ben Jann <ben.jann@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Mata st_store problem with selectvar (using mm_root)
Date   Wed, 11 Feb 2009 21:38:17 +0100

Think of what you are asking Mata to do.

 st_store(1, ...)

means: "Write something to observation 1." If you type

 st_store(1, "x", "rc", x)

(or -st_store(1, "x", 1, x)- if rc is the first variable) and variable
"rc" is zero for the first observation, this means: "Write x to no
observation". "no observation" has zero rows, but x is a scalar in
your example and therefore has 1 row. Hence the conformability error.

You could code, e.g.

  rc = (mm_root(x = .,&myfunc(),0,1,0,1000,a)==0)
  if (rc) st_store(1, "x", x)

ben


On Wed, Feb 11, 2009 at 4:25 PM, Bob Hammond <robert_hammond@ncsu.edu> wrote:
> All,
>
> I want to store a Mata scalar in my Stata data set using st_store(), but
> only in certain cases, as determined by selectvar.  My reading of the
> st_data() help file (where st_store() directs you to concerning selectvar)
> says that the following should work:
>
> clear
> cap mata: mata drop myfunc()
> set obs 1
> gen double rc = .
> gen double x = .
> mata
>   function myfunc(x, a) return(x^2 - a)
>   a = 2
>   rc = (mm_root(x = .,&myfunc(),0,1,0,1000,a)==0)
>   st_store(1,"rc",rc)
>   st_store(1,"x",1,x)
> end
> list
>
> My Stata variable "rc" is equal to one if the mm_root() function has found a
> valid solution.  I only want st_store() to write the solution to the data
> set when it is valid.
>
> Instead, the program correctly writes the solution to the data set when it
> is valid (try by changing a to "a = 2/3" above) but generates a "3200
>  conformability error" when it is invalid (try the program as written above,
> which generates no valid solution because it looks between 0 and 1 for the
> square root of 2).
> Maybe this is a simple error in my reading of how to use selectvar but I
> have tried it by placing "rc" and, alternatively, by placing 1 (the variable
> number of "rc") in the third argument of st_store().  Both ways give the
> same conformability error.
>
> Any thoughts would be appreciated.  Thanks in advance,
>
> Bob
> --
> ------------------------------------------------------------------------
> Bob Hammond
> Department of Economics
> North Carolina State University
> Office: (919) 513-2871
> Fax: (919) 515-7873
> http://www4.ncsu.edu/~rghammon/
> *
> *   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index