R/weights.R
Get or set ID variable level weights.
get_levels(x, id) get_weights(x, id) set_weights(x, id, weights)
x | rvtable. |
---|---|
id | character, ID column name in |
weights | list, data frame, or |
a list for get_weights
and get_levels
.
Weights are important for marginalizing distributions, when collapsing an rvtable over levels of an ID
variable must account for known unequal weighting of levels. Weights are strictly used by marginalize
.
get_weights
get a list of 2-column data frames of levels and associated weights for ID variables in an rvtable.
set_weights
sets weights for levels of ID variables in an rvtable.
Weights for levels of ID variables in an rvtable are assumed to be equal unless explicitly set otherwise.
They are usually set via set_weights
but can also be set as part of a call to the rvtable
constructor.
If weights=NULL
, id
is ignored and all ID variables' levels are set to equal weighting.
If id
refers to a single ID variable in x
, then weights
can be a valid weights data frame.
In general, weights
is a list of data frames corresponding to the variables specified id
.
If weights
is a named list, the named map to id
. If unnamed, they must be provided in the same order as id
.
Equal weights are set for an individual ID variable's levels
when a list element in weights
correpsonding to an id
value is NULL
.
get_levels
returns a list of levels for ID variables in x
, a subset of the information returned by get_weights
When an rvtable contains no ID variables, there are no levels or weights.
x <- rvtable(data.frame( id1=rep(LETTERS[1:5], each=4), id2=factor(c("low", "high")), id3=rep(1:2, each=2), Val=rep(1:10, each=20), Prob=rep(sqrt(1:10), each=20))) get_levels(x)#> $id1 #> [1] "A" "B" "C" "D" "E" #> #> $id2 #> [1] "high" "low" #> #> $id3 #> [1] 1 2 #>get_weights(x)#> $id1 #> # A tibble: 5 x 2 #> levels weights #> <chr> <dbl> #> 1 A 1.00 #> 2 B 1.00 #> 3 C 1.00 #> 4 D 1.00 #> 5 E 1.00 #> #> $id2 #> # A tibble: 2 x 2 #> levels weights #> <chr> <dbl> #> 1 high 1.00 #> 2 low 1.00 #> #> $id3 #> # A tibble: 2 x 2 #> levels weights #> <int> <dbl> #> 1 1 1.00 #> 2 2 1.00 #>get_weights(x, "id1")#> $id1 #> # A tibble: 5 x 2 #> levels weights #> <chr> <dbl> #> 1 A 1.00 #> 2 B 1.00 #> 3 C 1.00 #> 4 D 1.00 #> 5 E 1.00 #>x <- set_weights(x, "id1", data.frame(levels=LETTERS[1:5], weights=1:5)) get_weights(x, c("id1", "id3"))#> $id1 #> levels weights #> 1 A 1 #> 2 B 2 #> 3 C 3 #> 4 D 4 #> 5 E 5 #> #> $id3 #> # A tibble: 2 x 2 #> levels weights #> <int> <dbl> #> 1 1 1.00 #> 2 2 1.00 #>wts <- data.frame(levels=1:2, weights=c(0.3, 0.7)) x <- set_weights(x, c("id1", "id3"), list(id1=NULL, id3=wts)) get_weights(x, c("id1", "id3"))#> $id1 #> # A tibble: 5 x 2 #> levels weights #> <chr> <dbl> #> 1 A 1.00 #> 2 B 1.00 #> 3 C 1.00 #> 4 D 1.00 #> 5 E 1.00 #> #> $id3 #> levels weights #> 1 1 0.3 #> 2 2 0.7 #>wts <- data.frame(levels=levels(x$id2), weights=c(2, 1)) x <- set_weights(x, c("id1", "id2"), list(NULL, wts)) get_weights(x)#> $id1 #> # A tibble: 5 x 2 #> levels weights #> <chr> <dbl> #> 1 A 1.00 #> 2 B 1.00 #> 3 C 1.00 #> 4 D 1.00 #> 5 E 1.00 #> #> $id2 #> levels weights #> 1 high 2 #> 2 low 1 #> #> $id3 #> levels weights #> 1 1 0.3 #> 2 2 0.7 #>