---
title: "Overview of Pediatric Blood Pressure Distributions"
output:
 rmarkdown::html_vignette:
   toc: true
   number_sections: true
bibliography: references.bib
vignette: >
 %\VignetteIndexEntry{Overview of Pediatric Blood Pressure Distributions}
 %\VignetteEngine{knitr::rmarkdown}
 %\VignetteEncoding{UTF-8}
---


```{r label = "setup", include = FALSE}
################################################################################
#                        !!! DO NOT EDIT .Rmd files !!!                        #
#                                                                              #
# .Rmd files are generated by their corresponding .R files found in the        #
# vignette-spinners/ directory.  Any changes needed to the .Rmd file need to   #
# be made in the .R file                                                       #
################################################################################
knitr::opts_chunk$set(collapse = TRUE, fig.align = "center")
library(qwraps2)
```




```{r label = "helper functions", include = FALSE}
percentile_factor <- function(p) {
  factor(p, levels = sort(unique(p)), labels = paste0(sort(unique(p)) * 100, "th"))
}
```




```{r}
library(pedbp)
```


# Introduction

Part of the work of @martin2022machine required transforming blood
pressure measurement into percentiles based on published norms.  This
work was complicated by the fact that data for pediatric blood pressure
percentiles is sparse and generally only applicable to children at least one
year of age and requires height, a commonly unavailable data point in
electronic health records for a variety of reasons.

A solution to building pediatric blood pressure percentiles was developed and
is presented here for others to use.  Inputs for the developed method are:

1. Patient sex (male/female) _required_
2. Systolic blood pressure (mmHg) _required_
3. Diastolic blood pressure (mmHg) _required_
4. Patient height (cm) _if known_.

Given the inputs, the following logic is used to determine which data sets
will be used to inform the blood pressure percentiles.  Under one year of
age, the data from @gemelli1990longitudinal will be used; a height input is
not required for this patient subset. For those at least one year of age with
a known height, data from @nhlbi2011expert (hereafter referred to as
'NHLBI/CDC' as the report incorporates recommendations and inputs from the
National Heart, Lung, and Blood Institute [NHLBI] and the Centers for Disease
Control and Prevention [CDC]). If height is unknown and age is at least three
years, then data from @lo2013prehypertension is used.  Lastly, for children
between one and three years of age with unknown height, blood pressure
percentiles are estimated by the NHLBI/CDC data using as a default the median
height for each patient's sex and age.


```{r echo = FALSE, results = "asis"}
knitr::include_graphics("../man/figures/flowchart.png")
```


With version 2.0.0 and later, the option to select the specific reference
source data was introduced along with the additional @flynn2017clinical
reference.

The sources are:

1. @gemelli1990longitudinal for kids under one year of age.
2. @lo2013prehypertension for kids over three years of age and when height is unknown.
3. @nhlbi2011expert for kids 1 through 18 years of age with known stature.
4. @flynn2017clinical for kids 1 through 18 years of age with known stature.

The data from @flynn2017clinical and @nhlbi2011expert are similar but for one
major difference.  @flynn2017clinical excluded overweight and obese ( BMI
above the 85th percentile) children and @nhlbi2011expert included overweight
and obese children.

# Estimating Pediatric Blood Pressure Distributions

There are two functions provided for working with blood pressure
distributions.  These methods use Gaussian distributions for both systolic
and diastolic blood pressures with means and standard deviations either
explicitly provided in an aforementioned source or derived by optimizing the parameters
such that the sum of squared errors between the provided quantiles from an
aforementioned source and the distribution quantiles is minimized.  The
provided functions, a distribution function and a quantile function, follow a
similar naming convention to the distribution functions found in the stats
library in R.

## Percentiles

What percentile for systolic and diastolic blood pressure is 100/60 for a 44
month old male with unknown height?


```{r}
p_bp(q_sbp = 100, q_dbp = 60, age = 44, male = 1)
```


Using the default source of
`r  qwraps2::backtick(martin2022)  `
the data source for the above is @lo2013prehypertension since height was not
specified.  The same result could be found by explicitly using the
`r  qwraps2::backtick(lo2013)  `
source.

```{r}
p_bp(q_sbp = 100, q_dbp = 60, age = 44, male = 1, source = "lo2013")
```


Those percentiles would be modified if height was 103 cm:

```{r}
p_bp(q_sbp = 100, q_dbp = 60, age = 44, male = 1, height = 103)
p_bp(q_sbp = 100, q_dbp = 60, age = 44, male = 1, height = 103, source = "nhlbi")
```


If you don't have the height, but you do have the height percentiles you can
use that instead:

```{r}
p_height_for_age(103, male = 1, age = 44)
x <- p_bp(q_sbp = 100, q_dbp = 60, age = 44, male = 1, height_percentile = 0.80, source = "nhlbi")
x
```


A plotting method to show where the observed blood pressures are on the
distribution function is also provided.

```{r fig.width = 5, fig.height = 5}
bp_cdf(sbp = 100, dbp = 60, age = 44, male = 1, height_percentile = 0.80, source = "nhlbi")
```


Vectors of blood pressures can be used as well.
`r qwraps2::backtick(NA) `
values will return
`r qwraps2::backtick(NA) %s% "." `


```{r}
bps <-
  p_bp(
         q_sbp  = c(100, NA, 90)
       , q_dbp  = c(60, 82, 48)
       , age    = 44
       , male   = 1
      )
bps
```


If you want to know which data source was used in computing each of the
percentile estimates you can look at the
`r  qwraps2::backtick(bp_params)  `
attribute:

```{r}
attr(bps, "bp_params")
str(bps)
```


## Quantiles

If you have a percentile value and want to know the associated
systolic and diastolic blood pressures:


```{r}
q_bp(
       p_sbp = c(0.701, NA, 0.36)
     , p_dbp = c(0.85, 0.99, 0.50)
     , age = 44
     , male = 1
    )
```



## Working With More Than One Patient

The
`r  qwraps2::backtick(p_bp)  `
and
`r  qwraps2::backtick(q_bp)  `
methods are designed accept vectors for each of the arguments.
These methods expected each argument to be length 1 or all the same length.


```{r label = "bp_batch_example"}
eg_data <- read.csv(system.file("example_data", "for_batch.csv", package = "pedbp"))
eg_data

bp_percentiles <-
  p_bp(
         q_sbp  = eg_data$sbp..mmHg.
       , q_dbp  = eg_data$dbp..mmHg.
       , age    = eg_data$age
       , male   = eg_data$male
       , height = eg_data$height
       )
bp_percentiles

str(bp_percentiles)
```


Going from percentiles back to quantiles:

```{r}
q_bp(
       p_sbp  = bp_percentiles$sbp_p
     , p_dbp  = bp_percentiles$dbp_p
     , age    = eg_data$age
     , male   = eg_data$male
     , height = eg_data$height
     )
```


# Blood Pressure Charts

Percentiles over age:

```{r}
bp_chart()
```


The percentiles curves for a males in the 75th height percentile based on the
@flynn2017clinical data:

```{r}
bp_chart(male = 1, height_percentile = 75, source = "flynn2017")
```


# References
<div id="refs"></div>

