Announcement

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

  • command moptimize is unrecognized

    Hi all,

    I am trying to find optimized parameters (a and b) to minimize my objective function. The objective function is already defined in the program named as 'myobjfunc', which has a negative sign.
    However, moptimize command is unrecognized and the command is in black in my do-file. Could you give any hints by looking at the following code?
    Thank you.

    Code:
    gen opt_a = .
    gen opt_b = .
    
    
    levelsof USERID, local(userids)
    // constraints
    constraint 1 a > 1
    constraint 2 b > 1
    //initial_values
    scalar a_init = 1.1
    scalar b_init = 1.1
    foreach id of local userids {
        // keep the original dataset
        preserve
        
        // pick USERID satisfying condition No_dens > =3 , one by one.
       tempvar is_current_user
        gen `is_current_user' = (USERID == "`id'")
    
        keep if `is_current_user' & No_dens >= 3
    
    
        // optimization
    
       moptimize(myobjfunc) initial(a a_init b b_init) constraints(1 2)
        
        // bring optimized_a and optimized_b
        matrix opt_values = e(b)
        scalar opt_a_temp = opt_values[1,1]
        scalar opt_b_temp = opt_values[1,2]
    . // replace opt_a and opt_b in the original dataset
        replace opt_a = opt_a_temp if `is_current_user'
        replace opt_b = opt_b_temp if `is_current_user'
       // restore the original dataset for repeating same procedure to the next USERID
        restore
        
    
    
    }
    Last edited by Nayeon Kang; 29 Aug 2023, 10:23.

  • #2
    As I understand, moptimize() is a Mata function, not a command, and must be called as such.

    Comment

    Working...
    X