Learn R Programming

popEpi (version 0.4.3)

makeWeightsDT: Make a data.table of Tabulated, Aggregated Values and Weights

Description

An internal function that aggregates a table and merges in weights.

Usage

makeWeightsDT(data, values = NULL, print = NULL, adjust = NULL,
  formula = NULL, Surv.response = TRUE, by.other = NULL,
  custom.levels = NULL, custom.levels.cut.low = NULL, weights = NULL,
  internal.weights.values = NULL, enclos = NULL, NA.text = NULL)

Arguments

data

DF/DT; passed to envir in eval

values

values to tabulate. Anything evalPopArg can evaluate.

print

variables to tabulate by and include in prVars in attributes

adjust

variables to tabulate by and include in adVars in attributes

formula

a formula such as fot ~ sex or Surv(fot, lex.Xst) ~ sex

Surv.response

logical, if TRUE throws error if response in formula is not a Surv object and vice versa

by.other

other variables to tabulate by and include in boVars in attributes

custom.levels

a named list of values. When "inflating" the data in the cross-join / cartesian join sense (try e.g. merge(1:5, 1:2)), one can supply the levels to inflate by using this to ensure inflation is full. E.g. data might only have levels present to do inflation analogous to merge(2:5, 1:2) although merge(1:5, 1:2) is intended and needed.

custom.levels.cut.low

a character string vector of variable names. These variables mentioned in custom.levels and existing in data or first modified (in data) using cutLow() (essentially cut() with right = FALSE and returning the lower bounds as values). Handy for aggregating data e.g. to survival intervals. NOTE: the appropriate elements in custom.levels for these variables must exceptionally contain an extra value as the roof used in cutting, which will not be used in "inflating" the table using a merge. See Examples.

weights

a named list or long-form data.frame of weights. See Examples.

internal.weights.values

the variable to use to compute internal weights; only used if weights = "internal".

enclos

the enclosing environment passed on to eval. Variables not found in data or searched for here.

NA.text

a character string to display in a warning if there are any rows with missing values or adjust values. special: key phrase %%NA_COUNT%% in text is replaced with the count of missing observations. E.g. "Missing %%NA_COUNTS%% observations due to derpness."

Examples

Run this code
# NOT RUN {
library(survival)
library(data.table)

makeWeightsDT <- popEpi:::makeWeightsDT ## this avoids errors during tests

sire <- copy(popEpi::sire)
set.seed(1L)
sire$sex <- rbinom(nrow(sire), 1, 0.5)
ag <- lexpand(sire, birth = "bi_date", entry = "dg_date", exit = "ex_date",
              status = status %in% 1:2, pophaz = popmort, pp = FALSE,
              aggre = list(sex, agegr = cut(dg_age, c(0,50,75,Inf)), fot), 
              fot = seq(0, 5, 1/12))
ps <- quote(list(sex, fot))
as <- quote(list(agegr))
vs <- list(quote(list(pyrs, at.risk)))
ws <- list(agegr = c(0.2,0.4,0.4))

#### custom.levels usage
fb <- seq(0, 5-1/12, 1/12) ## exclude 5 as no row has that value
ag2 <- ag[fot > 0.5,]
# repeats fot intervals < 0.5 as empty rows
# may be the safest way to do this
dt <- makeWeightsDT(ag2, print = ps, adjust = as, 
                    values = vs, weights = ws,
                    custom.levels = list(fot = fb))
## aggregate from intervals seq(0, 5, 1/12) to 0:5
fb2 <- 0:5 ## (this time we include 5 as the roof)       
dt <- makeWeightsDT(ag2, print = ps, adjust = as, 
                    values = vs, weights = ws,
                    custom.levels = list(fot = fb2),
                    custom.levels.cut.low = "fot")              
                    

#### use of enclos
TF <- environment()
gender <- factor(ag$sex)
dt <- makeWeightsDT(ag, print = quote(gender), adjust = as, 
                    values = vs, weights = ws, enclos = TF)
## or NULL: uses calling frame by default.
dt <- makeWeightsDT(ag, print = quote(gender), adjust = as, 
                    values = vs, weights = ws,
                    enclos = NULL)
## passing parent.fram(1) is the same thing (as below),
## but won't pass in testing these examples somehow (but work in real life)
# dt <- makeWeightsDT(ag, print = quote(gender), adjust = as, 
#                     values = vs, weights = ws,
#                     enclos = NULL)                  

#### formula usage
form <- Surv(fot, factor(from0to1))~gender
dt <- makeWeightsDT(ag, formula = form, Surv.response = TRUE,
                    adjust = as, values = vs, weights = ws,
                    enclos = NULL)
                    
## or
form <- Surv(fot, factor(from0to1))~gender + adjust(agegr)
dt <- makeWeightsDT(ag, formula = form, Surv.response = TRUE,
                    adjust = NULL, values = vs, weights = ws,
                    enclos = NULL)
                    
## or   
form <- from0to1 ~ fot + gender + adjust(agegr)
dt <- makeWeightsDT(ag, formula = form, Surv.response = FALSE,
                    adjust = NULL, values = vs, weights = ws,
                    enclos = NULL)            

form <- from0to1 ~ fot + adjust(agegr) + adjust(sex)
ws2 <- list(agegr = c(0.33, 0.33, 0.33), sex = c(0.5, 0.5))
dt <- makeWeightsDT(ag, formula = form, Surv.response = FALSE,
                    adjust = NULL, values = vs, weights = ws2,
                    enclos = NULL)

## international standard pops
ag <- lexpand(sire, birth = "bi_date", entry = "dg_date", exit = "ex_date",
              status = status %in% 1:2, pophaz = popmort, pp = FALSE,
              aggre = list(sex, agegr = cut(dg_age, c(seq(0, 85, 5), Inf)), fot), 
              fot = seq(0, 5, 1/12))
              
form <- from0to1 ~ fot + adjust(agegr)
dt <- makeWeightsDT(ag, formula = form, Surv.response = FALSE,
                    adjust = NULL, values = vs, weights = "world_1966_18of5",
                    enclos = NULL)
                    
form <- from0to1 ~ fot + adjust(agegr, sex)
dt <- makeWeightsDT(ag, formula = form, Surv.response = FALSE,
                    adjust = NULL, values = vs, 
                    weights = list(agegr = "nordic_2000_18of5", sex=c(1,1)),
                    enclos = NULL)
# }

Run the code above in your browser using DataLab