# How to implement R-like marginaleffects?

**URL:** <https://discourse.julialang.org/t/how-to-implement-r-like-marginaleffects/114260>\
**Category:** New to Julia\
**Created:** [May 14, 2024, 5:06pm UTC](https://discourse.julialang.org/t/how-to-implement-r-like-marginaleffects/114260 "2024-05-14T17:06:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![edvin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edvin/32/50224_2.png) [@edvin](https://discourse.julialang.org/u/edvin)\
**Post date:** [May 14, 2024, 5:06pm UTC](https://discourse.julialang.org/t/how-to-implement-r-like-marginaleffects/114260/1 "2024-05-14T17:06:38Z")

</div>

Hi,

I’m interested in how to implement calculation of partial derivatives/marginal effects similar to the package marginaleffects in r.

I translated some code from the excellent [documentation](https://marginaleffects.com/vignettes/slopes.html), to simply calculate the marginal effect of hp for every unit in the dataset.

```julia
using GLM, RDatasets, DataFrames

mtcars = dataset("datasets", "mtcars")
rename!(mtcars, lowercase.(names(mtcars)))
lm_mod = glm(@formula(mpg ~ hp * cyl), mtcars, Normal())

partial_derivative = function(data, model, eps)
    d1 = transform(data, :hp => (x -> x .- eps / 2) => :hp)
    d2 = transform(data, :hp => (x -> x .+ eps / 2) => :hp)
    (p1, p2) = map(d -> predict(model, d), [d1, d2])
    round.((p2-p1)/eps, digits=2)
end

partial_derivative(mtcars, lm_mod, 1e-4)

```

What would be a reasonable way to implemented this in a way that could give me unit-level marginaleffects for every variable in the model efficiently?  
I have been looking at ForwardDiff, but haven’t been able to figure out how to do it.

Note this is crossposted on [zulip](https://julialang.zulipchat.com/#narrow/stream/274208-helpdesk-.28published.29/topic/marginaleffects.20calculation.20.28similar.20to.20R.29/near/438549401).

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [May 14, 2024, 5:18pm UTC](https://discourse.julialang.org/t/how-to-implement-r-like-marginaleffects/114260/2 "2024-05-14T17:18:01Z")

</div>

Check out [GitHub - beacon-biosignals/Effects.jl: Effects Prediction for Regression Models](https://github.com/beacon-biosignals/Effects.jl)

---

<div class="post-metadata">

**Author:** ![edvin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edvin/32/50224_2.png) [@edvin](https://discourse.julialang.org/u/edvin)\
**Post date:** [May 15, 2024, 6:42am UTC](https://discourse.julialang.org/t/how-to-implement-r-like-marginaleffects/114260/3 "2024-05-15T06:42:47Z")

</div>

Thank you!

I’m looking to understand how to implement this in a better (faster/cleaner) way.  
I was looking at the implementation in Effects earlier, but didn’t quite get how it was done.  
How can I implement something similar to my example above in a way that scales with more variables/data?

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [May 15, 2024, 1:57pm UTC](https://discourse.julialang.org/t/how-to-implement-r-like-marginaleffects/114260/4 "2024-05-15T13:57:08Z")

</div>

I’m the main author of Effects.jl, so if I knew how to make things faster/cleaner, I would have already done it. This is the core computation:

> <https://github.com/beacon-biosignals/Effects.jl/blob/d5d8fbb7210c37c8dd7bdec4cd6f878f3d336765/src/regressionmodel.jl#L126-L131>

`_difference_method!` is factored out into a separate function so that it can use gradients from ForwardDiff or analytic gradients for common link functions via package extensions.

---

<div class="post-metadata">

**Author:** ![edvin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edvin/32/50224_2.png) [@edvin](https://discourse.julialang.org/u/edvin)\
**Post date:** [May 16, 2024, 5:23am UTC](https://discourse.julialang.org/t/how-to-implement-r-like-marginaleffects/114260/5 "2024-05-16T05:23:17Z")

</div>

Thanks! I’ll take a closer look at this.
