Announcement

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

  • Problem with Maximum Likelihood model

    Hello, everyone.

    I have run the following code and got the r(133) error 'unknown function ()'. It is my first time running a Maximum Likelihood model; therefore, I don't really know where to look. I have removed the spaces, tried to modify the last part, and write
    ml model lf myll (d: _cons) (l1: _cons) (l2: _cons)
    ml maximize

    but nothing worked.

    The code is:


    capture program drop myll

    use first_spell_for_ml.dta, clear


    summarize e0i, meanonly
    local u = 1 - r(mean)

    program define myll
    version 18.1
    args d l1 l2

    quietly {
    * 1) k and λ0
    local k = `l1'/(`d'+`l2')
    local l0 = `d'*( (1-`u')/`u' )

    * 2) temporaries
    tempvar F0 f0 F1 f1 mu logli

    * CDF & density at w0i
    gen double `F0' = ((1+`k')*Ghat0) / (1 + `k'*Ghat0)
    gen double `f0' = (1+`k') / ( (1 + `k'*Ghat0)^2 ) * ghat0

    * CDF & density at w1i
    gen double `F1' = ((1+`k')*Ghat1) / (1 + `k'*Ghat1)
    gen double `f1' = (1+`k') / ( (1 + `k'*Ghat1)^2 ) * ghat1

    * 3) hazard μ_i
    gen double `mu' = cond(e0i==1, `d'+`l2'+`l1'*`F0', `l0')

    * 4) survival term
    gen double `logli' = - `mu' * t0i
    replace `logli' = `logli' + (cs0i==0)*ln(`mu')

    * 5) branch on transition
    * job→unemp
    replace `logli' = `logli' + ///
    (e0i==1 & cs0i==0 & e1i==0)*ln( `d'/`mu' )

    * job→job
    replace `logli' = `logli' + ///
    (e0i==1 & cs0i==0 & e1i==1)*(
    ln((`l2'+`l1')/`mu') + ln(`f1')
    )

    * nonemp→job
    replace `logli' = `logli' + ///
    (e0i==0 & cs0i==0 & e1i==1)*(
    ln(`l0') + ln(`f1')
    )

    * 6) total log‐likelihood
    quietly summarize `logli', meanonly
    return scalar lf = r(sum)
    }
    end

    *where the problem seems to occur
    ml model lf myll (d(l1(l2
    ml maximize



    I have tried a different version of the code, and the problem seemed to be that Stata didn't recognize my parameters and saw them as variables. I used to get error r(198) '05 invalid name'. The code I used was:




    use first_spell_with_gdensity.dta, clear
    capture program drop loglf
    program define loglf

    args lnf d l0 l1 l2


    capture drop double kappa F0 lambdaE lambdaU rate S h pEE pEU f1 logL

    // steady‐state mapping
    gen double kappa = `l1' / (`d' + `l2')
    gen double F0 = (1 + kappa)*Ghat / (1 + kappa*Ghat)

    // hazard rates

    gen double lambdaE = `d' + `l2' + `l1'*(1-F0) // employed spells


    gen double lambdaU = `l0' // unemployed spells



    // 1) survival up to t0i
    gen double rate = cond(e0i==1, lambdaE, lambdaU)
    gen double S = exp( - rate * t0i )

    // 2) density of exit at t0i
    gen double h = rate * S

    // 3) transition‐probabilities for job‐spells
    gen double pEE = (`l2' + `l1'*F0)/lambdaE
    gen double pEU = `d'/lambdaE

    // 4) offer‐density at w1 (for UE‐ or job→job spells)
    gen double f1 = ((1 + kappa)/((1 + kappa*Ghat)^2)) * ghat_density ///
    if cs0i==0 & e1i==1



    gen double logL = .
    quietly {
    // case 1: censored spells (any initial state)
    replace logL = ln(S) if cs0i==1

    // case 2: uncensored job‐spells
    replace logL = ln(h) + ln( cond(e1i, pEE, pEU) ) if e0i==1 & cs0i==0

    // case 3: add f1 for job→job transitions
    replace logL = logL + ln(f1) if e0i==1 & cs0i==0 & e1i==1

    // case 4: uncensored nonemp spells (must take a job‐offer)
    // h=λ0 S ; then one job‐offer density f1
    replace logL = ln(h) + ln(f1) if e0i==0 & cs0i==0
    }

    // pass the result back to ml
    replace `lnf' = logL
    end

    *where the problem seems to occur
    ml model lf loglf ///
    (d:0.05 l0:0.10 l1:0.10 l2:0.05) ///
    , maximize difficult









  • #2
    There is an ml trace option that you set on, which might help you locate the problem.

    As an aside, you seem to be referring by name to variables in the dataset from within the evaluator program. Maybe look into trying to pass them through ml perhaps via nonlinear equations.

    Comment


    • #3
      The code in #1 is very hard to follow. At one point you seem to be defining a program within code that defines another program.

      That aside, the error message you got may be because a program is evaluating a local macro that it can't see.

      Comment

      Working...
      X