Announcement

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

  • Ado files and mata function

    Hi,

    I am writing a stata program. I would like to have as one of the arguments of the stata program to be a mata function. Can I get some advice if this is feasible and if it is, what is the best way to go about doing this? It would be great if someone can direct me to the relevant documentation.

    For example, my code will look something like the following:

    Code:
    program define logdensity 
    
    syntax varlist(max = 1) [if] [in], g(mata function)
    marksample touse
     
    mata: PS_estimator(varlist, g)
    end
    
    mata
    
    void PS_estimator(varlist, g){
    
    "performs calculation at the background that involves the g function"
    
    }
    end
    Secondly, I would also like to know if there is a way to allow mata functions to accept other mata functions as an argument. Thanks.

  • #2
    Dear Han

    One way could be seeking inspiration from:
    Code:
    cls
    clear
    mata mata clear
    capture program drop rmf
    program define rmf //run mata function 
    syntax varlist(max = 1) [if] [in], Function(string)
    
    if !inlist(`"`function'"', "mean", "sum") _error("Allowed mata functions are mean or sum")
    else mata: f`function'(`"`varlist'"')
    end
    
    mata:
        real scalar fmean(string scalar vn) return(mean(st_data(.,vn)))
        real scalar fsum(string scalar vn) return(sum(st_data(.,vn)))
    end
    
    set seed 4
    set obs 10
    generate x = runiformint(0,5)
    rmf x, f(sum)
    rmf x, f(mean)
    Regarding your second question look at pointers

    Kind regards

    nhb

    Comment


    • #3
      Thanks Niels!

      Comment

      Working...
      X