/
rslab.Rmd
233 lines (155 loc) 路 8.83 KB
/
rslab.Rmd
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
---
title: "Introduction to rslab"
output:
rmarkdown::html_vignette:
fig_width: 6
fig.align: 'center'
fig.asp: 0.618
dpi: 320
toc: true
warning: FALSE
message: FALSE
vignette: >
%\VignetteIndexEntry{Introduction to rslab}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
dpi = 300,
warning = FALSE,
message = FALSE,
out.width = "100%",
comment = "#>"
)
```
## BTPS Calculator
[Spirometry test](https://en.wikipedia.org/wiki/Spirometry) is a procedure that measures lung volumes and air flow parameters which calculate from inspiratory and expiratory gas volumes of a human subject.
The gas volumes/flows obtained by the spirometer are in **ATPS** (Ambient Temperature and Pressure Saturated) conditions which its volumes also depends on environmental conditions including room temperature.
The obtained volumes/flows must be converted to gas condition in **BTPS** (Body Temperature, Pressure, water vapor Saturated) which gives more accurate representation of actual volumes/flows within the lungs.
\
[BTPS correction factor](https://nddmed.com/_Resources/Persistent/8a716ac3fd123ce0becd7b56596582d4fc4c0c47/appnote-btps-correction-v01r.pdf) is a coefficient used to convert flow and volume measured at ambient conditions (ATPS) to the conditions within the lungs (BTPS).
`get_btps_factor()` is a function to obtain the BTPS correction factor.
```{r setup}
library(rslab)
```
```{r btps}
# BTPS correction factor at room temperature = 27 degree celsius
btps_factor27 <- get_btps_factor(temp = 27)
btps_factor27
```
If Tidal Volume (TV) was measured as 500 ml at room temperature of 27 degree celsius, the
TV in the lung at BTPS (body temperature, pressure, water vapor saturated) would be:
```{r}
# TV (ml) at BTPS
500 * btps_factor27
```
If you want to convert several lung volumes or flow (from ATPS to BTPS), use `lung_vol_atps_btps()` and the result will be shown as a tibble.
```{r lung_vol_conv}
lung_vol_atps_btps(
temp = 27,
FEV1 = 3.5,
FVC = 4.5,
PEF = 450,
TV = 0.5,
IC = 2.5,
EC = 2.5,
VC = 4.5
)
```
## Metabolic Rate & Oxygen Consumption Calculator
In a laboratory experiment using [Harvard Spirometer](https://www.somatco.com/Recording-Spirometer-50-1833-50-1817.pdf), this package provides functions to calculate metabolic rate and oxygen consumption by input a displacement in x-and y-direction of Harvard spirometer tracing, height and weight of the subject, and environmental conditions which are temperature and barometric pressure.
### Oxygen Consumption
In Harvard spirometer tracing, horizontal direction (x-direction) represents time (default paper speed = 25 mm/min) whereas vertical direction (y-direction) represents usage of oxygen (1 mm = 30 ml of oxygen). With a little bit of calculation, we can derived an oxygen consumption in unit of L/hr in the **ATPS** condition (**A**mbient **T**emperature and barometric **P**ressure **S**aturated with water vapor condition).
You can use `get_oxycons()` to calculate oxygen consumption and it will also provide other info printed to R console as well.
```{r oxycons}
oxycons <- get_oxycons(x = 15, # displacement in x-direction = 15 mm
y = 80, # displacement in y-direction = 80 mm
paper_speed = 25 # Paper speed of the kymograph = 25 mm/min
)
```
### Metabolic Rate
To calculate metabolic rate, we must first convert oxygen consumption $\dot{V}_{o_2}$ at **ATPS** to **STPD** (**S**tandard **T**emperature of 0掳C and a barometric **P**ressure of 760 mmHg, and in a **D**ry state).
STPD correction factor can be used to convert gas in ATPS to BTPS condition.
It has a linear relationship with barometric pressure and temperature.
Therefore, `get_STPD_factor()` can be used to predict STPD factor using `baro` and `temp_c` as predictors. (It use multiple linear regression model behind the scene.)
```{r stpd_760_25}
stpd_760_25 <- get_STPD_factor(
baro = 760, # Barometric pressure at the recording site.
temp_c = 25 # Temperature in celsius at the recording site.
)
```
Correction can be made by multiply oxygen consumption $\dot{V}_{o_2}$ at ATPS to STPD correction factor.
```{r}
oxycons * stpd_760_25 # Unit in L/hr
```
Finally, metabolic rate can be calculated by times an oxygen consumption $\dot{V}_{o_2}$ (L/hr) at STPD to a caloric equivalent of oxygen (Cal/hr) divided by Body Surface Area (BSA) in m^2^, which can be calculated by [DuBois & DuBois formula](http://www-users.med.cornell.edu/~spon/picu/calc/bsacalc.htm) (DuBois D, DuBois EF)
<!-- $$ -->
<!-- Met \ Rate \ (Cal/m^2/hr) = \frac{ \dot{V}_{o_2} (L/hr) \times CalEqi \ O_2 \ (Cal/hr)}{ BSA \ (m^2) } -->
<!-- $$ -->
<!-- ![equation](https://latex.codecogs.com/svg.latex?Met%20%5C%20Rate%20%5C%20%28Cal/m%5E2/hr%29%20%3D%20%5Cfrac%7B%20%5Cdot%7BV%7D_%7Bo_2%7D%20%28L/hr%29%20%5Ctimes%20CalEqi%20%5C%20O_2%20%5C%20%28Cal/hr%29%7D%7B%20BSA%20%5C%20%28m%5E2%29%20%7D) -->
\
$$
Met \ Rate \ (Cal/m^2/hr) = \frac{ \dot{V}_{o_2} (L/hr) \times CalEqi \ O_2 \ (Cal/hr)}{ BSA \ (m^2) }
$$
\
`get_metabolic_rate()` is a final wrapper function that calculate metabolic rate; moreover, it also reports metabolic rate along with oxygen consumption and other related parameters printed to the R console.
```{r}
get_metabolic_rate(x = 15, # displacement in x-direction = 15 mm
y = 80, # displacement in y-direction = 80 mm
paper_speed = 25, # Paper speed of the kymograph = 25 mm/min
baro = 760, # Barometric pressure at the recording site.
temp_c = 25, # Temperature in celsius at the recording site.
wt_kg = 70, # Subject's weight in kilogram
ht_cm = 180, # Subject's height in centimetre
cal_eqi_oxygen = 4.825 # Caloric equivalent of Oxygen at RQ = 0.82
)
```
## Simulation of Harvard Spirometer Tracing
### Simulate Data
In this section, contrary to the previous, we will simulate data to plot Harvard spirometer tracing from the respiratory parameters.
\
`sim_Harvard_tracing()` is the function to simulates volume-time tracing data produced by breathing of a hypothetical subject as recorded by the Harvard spirometer.
The input parameters can be categorized into the followings:
- **Function to Simulate 1 Respiratory Cycle:** as supply by the argument `f`, the default, currently, is a cosine (`cos`) function which might give a rough representation of 1 respiratory cycle. (It is not a physiologic representation of respiratory waveform. I still need more research on this.)
- **Amplitude and Wavelength of the sinusoidal function:** this can be calculated by knowing the Respiratory Rate (`RR`), Tidal Volume (`TV`), and paper speed (`paper_speed`)
- **Slope and Y-intercept of an Oxygen Line:** the oxygen line, i.e., the linear line fitted from the lowest points of each respiratory waveform, can be simulated by knowing its slope ($\beta_1$), y-intercept ($\beta_0$), and random error term ($\epsilon$).
- **Slope ($\beta_1$)** can be calculated from an oxygen consumption (argument `oxycons`) and paper speed (`paper_speed`).
- **Y-intercept ($\beta_0$)** can be specified directly from the user.
- And, **a random error ($\epsilon$)** can be generated from a Gaussian distribution with $\mu$ = 0, and `sd` as specified.
```{r tracing_df}
tracing_df <- sim_Harvard_tracing(
f = "cos", # Cosine function to represent 1 respiratory cycle
t_start = 0, # First `x` value will be started at time = 0 minute
t_end = 1, # Last `x` value will be ended at time = 1 minute
paper_speed = 25, # Paper speed in mm/minute
y_int_O2_line = 5, # Y-intercept of an oxygen line
oxycons = 10, # Oxygen consumption is 10 L/hr (default unit)
TV = 500, # Tidal Volume is 500 ml
RR = 20, # Respiratory Rate is 20 /min
seq_x_by = 0.01, # This control resolution of simulated data
epsilon_sd = 0.3, # Add random variation sampled from Gaussian distribution with mean = 0, sd = 0.3
seed = 1, # Seed to generate the random variation
)
head(tracing_df)
```
### Plot
With a little help of `{ggplot2}` package, the tracing can then be visualized.
```{r}
library(ggplot2)
library(latex2exp)
theme_set(theme_bw())
```
```{r}
ggplot(tracing_df) +
geom_path(aes(x, y)) +
geom_smooth(aes(x, y_O2_line), method = "lm", formula = "y~x",
linetype = "dashed") +
expand_limits(y = 0) +
labs(title = "Simulation of Harvard Spirometer Tracing",
subtitle = TeX("(TV = 500 ml, RR = 20/min, $\\dot{V}_{O_2}$ = 10 L/hr)"),
x = "x (mm)", y = "y (mm)")
```
### Behind the Tracing
If you curious about the **mathematical model** that underlies this simulated tracing, please visit [this article](https://lightbridge-ks.github.io/rslab/articles/mod-tracing.html).