forked from vynguyen92/nhanes_mortality_associations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f - determine_predicted_risk.R
289 lines (233 loc) · 12.7 KB
/
f - determine_predicted_risk.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
############## FUNCTION TO DETERMINE THE HAZARD RATIO FOR EACH VALUE OF THE CONTINOUS VARIABLE ##############
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Purpose: This function runs a cox regression model to calculate the hazard ratio of each value of the
# transformed variable
#
# Inputs: dataset_updated - dataframe the pertinent variables for running the regression model
# contin_values - numeric vector of the continous variable
# included_colnames - string vector of the pertinent column names
# analysis_type - string indicating whether the survey weights should be used or not, i.e. "weighted"
# or "unweighted"
# codename - string indicating the name of the population type ("all" or "[insert code name of
# physiological indicator]") or the codename of the physiological indicator
# treatment_on_contin - string indictating the treatment on the continous variable, i.e. "linear",
# "novemtiles", or "splines"
# colname_treated - string of the column name pertaining to the transformed variable
# sensitivity_range - string indicating the percentiles of the transformed variable on which
# participants to include in the regression models
# model_types - string indicating whether the transformation was applied on age or the physiological
# indicator
# dataset_refs - default to NA. If specified, then it's a dataframe of reference groups
#
# Outputs: df_contin_value - a dataframe of the hazard ratios of each value of the transformed variable
determine_predicted_risk <- function(dataset_updated
, contin_values
, included_colnames
, type_analysis
, codename
, treatment_on_contin
, colname_treated
, sensitivity_range
, model_types
, dataset_refs = NA)
{
# Define a vector of column names for the explanatory and outcome variables
included_columns_updated <- c(included_colnames
, "time_to_death"
, "mortality_status")
# print(included_columns_updated)
# Define the subset to have the pertinent variables
subset_updated <- dataset_updated[,included_columns_updated]
# Extract the survey weights
survey_weights <- dataset_updated$adjusted_weights
# Extract the sampling clusters
survey_clusters <- dataset_updated$cluster
# If "cluster" is in the vector of pertinent variables, remove it because we need to apply
# a function on it to correctly account for NHANES sampling design
if("cluster" %in% included_columns_updated)
{
# Determine the index pertaining to "cluster"
index_cluster <- which(included_columns_updated == "cluster")
# Remove it from the vector of pertinent variables
included_columns_updated <- included_columns_updated[-index_cluster]
}
# Define the subset to have the pertinent variables
subset_updated <- dataset_updated[,included_columns_updated]
# print(colnames(subset_updated))
# Run a regression model to calculate the predicted risk
if(treatment_on_contin %in% c("linear", "novemtiles"))
{
if(type_analysis == "weighted")
{
# Run the cox regression model accounting for NHANES sampling design and the survey weights
coxph_model <- coxph(Surv(time = time_to_death
, event = mortality_status) ~ . +
cluster(survey_clusters)
, weights = survey_weights
, data = subset_updated)
} else if(type_analysis == "unweighted") {
# Run the cox regression model without accounting for NHANES sampling design and the survey weights
coxph_model <- coxph(Surv(time = time_to_death
, event = mortality_status) ~ .
, data = subset_updated)
}
} else if(treatment_on_contin == "splines") {
# print(colnames(subset_updated))
index_splines <- which(grepl("^splines", colnames(subset_updated)))
columns_with_out_splines <- colnames(subset_updated)[-index_splines]
# print(columns_with_out_splines)
subset_updated <- subset_updated[,columns_with_out_splines]
# Define for the number of cut-off points
step_size <- 1/4
# Cut the continous variables into quartiles to determine the cut-off points or knots
ntiles_pi <- quantile(contin_values
, na.rm = TRUE
, probs = seq(0, 1, by = step_size)
, names = FALSE)
# Define a vector to contain the cut-off points, which is all boundaries except the minimum and maximum
# ones
ntiles_pi <- ntiles_pi[2:((1/step_size))]
if(type_analysis == "weighted")
{
# Run the cox regression splines model accounting for NHANES sampling design and the survey weights
coxph_model <- coxph(Surv(time = time_to_death
, event = mortality_status) ~
bs(contin_values, knots = ntiles_pi) +
cluster(survey_clusters) +
.
, weights = survey_weights
, data = subset_updated)
} else if(type_analysis == "unweighted") {
# Run the cox regression splines model without accounting for NHANES sampling design and the survey
# weights
coxph_model <- coxph(Surv(time = time_to_death
, event = mortality_status) ~
bs(contin_values, knots = ntiles_pi) + .
, data = subset_updated)
}
# print(summary(coxph_model))
# Define a string so that it can be used to extract the correct dataframe containing the predicted risk
# for the spline model
colname_treated <- "contin_values"
} else {
print("Error: Transformation on Physiological Indicator was not considered.")
}
# print((coxph_model))
# print(colname_treated)
# Determine the predicted risk for each explanatory variable
predicted_data_points <- termplot(coxph_model
, se = TRUE
, plot = FALSE)
# print(predicted_data_points)
# Extract the predicted risk for the transformed variable
# This dataframe has a predicted risk for each unique value of the transformed variable
df_contin_value <- predicted_data_points[[colname_treated]]
# print(df_contin_value)
# If the reference group is not specified, then set the median as the reference group
if(anyNA(dataset_refs) == TRUE)
{
reference_point <- quantile(contin_values
, probs = 0.5
, names = FALSE)
# If the reference is specified, then set that value as the reference group
} else {
reference_point <- dataset_refs %>%
# Include a given treatment
filter(treatment_on_pi == treatment_on_contin) %>%
# Select the column vector pertaining to value serving as the reference group
select(ref) %>%
unlist(., use.names = FALSE) %>%
# Ensure that the value is numeric
as.numeric(.)
}
# print(reference_point)
# If the reference point is a value of the continuous variable, then the reference point remains unchanged
if(reference_point %in% df_contin_value$x)
{
reference_point <- reference_point
# If the reference point is not, then determine the value that is closest to the reference point
} else {
# Find the difference between the reference point and each value of the continous variable
diffs <- abs(contin_values - reference_point)
# Determine the minimum difference
min_diff <- min(diffs)
# Determine the index pertaining to the minimum difference
index_min <- which(diffs == min_diff)
# Find the value that is close to the reference point
value_close_to_reference_point <- contin_values[index_min] %>%
unique()
# If there are several values close to the reference point, then close one of them
reference_point <- value_close_to_reference_point[1] %>%
unname()
}
# print(reference_point)
# Define a dataframe of the predicted risk
if(model_types == "pi")
{
# Rename the column names for legibility
colnames(df_contin_value) <- c("pi_value", "predicted_risk", "se")
# Find the predicted risk corresponds to the reference point
center <- df_contin_value$predicted_risk[df_contin_value$pi_value == reference_point]
# print(center)
# Define the dataframe of predicted risk for a physiological indicator
df_contin_value <- df_contin_value %>%
# Define a column vector of the codename of the physiological indicator
mutate(pi = rep(codename, nrow(.))) %>%
# Define a column vector of the treatment on the physiological indicator
mutate(treatment_on_pi = rep(treatment_on_contin, nrow(.))) %>%
# Define a column vector of sensitivity analysis on the physiological indicator
mutate(sensitivity_range = rep(sensitivity_range, nrow(.))) %>%
# Define the mean hazard ratio for each value of the physiological indicator
mutate(hazard_ratio = exp(predicted_risk - center)) %>%
# Define the lower bound of the 95% confidence interval on hazard ratio for each value of the
# physiological indicator
mutate(hazard_ratio_low_ci = exp(predicted_risk - se*1.96 - center)) %>%
# Define the upper bound of the 95% confidence interval on hazard ratio for each value of the
# physiological indicator
mutate(hazard_ratio_high_ci = exp(predicted_risk + se*1.96 - center))
# Define a vector of the column names rearranged in a reasonable order
colnames_arranged <- c("pi"
, "treatment_on_pi"
, "sensitivity_range"
, "pi_value"
, "predicted_risk"
, "se"
, "hazard_ratio"
, "hazard_ratio_low_ci"
, "hazard_ratio_high_ci")
} else if(model_types == "demo") {
# Rename the column names for legibility
colnames(df_contin_value) <- c("demo_value", "predicted_risk", "se")
# Find the predicted risk corresponds to the reference point
center <- df_contin_value$predicted_risk[df_contin_value$demo_value == reference_point]
# Define the dataframe of predicted risk for the age variable
df_contin_value <- df_contin_value %>%
# Define a column vector of the population type ("all" or "[insert code name of physiological indicator]")
mutate(population_type = rep(codename, nrow(.))) %>%
# Define a column vector of the treatment on the age variable
mutate(treatment_on_demo = rep(treatment_on_contin, nrow(.))) %>%
# Define a column vector of sensitivity analysis on the age variable
mutate(sensitivity_range = rep(sensitivity_range, nrow(.))) %>%
# Define the mean hazard ratio for each value of the age variable
mutate(hazard_ratio = exp(predicted_risk - center)) %>%
# Define the lower bound of the 95% confidence interval on hazard ratio for each value of the age variable
mutate(hazard_ratio_low_ci = exp(predicted_risk - se*1.96 - center)) %>%
# Define the uper bound of the 95% confidence interval on hazard ratio for each value of the age variable
mutate(hazard_ratio_high_ci = exp(predicted_risk + se*1.96 - center))
# Define a vector of the column names rearranged in a reasonable order
colnames_arranged <- c("population_type"
, "treatment_on_demo"
, "sensitivity_range"
, "demo_value"
, "predicted_risk"
, "se"
, "hazard_ratio"
, "hazard_ratio_low_ci"
, "hazard_ratio_high_ci")
}
# Arrange the column into the specified order
df_contin_value <- df_contin_value[,colnames_arranged]
# View(df_contin_value)
return(df_contin_value)
}