Grouped lag

Open
#182 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
julia
Domain
data

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaStats/StatsModels.jl

All issues in JuliaStats/StatsModels.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.