Stream: helpdesk (published)

Topic: marginaleffects calculation (similar to R)


view this post on Zulip Edvin (May 14 2024 at 10:50):

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, to simply calculate the marginal effect of hp for every unit in the dataset.

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.

view this post on Zulip Nils (May 14 2024 at 17:41):

Not sure I understand your question - are you just asking for how to do the partial_derivative function for every variable in the model? That would just be partial_derivative(data, model, eps, var) end than replace var with whatever variable you're interested in, or use the StatsModels API to extract all RHS variables and vall that function on all of them.

view this post on Zulip Edvin (May 14 2024 at 18:15):

Not so much asking for ways to expand this function. But more so interested if there's some alternative implementation that would be faster/scale better with more variables and data.

view this post on Zulip Edvin (May 14 2024 at 18:17):

I was looking through the code for Effects.jl but couldn't really understand the implementation they are using there.

view this post on Zulip Edvin (May 15 2024 at 06:37):

Thanks for answering Nils, let's continue on the discourse (i xposted) there: https://discourse.julialang.org/t/how-to-implement-r-like-marginaleffects/114260


Last updated: Nov 06 2024 at 04:40 UTC