-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathegfr.R
More file actions
273 lines (251 loc) · 10.8 KB
/
egfr.R
File metadata and controls
273 lines (251 loc) · 10.8 KB
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
#' eGFR by CKD-EPI equation
#'
#' A vectorised function to calculate estimated glomerular filtration rate using the CKD-EPI
#' equation. By default the equation accepts serum creatinine in µmol/l but can be changed to
#' mg/dl by setting the units parameter to "US". To allow for serial measurements over time, such as
#' for transplant follow-up data, there is an optional offset = n parameter which increases the age
#' value used in the equation by n years.
#'
#' Reference: Levey AS, Stevens LA, Schmid CH, et al. A new equation to estimate glomerular filtration
#' rate. Ann Intern Med 2009; 150(9):604-612.
#'
#' @param creat numeric vector of serum creatinine in µmol/l (or mg/dl if units = "US")
#' @param age numeric vector of age in years (accepts integers or decimals)
#' @param sex character vector of sex ("F" for female, "M" for male)
#' @param ethnicity character vector of patient ethnicity, one of "black" or "non-black"
#' @param units non-vectorised optional parameter for creatinine unit ("SI" for µmol/l (default), "US" for mg/dl)
#' @param offset non-vectorised optional numeric parameter for offset in years
#'
#' @return a numeric vector of eGFR values
#' @export
#'
#' @examples
#' ckd_epi(creat = 120, age = 45.2, sex = "M", ethnicity = "non-black")
#' ckd_epi(creat = 1.5, age = 64.3, sex = "F", ethnicity = "black", units = "US")
ckd_epi <- function(creat, age, sex, ethnicity, units = "SI", offset = 0) {
# determine level of sexvar, alpha and kappa
sexvar = ifelse(sex == "M", 1, 1.018)
alpha = ifelse(sex == "M", -0.411, -0.329)
kappa = ifelse(sex == "M", 0.9, 0.7)
# convert SI to US units
if (units == "SI") {
creat = creat / 88.42
}
# age offset for serial measures
if (offset > 0) age = age + offset
gfr = 141 * ifelse(creat / kappa > 1, 1, creat / kappa)^alpha *
ifelse(creat / kappa < 1, 1, creat / kappa)^-1.209 *
0.993^age * sexvar
# adjustment for black ethnicity
gfr = ifelse(ethnicity == "black", gfr * 1.159, gfr)
gfr
}
#' eGFR by abbreviated MDRD equation
#'
#' A vectorised function to calculate estimated glomerular filtration rate using the
#' abbreviated (four variable) MDRD equation. By default the equation accepts serum creatinine
#' in µmol/l but can be changed to mg/dl by setting the units parameter to "US".
#' To allow for serial measurements over time, such as
#' for transplant follow-up data, there is an optional offset = n parameter which increases the age
#' value used in the equation by n years.
#'
#' Reference: Levey AS, Greene T, Kusek JW, et al. A simplified equation to predict glomerular
#' filtration rate from serum creatinine. J Am Soc Nephrol 2000; 11:A0828.
#'
#' @param creat numeric vector of serum creatinine in µmol/l (or mg/dl if units = "US")
#' @param age numeric vector of age in years (accepts integers or decimals)
#' @param sex character vector of sex ("F" for female, "M" for male)
#' @param ethnicity character vector of patient ethnicity, one of "black" or "non-black"
#' @param units non-vectorised optional parameter for creatinine unit ("SI" for µmol/l (default), "US" for mg/dl)
#' @param offset non-vectorised optional parameter for offset in years
#'
#' @return a numeric vector of eGFR values
#' @export
#'
#' @examples
#' mdrd(creat = 120, age = 45.2, sex = "M", ethnicity = "non-black")
#' mdrd(creat = 1.5, age = 64.3, sex = "F", ethnicity = "black", units = "US")
mdrd <- function (creat, age, sex, ethnicity, units = "SI", offset = 0) {
# determine level of sexvar
sexvar = ifelse(sex == "M", 1, 0.742)
# convert SI to US units
if (units == "SI") {
creat = creat / 88.42
}
# age offset for serial measures
if (offset > 0) age = age + offset
gfr = 186 * creat^-1.154 * age^-0.203 * sexvar
# adjustment for black ethnicity
gfr = ifelse(ethnicity == "black", gfr * 1.21, gfr)
gfr
}
#' eGFR by bedside Schwartz formula
#'
#' A vectorised formula to calculate estimate glomerular filtration rate in children using the bedside Schwartz formula. By default this uses
#' serum creatinine in µmol/l but this can be changed to mg/dl by setting the optional units parameter to "US".
#'
#' Reference: Schwartz GJ, Munoz A, Schneider MF et al. New equations to estimate GFR in children
#' with CKD. J Am Soc Nephrol 2009; 20(3):629-637.
#'
#' @param creat numeric vector of creatinine levels in µmol/l (or mg/dl if units = "US")
#' @param height numeric vector of heights in cm
#' @param units non-vectorised optional parameter for creatinine unit ("SI" for µmol/l (default), "US" for mg/dl)
#'
#' @return numeric vector of eGFR values
#' @export
#'
#' @examples
#' # calculate using creatinine in µmol/l
#' schwartz(creat = 64, height = 101)
#'
#' # calculate using mg/dl
#' schwartz(creat = 0.7, height = 101, units = "US")
schwartz = function(creat, height, units = "SI") {
if (units == "SI") {
gfr = 36.5 * height / creat
} else {
gfr = 0.413 * height / creat
}
gfr
}
#' eGFR by CKD-EPI equation (US units)
#'
#' A wrapper function for the ckd_epi() vectorised function to calculate estimated glomerular
#' filtration rate using the CKD-EPI equation, using serum creatinine in mg/dl.
#' To allow for serial measurements over time, such as
#' for transplant follow-up data, there is an optional offset = n parameter which increases the age
#' value used in the equation by n years.
#'
#' Reference: Levey AS, Stevens LA, Schmid CH, et al. A new equation to estimate glomerular filtration
#' rate. Ann Intern Med 2009; 150(9):604-612.
#'
#' @param creat numeric vector of serum creatinine in µmol/l (or mg/dl if units = "US")
#' @param age numeric vector of age in years (accepts integers or decimals)
#' @param sex character vector of sex ("F" for female, "M" for male)
#' @param ethnicity character vector of patient ethnicity, one of "black" or "non-black"
#' @param offset non-vectorised optional parameter for offset in years
#'
#' @return a numeric vector of eGFR values
#' @export
#'
#' @examples
#' ckd_epi_US(creat = 1.5, age = 64.3, sex = "F", ethnicity = "black")
ckd_epi_US <- function (creat, age, sex, ethnicity, offset = 0) {
ckd_epi(creat, age, sex, ethnicity, units = "US", offset = offset)
}
#' eGFR by abbreviated MDRD equation (US units)
#'
#' A wrapper for the mdrd4v() vectorised function to calculate estimated glomerular filtration rate
#' using the abbreviated (four variable) MDRD equation, but using serum creatinine in mg/dl.
#' To allow for serial measurements over time, such as
#' for transplant follow-up data, there is an optional offset = n parameter which increases the age
#' value used in the equation by n years.
#'
#' Reference: Levey AS, Greene T, Kusek JW, et al. A simplified equation to predict glomerular
#' filtration rate from serum creatinine. J Am Soc Nephrol 2000; 11:A0828.
#'
#' @param creat numeric vector of serum creatinine in µmol/l (or mg/dl if units = "US")
#' @param age numeric vector of age in years (accepts integers or decimals)
#' @param sex character vector of sex ("F" for female, "M" for male)
#' @param ethnicity character vector of patient ethnicity, one of "black" or "non-black"
#' @param offset non-vectorised optional parameter for offset in years
#'
#' @return a numeric vector of eGFR values
#' @export
#'
#' @examples
#' mdrd_US(creat = 1.5, age = 64.3, sex = "F", ethnicity = "black")
mdrd_US <- function (creat, age, sex, ethnicity, offset = 0) {
mdrd(creat, age, sex, ethnicity, units = "US", offset = offset)
}
#' eGFR by bedside Schwartz formula (US units)
#'
#' A wrapper function for the schwartz() vectorised formula to calculate estimate glomerular filtration rate in children
#' using the bedside Schwartz formula, using
#' serum creatinine in mg/dl. Use the schwartz() function instead for µmol/l.
#'
#' Reference: Schwartz GJ, Munoz A, Schneider MF et al. New equations to estimate GFR in children
#' with CKD. J Am Soc Nephrol 2009; 20(3):629-637.
#'
#' @param creat numeric vector of creatinine levels in µmol/l (or mg/dl if units = "US")
#' @param height numeric vector of heights in cm
#'
#' @return numeric vector of eGFR values
#' @export
#'
#' @examples
#' # calculate using creatinine in -mg/dl
#' schwartz_US(creat = 0.7, height = 101)
schwartz_US = function(creat, height) {
schwartz(creat = creat, height = height, units = "US")
}
#' Creatinine clearance by Cockcroft-Gault equation
#'
#' A vectorised function to estimate creatinine clearance using the Cockcroft-Gault equation.
#' By default this uses serum creatinine in µmol/l but can be changed to mg/dl by setting the
#' units parameter to "US"
#'
#' Reference: Cockcroft DW, Gault MH. Prediction of creatinine clearance from serum creatinine.
#' Nephron 1976; 16(1):31-41
#'
#' @param creat numeric vector of creatinine levels in µmol/l (or mg/dl if units = "US")
#' @param age numeric vector of ages in years
#' @param sex character vector of sex ("F" = female, "M" = male)
#' @param weight numeric vector of weights in kilograms
#' @param units non-vectorised parameter for creatinine units ("SI" for µmol/l (default) or "US" for mg/dl)
#'
#' @return numeric vector of creatinine clearances in ml/min
#' @export
#'
#' @examples
#' # calculate creatinine clearance using creatinine in µmol/l
#' cockcroft(creat = 88.4, age = 25, sex = "F", weight = 60)
#'
#' # calculate using creatinine in mg/dl
#' cockcroft(creat = 1, age = 25, sex = "F", weight = 60, units = "US")
cockcroft = function(creat, age, sex, weight, units = "SI"){
if (units == "SI") {
creat = creat / 88.4
}
sexvar = ifelse(sex == "M", 1, 0.85)
sexvar * (140 - age) * weight / creat / 72
}
#' Creatinine clearance by Cockcroft-Gault equation (US units)
#'
#' A wrapper function for cockcroft(), a vectorised function to estimate creatinine clearance using the Cockcroft-Gault equation,
#' but using creatinine in mg/dl
#'
#' Reference: Cockcroft DW, Gault MH. Prediction of creatinine clearance from serum creatinine.
#' Nephron 1976; 16(1):31-41
#'
#' @param creat numeric vector of creatinine levels in mg/dl
#' @param age numeric vector of ages in years
#' @param sex character vector of sex ("F" = female, "M" = male)
#' @param weight numeric vector of weights in kilograms
#'
#' @return numeric vector of creatinine clearances in ml/min
#' @export
#'
#' @examples
#' cockcroft_US(creat = 1, age = 25, sex = "F", weight = 60)
cockcroft_US = function(creat, age, sex, weight){
cockcroft(creat = creat, age = age, sex = sex, weight = weight, units = "US")
}
#' Ideal body weight
#'
#' A vectorised function to calculate adult ideal body weight based on height and sex.
#' This function assumes ideal BMI of 21.5 for females and 23 for males.
#'
#' @param height numeric vector of heights in cm
#' @param sex character vector of sex ("F" for female or "M" for male)
#'
#' @return numeric vector of ideal body weights in kg
#' @export
#'
#' @examples
#' ibw(height = 183, sex = "M")
ibw = function(height, sex) {
bmivar = ifelse(sex == "M", 23, 21.5)
height = height / 100
height ^ 2 * bmivar
}