Package 'weighter'

Title: Model averaging
Description: This package implementes model averaging.
Authors: Sangeeta Bhatia [aut, cre], Anne Cori [aut], Pierre Nouvellet [aut]
Maintainer: Sangeeta Bhatia <[email protected]>
License: MIT + file LICENSE
Version: 0.0.1
Built: 2024-10-21 02:45:50 UTC
Source: https://github.com/mrc-ide/weighter

Help Index


Mapping of groupvars to model

Description

Mapping of groupvar to models run

Usage

groupvar_to_model(pred, groupvars)

Arguments

pred

data.frame with at least one column called model

groupvars

quoted variable names used to define groups within which models should be ranked.

Details

In some cases, we might not have the same set of models run for a grouping variable. For example, say the grouping variable is country, and we have Models m1m1, m2m2 and m3m3 run for country c1c1, and models m1,m2,m3,m4m1, m2, m3, m4 run for country c2c2. For this example, groupvar_to_model returns a named list where names are formed by concatenating models and elements are countries for which a particular set of models has been run. (m1_m2_m3 = c1, m1_m2_m3_m4 = c2)

Value

list

Author(s)

Sangeeta Bhatia


Assign ranks to models

Description

Rank models according to the error

Usage

model_ranks(pred, groupvars, errvar)

Arguments

pred

data.frame with at least one column called model

groupvars

quoted variable names used to define groups within which models should be ranked.

errvar

quoted column name that contains the metric used to rank models

Details

When multiple models have made predictions, we want to rank them by their error, where error can be any metric e.g. Root mean squared error or likelihood. Each model is ranked within the groups defined by groupvars

Value

data.frame with the same structure as pred and an extra column called rank which contains the model ranks within the groups defined by groupvar

Author(s)

Sangeeta Bhatia

Examples

pred <- data.frame(
  models = as.factor(c("a", "b", "c", "a", "b", "c")),
  country = c("C1", "C1", "C1", "C2", "C2", "C2"),
  error = c(6L, 1L, 3L, 4L, 7L, 9L),
  date = c("1", "1", "1", "2", "2", "2"),
  stringsAsFactors = FALSE
)
model_ranks(pred, c("country", "date"), "error")

Mapping of models to groups for which they have been run

Description

Mapping of models to groups for which they have been run

Usage

model_to_groupvar(pred, groupvars)

Arguments

pred

data.frame with at least one column called model

groupvars

quoted variable names used to define groups within which models should be ranked.

Details

In some cases, we might not have the same set of models run for a grouping variable. For example, say the grouping variable is country, and we have Models M1M_1, M2M_2 and M3M_3 run for country C1C_1, and models M1,M2,M3,M4M_1, M_2, M_3, M_4 run country C2C_2. For this example, this function will return the list m_1 = c_1, c_2, m_2 = c_1, c_1, m_3 = c_1, c_2, m_4 = c_2 This can be used to identify for which countries a give model has been run.

Value

list

Author(s)

Sangeeta Bhatia


Compute Model Weights

Description

Compute Model Weights

Usage

model_weights(pred, groupvars, errvar)

Arguments

pred

data.frame with at least one column called model

groupvars

quoted variable names used to define groups within which models should be ranked.

errvar

quoted column name that contains the metric used to rank models

Details

Compute model weights within each group

Value

named list where the names are the combinations of models run for a group, and each element of the list is a data.frame containing model weights outputted from model_weights_in_group

Author(s)

Sangeeta Bhatia

See Also

model_weights_in_group


Compute Model Weights

Description

Compute model weights from model ranks

Usage

model_weights_in_group(pred, groupvars = "model", rankvar = "rank")

Arguments

pred

data.frame that has at least columns specified via parameters groupvars and rank. pred is expected to have the same number of elements of grouping variable within each group. That is, each model should have been run for the same number of elements of groupvar. If this is not the case, use model_weights instead.

groupvars

quoted variable names used to define groups within which model weights are computed. Defaults to model.

rankvar

quoted variable names that contain model ranks.

Details

In a group defined by groupvars, the weight of a model is defined as

weight(M)=i=1MK(i)/iweight(M) = \sum\limits_{i = 1}^{M}{K(i) / i}

where K(i)K(i) is the number of times model M is ranked ii among M1,M2,MM_1, M_2, \dots M models.

Value

a data.frame with columns model and weight where weight is the unnormalised weight of the model

Author(s)

Sangeeta Bhatia

See Also

model_ranks model_weights

Examples

pred <- data.frame(
  model = as.factor(c("a", "b", "c", "a", "b", "c")),
  country = c("C1", "C1", "C1", "C2", "C2", "C2"),
  error = c(6L, 1L, 3L, 4L, 7L, 9L),
  date = c("1", "1", "1", "2", "2", "2"),
  stringsAsFactors = FALSE
)
ranked <- model_ranks(pred, c("country", "date"), "error")
model_weights_in_group(ranked)