Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stata crashes when using moptimze, subviews, and factor variables

    I have run across a problem when using moptimize() with a subview that contains factor variables. Here's a quick program that can be used as as an example:

    Code:
    mata:
    void mylogit(real colvector Ymat, real matrix Xmat)
    {
      transmorphic S
      S=moptimize_init()
      moptimize_init_evaluator(S, &logit_eval())
      moptimize_init_evaluatortype(S,"lf")
      moptimize_init_depvar(S,1,Ymat)
      moptimize_init_eq_indepvars(S,1,Xmat)  
      moptimize_init_eq_colnames(S, 1, (J(1,cols(Xmat),"x") + strofreal((1..cols(Xmat))))) // (another problem, which might be related, is that moptimize throws an error message if X includes factor variables; this is a quick hack)
      moptimize(S)
      moptimize_result_display(S)
    }
    void logit_eval(transmorphic S, real rowvector beta, real colvector lnf)
    {
      real colvector Y, pm, xb, lj
      Y  = moptimize_util_depvar(S, 1)
      xb = moptimize_util_xb(S, beta, 1)
      pm = 2*(Y :!= 0) :- 1
      lj = invlogit(pm:*xb)
      lnf  = ln(lj)
    }
    end
    If I run the following code, Stata will completely crash -- killing the Stata.exe process in Windows. This is not simply a matter of Stata returning an error code. It actually crashes.
    Code:
    sysuse auto
    gen vlong = (length>150)
    mata: 
    Y=X=dta=.
    st_view(dta, ., "foreign price weight 0b0.vlong 1b0.vlong")
    st_subview(Y, dta, ., 1 )
    st_subview(X, dta, ., 2\. )
    invsym(quadcross(X, 1, X, 1))*quadcross(X, 1, Y, 0)
    mylogit(Y,X)
    "If you can see this, Stata did not crash"
    end
    This code is supposed to reproduce the coefficients from logit. The invsym() line produces the coefficients from regress, and demonstrates that the subviews (X and Y) are fine. That is, there is no problem with the way the subviews are created, but instead is a problem with how moptimize() uses these subviews.

    However, I don't have any problem if X is a view (instead of being a subview):
    Code:
    mata: 
    Y=X=.
    st_view(Y, ., "foreign")
    st_view(X, ., "price weight 0b0.vlong 1b0.vlong")
    mylogit(Y,X)
    end
    Furthermore, I don't have any problem if X is a subview without factor variables:
    Code:
    mata: 
    Y=X=dta=.
    st_view(dta, ., "foreign price weight")
    st_subview(Y, dta, ., 1 )
    st_subview(X, dta, ., 2\. )
    mylogit(Y,X)
    end
    Does anyone have any idea on how to make this work? Is the problem on my end, or a problem with the compiler? Thanks so much for the help!

    Best,
    Keith



    In case it's helpful, I'm using the latest version of Stata 15.1 MP for Windows:

    Code:
    c(stata_version) = 15.1
    c(version) = 15.1 (version)
    c(userversion) = 15.1 (version)
    c(dyndoc_version) = 1 (dyndoc)
    -----------------------------------------------------------------------------------------------------------
    c(born_date) = "30 Jan 2018"
    c(flavor) = "IC"
    c(bit) = 64
    c(SE) = 1
    c(MP) = 1
    c(processors) = 4 (Stata/MP, set processors)
    c(processors_lic) = 4
    c(processors_mach) = 8
    c(processors_max) = 4
    c(mode) = ""
    c(console) = ""
    -----------------------------------------------------------------------------------------------------------
    c(os) = "Windows"
    c(osdtl) = "64-bit"
    c(hostname) = "KKranker"
    c(machine_type) = "PC (64-bit x86-64)"
    c(byteorder) = "lohi"

  • #2
    In case it's helpful, here's a .do file and a log file.
    Attached Files

    Comment


    • #3
      Well, it similarly fails in my macOS setup, so whatever Stata is doing, it's consistent across two platforms, mine running IC and yours running MP. Sounds like you should be talking to Stata Technical Services. Even if the root cause is an error on your part, gracelessly crashing is not the ideal response.
      Code:
             c(stata_version) = 15.1
                   c(version) = 15.1                       (version)
               c(userversion) = 15.1                       (version)
            c(dyndoc_version) = 1                          (dyndoc)
          --------------------------------------------------------------------------------------------
                 c(born_date) = "30 Jan 2018"
                    c(flavor) = "IC"
                       c(bit) = 64
                        c(SE) = 1
                        c(MP) = 0
                c(processors) = 1                          (Stata/MP, set processors)
            c(processors_lic) = 1
           c(processors_mach) = .
            c(processors_max) = 1
                      c(mode) = ""
                   c(console) = ""
          --------------------------------------------------------------------------------------------
                        c(os) = "MacOSX"
                     c(osdtl) = "10.13.3"
                  c(hostname) = "MacBook.local"
              c(machine_type) = "Macintosh (Intel .."
                 c(byteorder) = "lohi"

      Comment


      • #4
        FWIW I get the same problem: up-to-date Stata 14.2 and Stata 15.1 (Windows 7/64 bit)

        Comment


        • #5
          Thanks to kkranker for giving us code that reproduces the problem.

          Long story short, some built-in moptimize() utilities were not playing nice with Mata subviews containing factor variables.

          We will fix this very soon.

          Comment


          • #6
            Thanks for looking into this, Jeff. I thought I was going crazy!

            Comment

            Working...
            X