This is typically the second step of an analysis using phenotype risk scores, the next is getScores().

## Usage

getWeights(
demos,
phecodeOccurrences,
method = c("prevalence", "logistic", "cox", "loglinear"),
methodFormula = NULL,
dopar = FALSE
)

## Arguments

demos

A data.table having one row per person in the cohort. Must have a column person_id. When the cox method is used, demos must have columns first_age and last_age corresponding to first and last age of visit (in years).

phecodeOccurrences

A data.table of phecode occurrences for each person in the cohort. Must have columns person_id and phecode under the "prevalence" or "logistic" methods, columns person_id, phecode, and num_occurrences under the "loglinear" method, and columns person_id, phecode, and occurrence_age under the "cox" method. num_occurrences refers to the number of unique dates a phecode was recorded for a person. occurrence_age refers to the first age (in years) a person acquired a phecode.

method

A string indicating the statistical model for calculating weights.

methodFormula

A formula representing the right-hand side of the model corresponding to method. All terms in the formula must correspond to columns in demos. Do not use age-related covariates with the "cox" method.

dopar

Logical indicating whether to run calculations in parallel if a parallel backend is already set up, e.g., using doParallel::registerDoParallel(). Recommended to minimize runtime.

## Value

A data.table with various columns. If method is "prevalence": phecode, pred, and w (weight). If method is "logistic", "cox", or "loglinear": person_id, phecode, pred, and w. The column pred

represents a different quantity depending on method. Under the "prevalence" method, it is fraction of the cohort that has at least one occurrence of the given phecode. Under "logistic" or "cox" method, it is the predicted probability of given individual having a given phecode based on methodFormula. Under the "loglinear" method, it is the predicted log2(num_occurrences + 1) of a given phecode for a given individual based on methodFormula. For the "prevalence", "cox", and "logistic" methods, weight is calculated as -log10(pred), and for "loglinear" as the difference between the observed log2(num_occurrences + 1) and pred.

getPhecodeOccurrences(), getScores(), phers()

## Examples

library('data.table')
library('survival')

# map ICD codes to phecodes
phecodeOccurrences = getPhecodeOccurrences(icdSample)

# calculate weights using the prevalence method
weightsPrev = getWeights(demoSample, phecodeOccurrences)

# calculate weights using the logistic method
weightsLogistic = getWeights(
demoSample, phecodeOccurrences, method = 'logistic', methodFormula = ~ sex)

# calculate weights using the loglinear method
phecodeOccurrences2 = phecodeOccurrences[, .(
num_occurrences = uniqueN(entry_date)), by = .(person_id, phecode)]
weightsLoglinear = getWeights(
demoSample, phecodeOccurrences2, method = 'loglinear', methodFormula = ~ sex)

# calculate weights using the cox method
phecodeOccurrences3 = phecodeOccurrences[, .(
first_occurrence_date = min(entry_date)) , by = .(person_id, phecode)]
phecodeOccurrences3 = merge(
phecodeOccurrences3, demoSample[, .(person_id, dob)], by = 'person_id')
phecodeOccurrences3[,
occurrence_age := as.numeric((first_occurrence_date - dob)/365.25)]
#>      person_id phecode first_occurrence_date        dob occurrence_age
#>   1:         1     365            2008-02-22 1999-09-03       8.472154
#>   2:         1     366            2006-02-15 1999-09-03       6.454358
#>   3:         1     735            2009-11-05 1999-09-03      10.175097
#>   4:         1   735.1            2009-11-05 1999-09-03      10.175097
#>   5:         2     442            2014-07-06 1995-09-26      18.777409
#>  ---
#> 252:        49     474            2014-07-25 1999-10-21      14.759346
#> 253:        49   474.2            2014-07-25 1999-10-21      14.759346
#> 254:        49     496            2014-07-25 1999-10-21      14.759346
#> 255:        49   496.3            2014-07-25 1999-10-21      14.759346
#> 256:        49     963            2014-07-25 1999-10-21      14.759346
phecodeOccurrences3[, :=(first_occurrence_date = NULL, dob = NULL)]
#>      person_id phecode occurrence_age
#>   1:         1     365       8.472154
#>   2:         1     366       6.454358
#>   3:         1     735      10.175097
#>   4:         1   735.1      10.175097
#>   5:         2     442      18.777409
#>  ---
#> 252:        49     474      14.759346
#> 253:        49   474.2      14.759346
#> 254:        49     496      14.759346
#> 255:        49   496.3      14.759346
#> 256:        49     963      14.759346
demoSample3 = demoSample[, .(
person_id, sex,
first_age = as.numeric((first_visit_date - dob)/365.25),
last_age = as.numeric((last_visit_date - dob)/365.25))]

weightsCox = getWeights(
demoSample3, phecodeOccurrences3, method = 'cox', methodFormula = ~ sex)
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Ran out of iterations and did not converge
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.
#> Warning: Loglik converged before variable  1 ; beta may be infinite.