margins icon indicating copy to clipboard operation
margins copied to clipboard

Unit Specific Variances Possible Speed-Up

Open EdJeeOnGitHub opened this issue 5 years ago • 5 comments

Do unit specific variances in build_margins.R strictly need to be calculated row-by-row?

I've been calculating SEs in blocks of 10,000 manually since the default implementation was taking too long - my case might be unique as I already know the gradient function for every row but if it isn't I'm happy to try and speed this section up.

Apologies if I'm missing something and this is the only way to deal with calculating the SEs.

EdJeeOnGitHub avatar Feb 06 '19 22:02 EdJeeOnGitHub

There should definitely be ways to improve this significantly. I intended to improve but then discovered most people don't seem to need these, so I turned it off by default (to improve overall speed) and haven't received a lot of requests for improvement. If you have ideas for how to update, I'm open to suggestions and PRs.

leeper avatar Feb 07 '19 10:02 leeper

Currently I'm calculating the Jacobian symbolically and then just applying Jacobian %*% vcov %*% Jacobian' in blocks of 10,000 and taking the square root of the diagonal.

I know we can't use the symbolic differentiation in margins but I imagine just finding the Jacobian row by row and then using the vectorised/block matrix method will offer a speed up over the current method.

If this all sounds sensible to you I'll give it a go.

EdJeeOnGitHub avatar Feb 08 '19 19:02 EdJeeOnGitHub

Go for it!

leeper avatar Feb 09 '19 19:02 leeper

Hi, I forked the repo and implemented the method above but I couldn't get any significant speed-ups - it looks like calculating the Jacobian row-by-row is by far the slowest part of unit_ses = TRUE so vectorising the the Jacobian sandwich separately afterwards doesn't help much.

If I can think of any way to speed up the Jacobian calculation with unit-specific standard errors I'll give it another shot but for now I don't think this is going anywhere.

EdJeeOnGitHub avatar Feb 17 '19 15:02 EdJeeOnGitHub

Bummer. Definitely let me know if you come up with something better. I'll leave this open.

leeper avatar Feb 17 '19 15:02 leeper