Add a cumulative membership effect to a data.frame
Source: R/model-design-tools.R
enw_add_cumulative_membership.Rd
This function adds a cumulative membership effect to a data
frame. This is useful for specifying models such as random walks (using
rw()
) where these features can be used in the design matrix with the
appropriate formula. Supports grouping via the optional .group
column.
Note that cumulative membership is indexed to start with zero (i.e. the
first observation is assigned a cumulative membership of zero).
Value
A data.frame
with a new columns cfeature$
that contain the
cumulative membership effect for each value of feature
. For example if the
original feature
was week
(with numeric entries 1, 2, 3
) then the new
columns will be cweek1
, cweek2
, and cweek3
.
See also
Functions used to formulate models
enw_add_pooling_effect()
,
enw_design()
,
enw_effects_metadata()
,
enw_one_hot_encode_feature()
Examples
metaobs <- data.frame(week = 1:2)
enw_add_cumulative_membership(metaobs, "week")
#> week .group cweek2
#> <int> <num> <num>
#> 1: 1 1 0
#> 2: 2 1 1
metaobs <- data.frame(week = 1:3, .group = c(1,1,2))
enw_add_cumulative_membership(metaobs, "week")
#> week .group cweek2 cweek3
#> <int> <num> <num> <num>
#> 1: 1 1 0 0
#> 2: 2 1 1 0
#> 3: 3 2 0 1