Announcement

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

  • ml "invalid syntax" problem, thanks!

    Hi all,

    I'm new to ml and it's really frustrating. It kept saying "invalid syntax" while I can't figure out where is the mistake.
    Thank you for your time and attention in advance.

    code for ml evaluator:
    program myMLE_gf0
    args todo b lnfj
    scalar seta1=`b'[1,1]
    scalar seta2=`b'[1,2]
    scalar seta3=`b'[1,3]
    capture confirm matrix EV_s0
    capture confirm matrix EV_s1
    scalar s0 = 1
    dis s0
    scalar s1 = 2
    dis s1
    mata: EV_s0 = getEV("seta1","seta2","seta3","M","s0")
    mata: st_matrix("EV_s0",EV_s0)
    dis EV_s0
    mata: EV_s1 = getEV("seta1","seta2","seta3","M","s1")
    mata: st_matrix("EV_s1",EV_s1)
    dis EV_s1
    mata: CProb_s0=getCProb("seta1","seta2","seta3","M","s0" ,"EV_s0")
    mata: st_matrix("CProb_s0",CProb_s0)
    dis CProb_s0
    mata: CProb_s1=getCProb("seta1","seta2","seta3","M","s1" ,"EV_s1")
    mata: st_matrix("CProb_s1",CProb_s1)
    dis CProb_s1

    forvalues bus_id = 0/`N' {
    forvalues mile_now = 0/`M' {
    forvalue action_now = 0/1 {
    quietly replace `lnfj'= Q_bus[`bus_id'+1,1]*ln(`action_now'*CProb_s0[1,`mile_now'+1]+(1-`action_now')*(1-CProb_s0[1,`mile_now'+1]))+ (1-Q_bus[`bus_id'+1,1])*ln(`action_now'*CProb_s1[1,`mile_now'+1]+(1-`action_now')*(1-CProb_s1[1,`mile_now'+1])) if bus==`bus_id' & mile==`mile_now' & action ==`action_now'
    }
    }
    }
    end


    ------------------end of code for ml evaluator----------------------------------

    Then:
    ml model gf0 myMLE_gf0 (action = mile period)
    ml check

    It shows:
    --------------------message from stata-----------------------------------------
    ...
    CProb_s1[1,6]
    c1 c2 c3 c4 c5 c6
    r1 .5 .5 .5 .5 .5 .5
    ----------------------------------------------------- end _matrix_cmds.List ---
    - exit
    ------------------------------------------------------------ end _matrix_cmds ---
    - forvalues bus_id = 0/`N' {
    = forvalues bus_id = 0/ {
    invalid syntax
    ----------------------------------------------------------------- end myMLE_gf0 ---
    - `end'
    = set trace off
    ------------------------------------------------------------------------------
    Fix myMLE_gf0.
    r(198);
    --------------------end of message from stata-----------------------------------------


    Looking forward to some one help me out from this.
    Thanks a lot!

  • #2
    Local macro N is undefined in your code. Nor is local macro M. The trace is showing you the first, as a reference to N is evaluated as empty.

    Comment


    • #3
      Thank you very much for your reply!
      Before ml evaluator, I have assigned value to the scalar N, scalar M, and matrix Q_bus.
      This time I typed "dis N" in ml evaluator, it shows that N has been assigned 100.
      :-(




      Here is a trace of its execution:
      ------------------------------------------------------------------------------
      -> myMLE_gf0 0 __000009 __00000A __00000B __00000C
      - `begin'
      = capture noisily version 12.1: myMLE_gf0 0 __000009 __00000A __00000B __00000C
      ----------------------------------------------------------------------------------------------------------------------------------------------------------- begin myMLE_gf0 ---
      - args todo b lnfj
      - scalar seta1=`b'[1,1]
      = scalar seta1=__000009[1,1]
      - dis seta1
      0
      - scalar seta2=`b'[1,2]
      = scalar seta2=__000009[1,2]
      - dis seta2
      0
      - scalar seta3=`b'[1,3]
      = scalar seta3=__000009[1,3]
      - dis seta3
      0
      - capture confirm matrix EV_s0
      - capture confirm matrix EV_s1
      - scalar s0 = 1
      - dis s0
      1
      - scalar s1 = 2
      - dis s1
      2
      - mata: EV_s0 = getEV("seta1","seta2","seta3","M","s0")
      1
      - mata: st_matrix("EV_s0",EV_s0)
      - mat list EV_s0
      ------------------------------------------------------------------------------------------------------------------------------------------------------ begin _matrix_cmds ---
      - version 11.0
      - gettoken sub rest : 0, parse(" ,")
      - local 0 ,`sub'
      = local 0 ,list
      - capture syntax [ , List ]
      - if _rc {
      error 501
      }
      - if "`list'" != "" {
      = if "list" != "" {
      - List `rest'
      = List EV_s0
      ----------------------------------------------------------------------------------------------------------------------------------------------- begin _matrix_cmds.List ---
      - version 11
      - syntax anything(id="matrix name" name=mname) [, noBlank noHAlf noHeader noNames Format(string) TItle(string) nodotz ]
      - confirm matrix `mname'
      = confirm matrix EV_s0
      - if `"`format'"' != "" {
      = if `""' != "" {
      confirm numeric format `format'
      }
      - local bl = "`blank'" == ""
      = local bl = "" == ""
      - local ha = "`half'" == ""
      = local ha = "" == ""
      - local he = "`header'" == ""
      = local he = "" == ""
      - local na = "`names'" == ""
      = local na = "" == ""
      - local dz = "`dotz'" == ""
      = local dz = "" == ""
      - local title : list clean title
      - mata: st_matrix_list( "`mname'", "`format'", `"`title'"', `bl',`ha',`he',`na',`dz')
      = mata: st_matrix_list( "EV_s0", "", `""', 1,1,1,1,1)

      EV_s0[2,6]
      c1 c2 c3 c4 c5 c6
      r1 6.931471 6.931471 6.931471 6.931471 6.931471 6.931471
      r2 6.931471 6.931471 6.931471 6.931471 6.931471 6.931471
      ------------------------------------------------------------------------------------------------------------------------------------------------- end _matrix_cmds.List ---
      - exit
      -------------------------------------------------------------------------------------------------------------------------------------------------------- end _matrix_cmds ---
      - mata: EV_s1 = getEV("seta1","seta2","seta3","M","s1")
      - mata: st_matrix("EV_s1",EV_s1)
      - mat list EV_s1
      ------------------------------------------------------------------------------------------------------------------------------------------------------ begin _matrix_cmds ---
      - version 11.0
      - gettoken sub rest : 0, parse(" ,")
      - local 0 ,`sub'
      = local 0 ,list
      - capture syntax [ , List ]
      - if _rc {
      error 501
      }
      - if "`list'" != "" {
      = if "list" != "" {
      - List `rest'
      = List EV_s1
      ----------------------------------------------------------------------------------------------------------------------------------------------- begin _matrix_cmds.List ---
      - version 11
      - syntax anything(id="matrix name" name=mname) [, noBlank noHAlf noHeader noNames Format(string) TItle(string) nodotz ]
      - confirm matrix `mname'
      = confirm matrix EV_s1
      - if `"`format'"' != "" {
      = if `""' != "" {
      confirm numeric format `format'
      }
      - local bl = "`blank'" == ""
      = local bl = "" == ""
      - local ha = "`half'" == ""
      = local ha = "" == ""
      - local he = "`header'" == ""
      = local he = "" == ""
      - local na = "`names'" == ""
      = local na = "" == ""
      - local dz = "`dotz'" == ""
      = local dz = "" == ""
      - local title : list clean title
      - mata: st_matrix_list( "`mname'", "`format'", `"`title'"', `bl',`ha',`he',`na',`dz')
      = mata: st_matrix_list( "EV_s1", "", `""', 1,1,1,1,1)

      EV_s1[2,6]
      c1 c2 c3 c4 c5 c6
      r1 6.931471 6.931471 6.931471 6.931471 6.931471 6.931471
      r2 6.931471 6.931471 6.931471 6.931471 6.931471 6.931471
      ------------------------------------------------------------------------------------------------------------------------------------------------- end _matrix_cmds.List ---
      - exit
      -------------------------------------------------------------------------------------------------------------------------------------------------------- end _matrix_cmds ---
      - mata: CProb_s0=getCProb("seta1","seta2","seta3","M","s0" ,"EV_s0")
      1
      - mata: st_matrix("CProb_s0",CProb_s0)
      - mat list CProb_s0
      ------------------------------------------------------------------------------------------------------------------------------------------------------ begin _matrix_cmds ---
      - version 11.0
      - gettoken sub rest : 0, parse(" ,")
      - local 0 ,`sub'
      = local 0 ,list
      - capture syntax [ , List ]
      - if _rc {
      error 501
      }
      - if "`list'" != "" {
      = if "list" != "" {
      - List `rest'
      = List CProb_s0
      ----------------------------------------------------------------------------------------------------------------------------------------------- begin _matrix_cmds.List ---
      - version 11
      - syntax anything(id="matrix name" name=mname) [, noBlank noHAlf noHeader noNames Format(string) TItle(string) nodotz ]
      - confirm matrix `mname'
      = confirm matrix CProb_s0
      - if `"`format'"' != "" {
      = if `""' != "" {
      confirm numeric format `format'
      }
      - local bl = "`blank'" == ""
      = local bl = "" == ""
      - local ha = "`half'" == ""
      = local ha = "" == ""
      - local he = "`header'" == ""
      = local he = "" == ""
      - local na = "`names'" == ""
      = local na = "" == ""
      - local dz = "`dotz'" == ""
      = local dz = "" == ""
      - local title : list clean title
      - mata: st_matrix_list( "`mname'", "`format'", `"`title'"', `bl',`ha',`he',`na',`dz')
      = mata: st_matrix_list( "CProb_s0", "", `""', 1,1,1,1,1)

      CProb_s0[1,6]
      c1 c2 c3 c4 c5 c6
      r1 .5 .5 .5 .5 .5 .5
      ------------------------------------------------------------------------------------------------------------------------------------------------- end _matrix_cmds.List ---
      - exit
      -------------------------------------------------------------------------------------------------------------------------------------------------------- end _matrix_cmds ---
      - mata: CProb_s1=getCProb("seta1","seta2","seta3","M","s1" ,"EV_s1")
      - mata: st_matrix("CProb_s1",CProb_s1)
      - mat list CProb_s1
      ------------------------------------------------------------------------------------------------------------------------------------------------------ begin _matrix_cmds ---
      - version 11.0
      - gettoken sub rest : 0, parse(" ,")
      - local 0 ,`sub'
      = local 0 ,list
      - capture syntax [ , List ]
      - if _rc {
      error 501
      }
      - if "`list'" != "" {
      = if "list" != "" {
      - List `rest'
      = List CProb_s1
      ----------------------------------------------------------------------------------------------------------------------------------------------- begin _matrix_cmds.List ---
      - version 11
      - syntax anything(id="matrix name" name=mname) [, noBlank noHAlf noHeader noNames Format(string) TItle(string) nodotz ]
      - confirm matrix `mname'
      = confirm matrix CProb_s1
      - if `"`format'"' != "" {
      = if `""' != "" {
      confirm numeric format `format'
      }
      - local bl = "`blank'" == ""
      = local bl = "" == ""
      - local ha = "`half'" == ""
      = local ha = "" == ""
      - local he = "`header'" == ""
      = local he = "" == ""
      - local na = "`names'" == ""
      = local na = "" == ""
      - local dz = "`dotz'" == ""
      = local dz = "" == ""
      - local title : list clean title
      - mata: st_matrix_list( "`mname'", "`format'", `"`title'"', `bl',`ha',`he',`na',`dz')
      = mata: st_matrix_list( "CProb_s1", "", `""', 1,1,1,1,1)

      CProb_s1[1,6]
      c1 c2 c3 c4 c5 c6
      r1 .5 .5 .5 .5 .5 .5
      ------------------------------------------------------------------------------------------------------------------------------------------------- end _matrix_cmds.List ---
      - exit
      -------------------------------------------------------------------------------------------------------------------------------------------------------- end _matrix_cmds ---
      - dis N
      100
      - dis M
      5
      - forvalues bus_id = 0/`N' {
      = forvalues bus_id = 0/ {
      invalid syntax
      ------------------------------------------------------------------------------------------------------------------------------------------------------------- end myMLE_gf0 ---
      - `end'
      = set trace off
      ------------------------------------------------------------------------------
      Fix myMLE_gf0.
      r(198);

      .

      Comment


      • #4
        But locals are not scalars and scalars are not locals. Here is one way to get what you want:

        Code:
        local N = scalar(N) 
        forvalues bus_id = 0/`N' {
            local M = scalar(M) 
            forvalues mile_now = 0/`M' {
        I have not looked at the rest of the code.

        Comment

        Working...
        X