How to use zerocorr() while creating a formula?

First, while that works, it’s not the recommended way to create a formula programmatically; instead, it’s recommended to use sum(term.(effects)). EDIT: here’s the section in the StatsModels.jl docs on this: Modeling tabular data · StatsModels.jl

Second, that probably won’t work with zerocorr right now, because it’s not (easily) possible to create RE terms at run time. Programmatically Creating RE Terms · Issue #462 · JuliaStats/MixedModels.jl · GitHub

Third, keep in mind that zerocorr only makes sense for random effects where there’s more than one random term (intercept and one or more slopes). I don’t know what var2 is supposed to be in your example above, but if it’s not something like :(1 + a | g) then zerocorr wouldn’t make any sense anyway…