On Sun, 2014-02-02 at 17:35,
hq...@gopivotal.com wrote:
> User inputs a function, and a data.frame which contains all the variables
> that appear in the function. I will need to substitute the mean values of
> the variables into the function. (Actually for computing the marginal
> effects, one also needs to compute the average of the function values
> evaluated at all rows of the data.frame).
I think you're making hacking-life more complicated than it already is!
You'll only need macros if you insist that the naming of the function
arguments is automatically matched against the column names of the
dataframe. But I don't think that that is good idea: names of function
arguments are here to refer to values inside the function and not
outside of it. Nor is it, I think, a particular Julian way of coding.
I suggest instead something like this:
user supplies
- a function:
f(a,b,c,d) = ...
- a DataFrame:
df
- a tuple/list of column names to be used in the order they need to be
inserted into f; i.e. this is a mapping from column-names to function
argument position. E.g.:
("height", :width, 'd', :x)
you provide a function like so:
function F(userfn, datafr, fields)
# take mean of dataframe columns
colmeans = [mean(datafr[fl]) for fl in fields]
# maybe do some more stuff:
# call user function
return userfn(colmeans...) # (the three dots are the syntax used here)
end
Then the user can call it like so:
F(f, df, ("height", :width, 'd', :x))
I reckon you ought to give this kind of user interface a try and see
whether that works for you.