If you want to do this, the easiest way is to define your own implementation of 
the @~ macro that the latest version Julia uses to parse expressions that look 
like R’s formulas.

That will give you access to the quoted expressions you’d need to manipulate to 
do your analysis.

Given those quoted expressions, you’ll need to define a symbolic 
differentiation tool that’s rich enough to handle the inputs you want to 
process. The Calculus package handles symbolic differentiation for a good chunk 
of functions, but you may need to extend it to your use case.

It may be worth noting that your example makes very heavy usage of R’s 
non-standard evaluation functionality, which is something that the Julia 
community has not invested much time into developing yet. Most Julia 
programmers tend to avoid operating on symbolic expressions.

 — John

On Feb 1, 2014, at 3:42 PM, Walking Sparrow <[email protected]> wrote:

> You are right about that I have an R background. What I am trying to do is to 
> evaluate a function given by the user. For example,
> 
> I want to write a function that can compute the marginal effects of a linear 
> or logistic model. For simplicity, let's just use linear regression. If the 
> user did a linear regression using the following model (I am using the 
> formula syntax from R)
> 
> y ~ x + z + sin(x) * sin(z) for the data set my_data, which has three columns 
> x, y, and z
> 
> Then the marginal effects at the mean are computed like this: First, compute 
> the first derivative of 1+ x + z + sin(x) * sin(z). This can be done in R 
> using the function "deriv" to get the expression of the first derivative. In 
> the second step, I need to substitute the mean values of  x and z into the 
> result of the first step. An example of this would be the "margins" function 
> in the R package "PivotalR" (http://cran.r-project.org/web/packages/PivotalR/ 
> and https://github.com/gopivotal/PivotalR)
> 
> Right now, I have no idea how to do the first step in Julia. But that is OK, 
> because I just started learning Julia.
> 
> Now my question is in the second step. The user can use any complex 
> expressions in the linear regression like y ~ x + x*z + log(sin(x) + 2) * 
> log(cos(z) + 2), and the data set my_data and formula can have any number of 
> variables like x1, x2, ...., x1000. So when you write the code for the value 
> substitution in the second step, you cannot know which function and what 
> variables you will have.
> 
> So in Julia or R, I need a function or macro F(f, [....]) that does this: 
> given a function f, whose format is the input from the user, and a set of 
> variable values [...], whose number and names are also the input from the 
> user, F(f, [...]) returns the value of f evaluated at the values [...]. For 
> example, the user inputs
> 
> f = 1 + z + cos(x)*log(2+cos(z))/(2+sin(x))
> 
> and [x = 2.3, z = 1.4],
> 
> F should return the value of f evaluated at x = 2.3 and z = 1.4.
> 
> This can be done in R, see "margins" function in PivotalR, which actually 
> does big data computation in-database. The problem is how to do the same 
> thing in Julia?
> 
> Hope my explanation makes my question clearer.
> 
> On Saturday, February 1, 2014 2:35:38 PM UTC-8, Jameson wrote:
> You need to provide more detail on what you are trying to do with this. You 
> seem 
> to be confusing several concepts involving the usage of expressions, 
> macros, and functions. I can't tell if you are trying to write special 
> syntax, or are just unaware of anonymous functions: 
> 
> Mostly, why is :(sin(x) + cos(y) * sin(z)) an expression, and not a 
> function? It seems like you perhaps have an R background? 
> 
> f(x,y,z) = (sin(x) + cos(y) * sin(z)) 
> f(1,2,3) 
> 
> On Sat, Feb 1, 2014 at 12:04 PM, Walking Sparrow <[email protected]> wrote: 
> > So the real question is how to generate a code block like this 
> > 
> > quote 
> >     x = 2 
> >     y = 3 
> >     ..... 
> >     x + y + .... 
> > end 
> > 
> > Need to embed a for loop inside the macro definition? 
> > 
> > 
> > 
> > On Saturday, February 1, 2014 8:52:30 AM UTC-8, Walking Sparrow wrote: 
> >> 
> >> Please forgive me if this is a stupid question. Suppose I have an 
> >> expression 
> >> 
> >> :(sin(x) + cos(y) * sin(z)) 
> >> 
> >> and the values of x, y, z. 
> >> 
> >> How can I write a macro that can substitute the values of x, y, z into the 
> >> above expression? The number of values that I want to substitute depends 
> >> on 
> >> the actual use cases and thus is unknown. 
> >> 
> >> I wrote a function that can do this 
> >> 
> >> function substitute(expr::Expr, vals::Array{Expr,1}) 
> >>     for i = 1:length(vals) 
> >>         @eval $(vals[i]) 
> >>     end 
> >>     @eval $expr 
> >> end 
> >> 
> >> x = 10 
> >> y = 23 
> >> 
> >> substitute(:(x+y), [:(x = 2), :(y = 3)]) 
> >> 
> >> x 
> >> y 
> >> 
> >> But if you run the above code, you will see that the values of global x 
> >> and y are changed, which is not what I intend to do. This is because 
> >> "eval" 
> >> does the evaluation in the global scope. Besides, I think it is a bad 
> >> coding 
> >> pattern to use eval and it is slow. 
> >> 
> >> It would be better if this can be done using macro. But I have no idea 
> >> about how to do this. 

Reply via email to