---
title: "Random Weights"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Random Weights}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

## Random Weights

When doing a quantitative benefit/risk analysis, it may be of interest to add some uncertainty around the weights.  If this is the case, one can specify a vector of weights in lieu of a single weight:

```{r setup}
library(brisk)
library(dplyr)

benefit_fun <- approxfun(c(0, 0.5, 1), c(0, 0.2, 1))
risk_fun <- approxfun(c(0, 0.3, 0.6, 1), c(1, 0.9, 0.2, 0))

# weights
w1 <- runif(1e4, 0.6, 0.8)
w2 <- runif(1e4, 0.2, 0.3)

set.seed(1132)
out <- br(
  benefit("response", benefit_fun, weight = w1),
  risk("side_effect", risk_fun, weight = w2),
  br_group(
    label = "placebo",
    response = rbeta(1e4, 1 + 30, 1 + 70),
    side_effect = rbeta(1e4, 1 + 3, 1 + 97)
  ),
  br_group(
    label = "drug",
    response = rbeta(1e4, 1 + 60, 1 + 40),
    side_effect = rbeta(1e4, 1 + 40, 1 + 60)
  )
)

head(select(out, iter, response_weight, side_effect_weight))
```

In some cases is may be desirable to have weights which sum to 1 (e.g. when using the `mcda()` function).  If this is the case, one can use the `sim_weights()` function to generate a set of weights which sums to 1.  One needs to provide the number of weights to generate, and upper/lower bounds for each endpoint.  The function then generates weights using the bounds.  For instance, the following code will generate weights such that the ratio of side effect to response is from 0.2 to 0.3:

```{r}
w <- sim_weights(1e4, response = c(1, 1), side_effect = c(0.2, 0.3))
head(w)
summary(w$side_effect / w$response)
```

These weights can then be used in the benefit-risk analyses:

```{r}
out2 <- br(
  benefit("response", benefit_fun, weight = w$response),
  risk("side_effect", risk_fun, weight = w$side_effect),
  br_group(
    label = "placebo",
    response = rbeta(1e4, 1 + 30, 1 + 70),
    side_effect = rbeta(1e4, 1 + 3, 1 + 97)
  ),
  br_group(
    label = "drug",
    response = rbeta(1e4, 1 + 60, 1 + 40),
    side_effect = rbeta(1e4, 1 + 40, 1 + 60)
  )
)

head(select(out2, iter, response_weight, side_effect_weight))
```
