Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify forumulae #215

Closed
azev77 opened this issue Feb 9, 2021 · 4 comments
Closed

Simplify forumulae #215

azev77 opened this issue Feb 9, 2021 · 4 comments

Comments

@azev77
Copy link

azev77 commented Feb 9, 2021

Hey and thank you for this package!
I have a Diff-In-Diff w/ many hand-made variables:

m_dyndid = reg(df, @formula(hpg ~
    t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 +
    t11 + t12 + t13 + t14 + t15 + t16 + t17 + t18 + t19 + t20 +
    t21 + t22 + t23 + t24 + t25 + t26 + t27 + t28 + t29 + t30 +
    t31 + t32 + t33 + t34 + t35 + t36 + t37 + t38 + t39 + t40 +
    t41 + t42 + t43 + t44 + t45 + t46 + t47 + t48 + t49 + t50 +
    t51 + t52 + t53 + t54 + t55 + t56 + t57 + t58 + t59 + t60 +
    t61 + t62 + t63 + t64 + t65 + t66 + t67 + t68 + t69 + t70 +
    t71 + t72 + t73 + t74 + t75 + t76 + t77 + t78 + t79 + #t80 +
    t81 + t82 + t83 + t84 + t85 + t86 + t87 + t88 + t89 + t90 +
    t91 + t92 + t93 + t94 + t95 + t96 + t97 + t98 + t99 + t100 +
    t101 + t102 + t103 + t104 + t105 + t106 + t107 + t108 + t109 + t110 +
    t111 + t112 + t113 + t114 + t115 + t116 + t117 + t118 + t119 + t120
    + fe(loc) + fe(Date))
    )

Can this be automated like in STATA?

m_dyndid = reg(df, @formula(hpg ~ t1 - t79 + t81 - t120 + fe(loc) + fe(Date))   )
@palday
Copy link
Member

palday commented Feb 9, 2021

This could be done programmatically, e.g.

julia> varnames = map(x -> "t$x", [1:79; 81:120])
julia> fe = x -> x # define dummy FE
julia> form = term(:hpg) ~ sum(term.(varnames)) + @formula(0~fe(loc) + fe(Date)).rhs

The handling of fe could be improved, but that's part of some package-level extension to the formula syntax, so I don't know the best way to construct those terms off the top of my head.

You could also define an operator / FunctionTerm at the package level that does that type of expansion and returns a matrix term. This is how random effects are handled in MixedModels.jl.

@azev77
Copy link
Author

azev77 commented Feb 9, 2021

@palday thank you.
@matthieugomez this works w/ reg() but not w/ fegife

aa=unique(sort(df.Date))    # 120 months 2010-2020
for jj in 1:size(aa,1)
    col = "t" * string(jj)           #string(jj) # string(aa[jj])
    df[!, col] = (df.loc .== "Greater_Vancouver") .* (df.Date .== aa[jj])
end
PT = map(x -> "t$x", [1:79; 81:120]) 
f = term(:hpg) ~ sum(term.(PT)) + @formula(0~fe(loc) + fe(Date)).rhs

m_dyndid1 = reg(df, f, Vcov.cluster(:loc))  #works
m_ife     = regife(df, f, Vcov.cluster(:loc))  #ERROR: type Nothing has no field id

@matthieugomez
Copy link
Contributor

I guess one question is why don’t you simply create a categorical variable for dates? Then simply add the variable in the formula. You can specify the base level using keyword argument.

@palday palday closed this as completed Apr 23, 2021
@palday
Copy link
Member

palday commented Apr 23, 2021

This seems resolved/like an issue for FiexedEffectsModels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants