Grouped lag
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
Start by reading src/temporal_terms.jl and the proposed GroupedLagTerm implementation in this issue. Compare the existing lagged-term behavior with the grouped panel-data example, then clarify the intended handling of time indices, dependencies, missing values, and lead terms before deciding what a completed feature should include.
Written by the indexing model from the issue text.
Description
As was discussed in #109 it would be helpful to have more powerful lead/lag terms.
I work with panel data where the time-index is nice enough. I only need lag applied groupwise.
I wonder whether there is interest to have this in this package. It is very similar to the code in src/temporal_terms.jl. It has the implemented version of lagged terms as a special case.
If desired I can generalize to lead/lag and make a PR. But I suspect
- you aim at a version that also deals with time indices,
- you don't want to take a dependency on
DataFrames.jl. (Do you know of another package to do the groupby operation?)
I think it would be useful to have this because this cannot easily done differently. Missing time indices on the other hand can be dealt with by pre-processing the data.
cc @oxinabox who opened to references issue and @Nosferican who brought this up in a comment
using StatsModels
using ShiftedArrays
using DataFrames, Underscores
struct GroupedLagTerm{T,G} <: AbstractTerm
term::T
nsteps::Int
groups::G
end
groupedlag(t, n, g) = GroupedLagTerm(t, n, g)
GroupedLagTerm(term, nsteps) = GroupedLagTerm(term, nsteps, term(1))
StatsModels.terms(t::GroupedLagTerm) = (t.term, t.groups)
function StatsModels.apply_schema(t::FunctionTerm{typeof(groupedlag)},
sch::StatsModels.Schema,
Mod::Type)
term_parsed, nsteps_parsed, groups_parsed = t.args_parsed
nsteps = nsteps_parsed.n
term = apply_schema(term_parsed, sch, Mod)
groups = apply_schema(groups_parsed, sch, Mod)
return GroupedLagTerm(term, nsteps, groups)
end
# apply_schema to internal Terms and check for proper types
function StatsModels.apply_schema(t::GroupedLagTerm,
sch::StatsModels.Schema,
Mod::Type)
term = apply_schema(t.term, sch, Mod)
groups = apply_schema(t.groups, sch, Mod)
GroupedLagTerm(term, t.nsteps, groups)
end
function StatsModels.modelcols(ft::GroupedLagTerm, d::NamedTuple)
column, groups = modelcols(terms(ft), d)
df = DataFrame(column=column, groups=vec(groups))
@_ groupby(df, :groups) |>
transform!(__, :column => (x -> lag(x, ft.nsteps)) => :column_lagged) |>
__.column_lagged
end
StatsModels.width(ft::GroupedLagTerm) = 1
StatsModels.coefnames(ft::GroupedLagTerm) = "glag(" .* coefnames(ft.term) .* ", $(ft.n))"
StatsModels.termvars(p::GroupedLagTerm) = StatsModels.termvars(p.term)
This is how it is used
df = DataFrame(y = randn(10), x = randn(10), grp = rand(["a", "b"], 10))
f0 = @formula(y ~ x + grp + groupedlag(x, 1, 1))
sch = schema(f0, df)
f = apply_schema(f0, sch)
modelcols(f, df)
using GLM
lm(f0, df) # this doesn't work because of the introduced missings
- Dominant language
- Julia
- Stars
- 252
- Forks
- 36
- Avg merge
- 3h 14m
- Merged PRs (30d)
- 4
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from JuliaStats/StatsModels.jl
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
JuliaStats/StatsModels.jl#328 · 1 comment ·
-
Cell mean coding?Open
Difficulty 4/5 3-5 days Newbie friendliness 48/100
JuliaStats/StatsModels.jl#351 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
JuliaStats/StatsModels.jl#329 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
JuliaStats/StatsModels.jl#318 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
JuliaStats/StatsModels.jl#316 ·
All issues in JuliaStats/StatsModels.jl
Similar issues
-
Affine transformations of `JohnsonSU` could return a `JohnsonSU`Possibly taken @devmotion claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JuliaStats/Distributions.jl#2117 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
TomerAberbach/profiler-md#970 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
grame-cncm/faust#1409 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
SciML/PETScDiffEq.jl#521 ·
Maintainers usually reply within 1 day