makeWeight | R Documentation |
This function allows you to generate a weight variable by supplying a set of categorical variables and the target distribution for each of the variables' categories. Weights are computed by iteratively 'raking' conditional 'cells' to the provided marginal targets.
makeWeight(..., name)
... |
A series of expressions of the form |
name |
The name of the resulting variable |
For instance, if you wanted to create a
weight variable which equally weighted four categories stored in ds$var
you would call ds$weight1 <- makeWeight(ds$var ~ c(25, 25, 25, 25), name = "weight1")
.
Note that makeWeight
returns a VariableDefinition
, an expression that
when assigned into your Dataset becomes a derived variable. This does not on
its own set the new variable as "the weight" for your dataset. To set that
attribute, use weight()
. Alternatively, you can also create the variable
and set the weight attribute in one step with
weight(ds) <- makeWeight(ds$var ~ c(25, 25, 25, 25), name = "weight1")
.
A crunch VariableDefinition()
of the weight variable
weight<-()
; settings()
for the "default weight" for other
dataset viewers.
## Not run:
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)
ds <- newDataset(mtcars)
# Create a new "raked" variable
ds$weight <- makeWeight(ds$cyl ~ c(30, 30, 40, 0),
ds$gear ~ c(20, 20, 60, 0),
name = "weight"
)
summary(ds$weight)
# ds$weight is not "the weight" for the dataset unless you set it:
weight(ds) <- ds$weight
# Or, you can create the variable and set as weight in one step:
weight(ds) <- makeWeight(ds$var ~ c(25, 25, 25, 25), name = "weight2")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.