-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdose_response.R
132 lines (117 loc) · 5.07 KB
/
dose_response.R
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#' Dose response for Physical Activity
#'
#' Based on our meta-analysis, returns a response value for a specific cause or disease. \cr
#' 1. Disease/Cause: Name of the disease/cause - from a specific list\cr
#' 2. Type: Fatal, non-fatal or fatal-and-non-fatal. \cr
#'
#' At the end, this function returns a response values with or without confidence interval
#'
#'
#' @param cause All-cause-mortality or disease name
#' @param outcome_type Fatal, non-fatal or fatal-and-non-fatal
#' @param dose Dose (all-cause or disease)
#' @param quantile Numeric value between 0 and 1 - default is 0.5
#' @param censor_method Use either no censor method, or the 75th percentile by person years,
# ' use a fixed WHO recommended double level of physical activity (17.5 MMET hours per week) - default is 17.5 MMET hours per week (double the WHO recommencded guideline), or
# use a fixed WHO recommended quadruple level of physical activity (35 MMET hours per week)
#' @param confidence_intervals Boolean variable to determine whether confidence intervals are returned or not - default is FALSE
#' @return response for a specific dose (in a data frame)
#' @rdname dose_response
#' @importFrom magrittr %>%
#' @export
#'
dose_response <- function (cause, outcome_type, dose, quantile = 0.5, censor_method = "75thPercentile", confidence_intervals = F){
if (is.null(dose) || class(dose) != "numeric")
stop ('Please provide dose in numeric')
if (is.na(quantile) || class(quantile) != 'numeric' || quantile >= 1 || quantile < 0)
stop('Please provide the quantile value between 0 and 1')
if (is.na(censor_method) || class(censor_method) != "character" || !censor_method %in% c('none', '75thPercentile','WHO-DRL', 'WHO-QRL'))
stop('Please specificy `censor_method` by selecting either of four options: `none`, `75thPercentile`,`WHO-DRL`,`WHO-QRL`')
pert_75 <- readr::read_csv(system.file("extdata", "75p_diseases.csv",
package = "drpa",
mustWork = TRUE),
col_type = readr::cols())
if (!cause %in% pert_75$disease){
stop("Unsupported cause/disease. Please select from \n
all-cause-cancer \n
all-cause-cvd \n
all-cause-dementia \n
all-cause-mortality \n
alzheimer's-disease \n
bladder-cancer \n
breast-cancer \n
colon-cancer \n
coronary-heart-disease \n
depression \n
depressive-symptoms \n
diabetes \n
endometrial-cancer \n
esophageal-cancer \n
gastric-cardia-cancer \n
head-and-neck-cancer \n
heart-failure \n
kidney-cancer \n
liver-cancer \n
lung-cancer \n
major-depression \n
myeloid-leukemia \n
myeloma \n
parkinson's-disease \n
prostate-cancer \n
rectum-cancer \n
stroke \n
vascular-dementia \n")
}
if (!outcome_type %in% c('fatal', 'non-fatal', 'fatal-and-non-fatal')){
stop('Unsupported outcome_type. Please select from \n
fatal \n
non-fatal \n
fatal-and-non-fatal')
}
fname <- paste(cause, outcome_type, sep = "-")
if (!file.exists(system.file("extdata", paste0(fname, ".csv"),
package = "drpa"))){
stop(paste('Sorry but for ', cause, ' the outcome type ', outcome_type, ' is not supported by the package'))
}
lookup_table <- readr::read_csv(system.file("extdata", paste0(fname, ".csv"),
package = "drpa",
mustWork = TRUE),
col_type = readr::cols())
if(censor_method == "75thPercentile"){
upper_limit <- pert_75 %>% dplyr::filter(disease == cause) %>% dplyr::select(outcome_type) %>% as.numeric()
dose[dose > upper_limit] <- upper_limit
}else if (censor_method == "WHO-DRL"){ # Double of WHO's recommended level of PA for adults - which is 17.5 MMETs hours per week
dose[dose > 17.5] <- 17.5
}else if (censor_method == "WHO-QRL"){ # Quadruple of WHO's recommended level of PA for adults - which is 35 MMETs hours per week
dose[dose > 35] <- 35
}
rr <- approx(x = lookup_table$dose, y = lookup_table$RR, xout = dose,yleft = 1, yright = min(lookup_table$RR))$y
if (confidence_intervals || quantile != 0.5) {
lb <-
approx(
x = lookup_table$dose,
y = lookup_table$lb,
xout = dose,
yleft = 1,
yright = min(lookup_table$lb)
)$y
ub <-
approx(
x = lookup_table$dose,
y = lookup_table$ub,
xout = dose,
yleft = 1,
yright = min(lookup_table$ub)
)$y
}
## we assume that the columns describe a normal distribution with standard deviation defined by the upper and lower bounds.
if (quantile != 0.5){
rr <- qnorm(quantile, mean = rr, sd = (ub-lb)/1.96)
rr[rr<0] <- 0
}
if (confidence_intervals) {
return(data.frame (rr = rr, lb = lb, ub = ub))
}else{
return(data.frame(rr = rr))
}
}