sim_goal_weights | R Documentation |
Adds numeric weights to a set of goals (usually a single subject's set of goals).
sim_goal_weights( goals, weight_type = c("unweighted", "preference", "treatment") )
goals |
A data frame with a row for each goal. |
weight_type |
The type of weights to apply. See 'Details'. |
There are a few valid options to the weight_type
argument, including
"unweighted", "preference", and "treatment".
The "unweighted" type is the default and is the simplest:
it sets all goal weights to 1.
The "preference" type randomly randomly samples (without replacement)
integer weights from 1 to n
. For instance, a subject with n
= 3 goals
will have weights 1, 2, and 3 randomly assigned to their goals.
The "treatment" type computes a weight biased by the treatment effect, and
so requires a numeric treatment_fe
column. These weights are calculated
as n * treatment_fe / sum(treatment_fe)
.
The same goals
data frame with the added goal_weight
column.
library(dplyr) library(tidyr) library(purrr) # Generate some subjects and goals d <- sim_subjects(n_subjects = 40) %>% sim_goals(n_goals_range = c(1, 3)) # Apply weights in one of two ways # Using purrr::map() d %>% mutate( goals = map( goals, sim_goal_weights ) ) # Or using tidyr::unnest() on the goals column d %>% unnest(goals) %>% group_by(subject_id) %>% sim_goal_weights(weight_type = "preference") # The "treatment" goal weights require a treatment_fe column d %>% unnest(goals) %>% group_by(subject_id) %>% sim_treatment_effect(delta = 0.4) %>% sim_goal_weights(weight_type = "treatment")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.