adjprop: Calculate adjusted proportions using direct standardisation

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/adjprop.R

Description

Calculates adjusted proportions of a variable by groups defined by another variable using direct standardisation to the structure of the dataset, as defined by one or more variables.

Usage

1
2
3
4
adjprop(dataset, outcome_vars, categorical_vars, outcome_var_labels = NULL, 
categorical_var_labels = NULL, adjustment_vars = c("age", "sex"), 
adjustment_var_labels = NULL, format_table = FALSE,
transpose_table = FALSE, percentage = FALSE, ndigits = 2, title = "")

Arguments

dataset

A data frame containing all variables to be used.

outcome_vars

The names of the outcome variables in dataset (character vector). These are the variables of which adjusted proportions will be calculated.

categorical_vars

A character vector containing the names of categorical variables which define the groups by which adjusted proportions will be calculated. They must exist in dataset.

outcome_var_labels

Labels for the outcome variable to be printed in the table produced. This must be a list of length equal to the number of outcome variables, with each element a list of length two, the first element of which is a character with a label for the variable and the second element a character vector with labels for the levels of the variable. For example for two variables, the first of which has 3 levels and the second 2, list(list("Variable 1", c("Group 1", "Group 2", "Group 3")), list("Variable 2", c("Group 1", "Group 2"))). Note that if there is only one variable it should be list(list("Variable 1", c("Group 1", "Group 2", "Group 3"))). If null, the names and levels of the variables are used.

categorical_var_labels

Labels for the categorical variables by which proportions will be calculated, to be printed in the table produced. This must be a list of length equal to the number of variables by which adjusted proportions will be calculated, with each element a list of length two, the first element of which is a character with a label for the variable and the second element a character vector with labels for the levels of the variable. For example for two variables, the first of which has 3 levels and the second 2, list(list("Variable 1", c("Group 1", "Group 2", "Group 3")), list("Variable 2", c("Group 1", "Group 2"))). Note that if there is only one variable it should be list(list("Variable 1", c("Group 1", "Group 2", "Group 3"))). If null, the levels of the variable are used.

adjustment_vars

A character vector containing the names of categorical variables to be adjusted for. The default is age and sex, which standardises proportions of the subgroups to the age and sex structure of the overall dataset.

adjustment_var_labels

A character vector with labels for the variables adjusted for, to be printed in the table produced. If null, the variable names are used.

format_table

Whether the output table should be formatted. Defaults to FALSE.

transpose_table

Whether the output table should be transposed. Defaults to FALSE.

ndigits

Number of digits to be printed (defaults to 2).

percentage

If TRUE, percentages instead of proportions are calculated. The default is FALSE.

title

A title for the table (defaults to blank).

Details

The function produces a table of proportions of some outcome variable by one or more categorical variables using direct standardisation with target population a population with proportions within each group specified by some variables (default is age and sex) identical for all categories of the categorical variable and equal to the overall proportion in the data.

Value

A data frame of adjusted proportions with categorical variables defining the groupings as rows and outcome categories as columns.

Author(s)

Christiana Kartsonaki <christiana.kartsonaki@gmail.com>

See Also

adjmeans

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Example 1

# generate a dataframe with sleep deprivation (binary), sex and age group
data <- data.frame("sleep_deprivation" = rbinom(50, size = 1, prob = 0.5), 
"sex" = c(rep("m", 25), rep("f", 25)), 
"age_group" = rep(c("20-29", "30-39", "40-49", "50-59", "60-69"), 5))

adjprop(dataset = data, outcome_vars = "sleep_deprivation", 
categorical_vars = "sex", 
outcome_var_labels = list(list("Sleep deprivation", levels(as.factor(data$sleep_deprivation)))), 
categorical_var_labels = list(list("Sex", c("Female", "Male"))), 
adjustment_vars = "age_group", adjustment_var_labels = "age", 
title = "Proportions of sleep deprivation by sex.")

# Example 2

# generate a dataframe with sleep duration group (3 categories), sex and age group
data <- data.frame("sleep_group" = rbinom(500, size = 2, prob = 0.3), 
"sex" = c(rep("m", 25), rep("f", 25)), 
"age_group" = rep(c("20-29", "30-39", "40-49", "50-59", "60-69"), 5))

adjprop(dataset = data, outcome_vars = "sleep_group", 
categorical_vars = "sex", outcome_var_labels = list(list("Sleep duration group", 
c("Group 1", "Group 2", "Group 3"))),
categorical_var_labels = list(list("Sex", c("Female", "Male"))), 
adjustment_vars = "age_group", adjustment_var_labels = "age", 
title = "Proportions of sleep duration groups by sex.")

# Example 3

# generate a dataframe with sleep duration group (3 categories), sex and age group
data <- data.frame("sleep_group" = rbinom(500, size = 2, prob = 0.3), 
"sex" = c(rep("m", 25), rep("f", 25)), 
"age_group" = rep(c("20-29", "30-39", "40-49", "50-59", "60-69"), 5))

# no labels, more digits
adjprop(dataset = data, outcome_vars = "sleep_group", 
categorical_vars = "sex", adjustment_vars = "age_group", 
adjustment_var_labels = "age", ndigits = 4, 
title = "Proportions of sleep duration groups by sex.")

# Example 4

# generate a dataframe with sleep duration group (4 categories), sex and age group
data <- data.frame("sleep_group" = rbinom(500, size = 3, prob = 0.25), 
"sex" = c(rep("m", 25), rep("f", 25)), 
"age_group" = rep(c("20-29", "30-39", "40-49", "50-59", "60-69"), 5))

# no labels, proportions
adjprop(dataset = data, outcome_vars = "sleep_group", 
categorical_vars = "sex", adjustment_vars = "age_group", 
adjustment_var_labels = "age", title = "Proportions of sleep duration groups by sex.")

# no labels, percentages
adjprop(dataset = data, outcome_vars = "sleep_group", 
categorical_vars = "sex", adjustment_vars = "age_group", 
adjustment_var_labels = "age", percentage = TRUE, 
title = "Proportions of sleep duration groups by sex.")

Example output

      sleep_deprivation.variable sleep_deprivation.levels
sex.f                        Sex                   Female
sex.m                        Sex                     Male
      sleep_deprivation.N.Var1 sleep_deprivation.N.Freq
sex.f                        f                       25
sex.m                        m                       25
      sleep_deprivation.proportion.0 sleep_deprivation.proportion.1
sex.f                           0.44                           0.56
sex.m                           0.40                           0.60
      sleep_deprivation.variance.0 sleep_deprivation.variance.1
sex.f                      0.00576                      0.00576
sex.m                      0.00832                      0.00832
      sleep_deprivation.se.0 sleep_deprivation.se.1
sex.f             0.07589466             0.07589466
sex.m             0.09121403             0.09121403
      sleep_group.variable sleep_group.levels sleep_group.N.Var1
sex.f                  Sex             Female                  f
sex.m                  Sex               Male                  m
      sleep_group.N.Freq sleep_group.proportion.0 sleep_group.proportion.1
sex.f                250                    0.508                    0.380
sex.m                250                    0.512                    0.388
      sleep_group.proportion.2 sleep_group.variance.0 sleep_group.variance.1
sex.f                    0.112             0.00096544              0.0009136
sex.m                    0.100             0.00099392              0.0009456
      sleep_group.variance.2 sleep_group.se.0 sleep_group.se.1 sleep_group.se.2
sex.f             0.00039744       0.03107153       0.03022582       0.01993590
sex.m             0.00035424       0.03152650       0.03075061       0.01882126
      sleep_group.variable sleep_group.levels sleep_group.N.Var1
sex.f                  sex                  f                  f
sex.m                  sex                  m                  m
      sleep_group.N.Freq sleep_group.proportion.0 sleep_group.proportion.1
sex.f                250                    0.476                    0.420
sex.m                250                    0.504                    0.408
      sleep_group.proportion.2 sleep_group.variance.0 sleep_group.variance.1
sex.f                    0.104             0.00098976             0.00095200
sex.m                    0.088             0.00099840             0.00096192
      sleep_group.variance.2 sleep_group.se.0 sleep_group.se.1 sleep_group.se.2
sex.f             0.00036672       0.03146045       0.03085450       0.01914993
sex.m             0.00031744       0.03159747       0.03101484       0.01781685
      sleep_group.variable sleep_group.levels sleep_group.N.Var1
sex.f                  sex                  f                  f
sex.m                  sex                  m                  m
      sleep_group.N.Freq sleep_group.proportion.0 sleep_group.proportion.1
sex.f                250                    0.392                    0.460
sex.m                250                    0.416                    0.388
      sleep_group.proportion.2 sleep_group.proportion.3 sleep_group.variance.0
sex.f                    0.116                    0.032             0.00093696
sex.m                    0.192                    0.004             0.00096640
      sleep_group.variance.1 sleep_group.variance.2 sleep_group.variance.3
sex.f             0.00097632             0.00040672             0.00012352
sex.m             0.00094112             0.00061184             0.00001568
      sleep_group.se.0 sleep_group.se.1 sleep_group.se.2 sleep_group.se.3
sex.f       0.03060980       0.03124612        0.0201673      0.011113955
sex.m       0.03108697       0.03067768        0.0247354      0.003959798
      sleep_group.variable sleep_group.levels sleep_group.N.Var1
sex.f                  sex                  f                  f
sex.m                  sex                  m                  m
      sleep_group.N.Freq sleep_group.proportion.0 sleep_group.proportion.1
sex.f                250                    0.392                    0.460
sex.m                250                    0.416                    0.388
      sleep_group.proportion.2 sleep_group.proportion.3 sleep_group.variance.0
sex.f                    0.116                    0.032             0.00093696
sex.m                    0.192                    0.004             0.00096640
      sleep_group.variance.1 sleep_group.variance.2 sleep_group.variance.3
sex.f             0.00097632             0.00040672             0.00012352
sex.m             0.00094112             0.00061184             0.00001568
      sleep_group.se.0 sleep_group.se.1 sleep_group.se.2 sleep_group.se.3
sex.f       0.03060980       0.03124612        0.0201673      0.011113955
sex.m       0.03108697       0.03067768        0.0247354      0.003959798

DirectStandardisation documentation built on March 16, 2020, 9:06 a.m.