Grouped lag
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 30/100
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di JuliaStats/StatsModels.jl
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 68/100
JuliaStats/StatsModels.jl#328 · 1 commento ·
-
Cell mean coding?Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
JuliaStats/StatsModels.jl#351 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
JuliaStats/StatsModels.jl#329 · 2 commenti ·
-
Remove term from formulaAperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
JuliaStats/StatsModels.jl#318 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
JuliaStats/StatsModels.jl#316 ·
Tutte le issue di JuliaStats/StatsModels.jl
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
JuliaGPU/KernelAbstractions.jl#897 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
oscar-system/GAP.jl#1455 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
JuliaAI/MLJTransforms.jl#85 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
SciML/OrdinaryDiffEq.jl#4875 ·
I maintainer di solito rispondono entro 4 giorni