Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Grouped lag

Aperta
#182 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
30/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
julia
Ambito
data

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Julia
Stelle
250
Fork
35
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaStats/StatsModels.jl

Tutte le issue di JuliaStats/StatsModels.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.