-
Notifications
You must be signed in to change notification settings - Fork 46
/
deprecated.jl
156 lines (135 loc) · 4.95 KB
/
deprecated.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
##############################################################################
##
## Old one
##
##
##############################################################################
function oldparse_fixedeffect(df::AbstractDataFrame, feformula::FormulaTerm)
fe = FixedEffect[]
id = Symbol[]
for term in eachterm(feformula.rhs)
result = oldparse_fixedeffect(df, term, feformula)
if result != nothing
push!(fe, result[1])
push!(id, result[2])
end
end
return fe, id
end
# Constructors from dataframe + Term
function oldparse_fixedeffect(df::AbstractDataFrame, a::Term, feformula::FormulaTerm)
v = df[!, Symbol(a)]
if isa(v, CategoricalVector)
return FixedEffect(v), Symbol(a)
else
# x from x*id -> x + id + x&id
if !any(isa(term, InteractionTerm) & (a ∈ terms(term)) for term in eachterm(feformula.rhs))
error("The term $(a) in fe= is a continuous variable. Convert it to a categorical variable using 'categorical'.")
end
end
end
# Constructors from dataframe + InteractionTerm
function oldparse_fixedeffect(df::AbstractDataFrame, a::InteractionTerm, feformula::FormulaTerm)
factorvars, interactionvars = _split(df, a)
if !isempty(factorvars)
# x1&x2 from (x1&x2)*id
fe = FixedEffect((df[!, v] for v in factorvars)...; interaction = old_multiply(df, interactionvars))
id = old_name(Symbol.(terms(a)))
return fe, id
end
end
function _split(df::AbstractDataFrame, a::InteractionTerm)
factorvars, interactionvars = Symbol[], Symbol[]
for s in terms(a)
s = Symbol(s)
isa(df[!, s], CategoricalVector) ? push!(factorvars, s) : push!(interactionvars, s)
end
return factorvars, interactionvars
end
function old_multiply(df, ss::Vector{Symbol})
if isempty(ss)
out = Ones(size(df, 1))
else
out = ones(size(df, 1))
for j in eachindex(ss)
old_multiply!(out, df[!, ss[j]])
end
end
return out
end
function old_multiply!(out, v)
for i in eachindex(out)
if v[i] === missing
# may be missing when I remove singletons
out[i] = 0.0
else
out[i] = out[i] * v[i]
end
end
end
function old_name(s::Vector{Symbol})
if isempty(s)
out = nothing
else
out = Symbol(reduce((x1, x2) -> string(x1)*"x"*string(x2), s))
end
return out
end
struct ModelTerm
f::FormulaTerm
dict::Dict{Symbol, Any}
end
ModelTerm(f::FormulaTerm; kwargs...) = ModelTerm(f, Dict(pairs(kwargs)...))
function Base.show(io::IO, m::ModelTerm)
println(io, m.f)
for (k, v) in m.dict
println(io, k, ": ", v)
end
end
import StatsModels: capture_call
macro model(ex, kws...)
@warn "@model is deprecated, please use @formula"
f = StatsModels.terms!(StatsModels.sort_terms!(StatsModels.parse!(ex)))
d = Dict{Symbol, Any}()
for kw in kws
isa(kw, Expr) && kw.head== :(=) || throw("All arguments of @model, except the first one, should be keyboard arguments")
if kw.args[1] == :fe
@warn "The keyword argument fe is deprecated. Instead of @model(y ~ x, fe = state + year), write @formula(y ~ x + fe(state) + fe(year))"
d[:feformula] = kw.args[2]
elseif kw.args[1] == :ife
@warn "The keyword argument ife is deprecated. Instead of @model(y ~ x, ife = (state + year, 2)), write @formula(y ~ x + ife(state, year, 2))"
d[:ifeformula] = kw.args[2]
elseif kw.args[1] == :vcov
d[:vcovformula] = kw.args[2]
@warn "The keyword argument vcov is deprecated. Instead of reg(df, @model(y ~ x, vcov = cluster(State))), write reg(df, @formula(y ~ x), Vcov.cluster(:State))"
elseif kw.args[1] == :subset
d[:subsetformula] = kw.args[2]
@warn "The keyword argument subset is deprecated. Instead of reg(df, @model(y ~ x, subset = State .>= 30), write reg(df, @formula(y ~ x), subset = df.State .>= 30))"
elseif kw.args[1] == :weight
d[:weight] = kw.args[2]
@warn "The keyword argument weight is deprecated. Instead of reg(df, @model(y ~ x, weight = Pop), write reg(df, @formula(y ~ x), weight = :Pop)"
else
d[kw.args[1]] = kw.args[2]
end
end
:(ModelTerm($f, $d))
end
function evaluate_subset(df, ex::Expr)
if ex.head == :call
return Expr(ex.head, ex.args[1], (evaluate_subset(df, ex.args[i]) for i in 2:length(ex.args))...)
else
return Expr(ex.head, (evaluate_subset(df, ex.args[i]) for i in 1:length(ex.args))...)
end
end
evaluate_subset(df, ex::Symbol) = df[!, ex]
evaluate_subset(df, ex) = ex
function reg(df, m::ModelTerm;kwargs...)
reg(df, m.f; m.dict..., kwargs...)
end
function partial_out(df, m::ModelTerm; kwargs...)
partial_out(DataFrame(df), m.f; m.dict..., kwargs...)
end
function fes(args...)
@warn "fes() is deprecated. Use fe()"
fe(args...)
end