---
title: "Percentage-Change Approach to Clinical Significance in R"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Percentage-Change Approach to Clinical Significance in R}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

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

# Introduction
In this tutorial, we will explore the percentage-change approach to clinical significance using R. The percentage-change approach is centered around a predefined relative change, expressed in percent, that needs to be achieved in order to inferr a clinically significant change. If, for instance, the percentage-change cutoff is believed to be 30%, and a patient demonstrated a score change of at least 30%, then this change is believed to be clinically significant. We will be working with the `antidepressants` and `claus_2020` datasets, and the `cs_percentage()` function to demonstrate various aspects of this approach.


## Prerequisites
Before we begin, ensure that you have the following prerequisites in place:

- R installed on your computer.
- Basic understanding of R programming concepts.


## Looking at the Datasets
First, let's have a look at the datasets, which come with the package.

```{r}
library(clinicalsignificance)

antidepressants
claus_2020
```


# Percentage-Change Approach
The `cs_percentage()` function is a tool for assessing clinical significance. It allows you to determine if changes in patient outcomes are practically significant. Let's go through the basic usage and some advanced features of this function.


## Basic Analysis
Let's start with a basic clinical significance distribution analysis using the `antidepressants` dataset. We are interested in the Mind over Mood Depression Inventory (`mom_di`) measurements and want to set the percentage cutoff for an improvement to a value of 0.3.
```{r}
pct_results <- antidepressants |> 
  cs_percentage(
    id = patient,
    time = measurement,
    outcome = mom_di,
    pct_improvement = 0.3
  )
```


## Handling Warnings
Sometimes, as in the example above, you may encounter warnings when using this function. You can turn off the warning by explicitly specifying the pre-measurement time point using the pre parameter. This can be helpful when your data lacks clear pre-post measurement labels.


```{r}
# Turning off the warning by specifying pre-measurement time
pct_results <- antidepressants |>
  cs_percentage(
    id = patient,
    time = measurement,
    outcome = mom_di,
    pre = "Before",
    pct_improvement = 0.5
  )
```

Here's a breakdown of the code:

- `patient`, `measurement`, and `mom_di` represent the patient identifier, assessment time points, and HAM-D scores, respectively.
- `pre` and `post` specify the time points for the pre and post-assessment.
- `pct_improvement` sets the percentage change threshold for improvement to 30%.


## Printing and Summarizing the Results
```{r}
# Print the results
pct_results

# Get a summary
summary(pct_results)
```


## Visualizing the Results
Visualizing the results can help you better understand the clinical significance of changes in patient outcomes.
```{r}
# Plot the results
plot(pct_results)

# Show clinical significance categories
plot(pct_results, show = category)
```


## Data with More Than Two Measurements
When working with data that has more than two measurements, you must explicitly define the pre and post measurement time points using the `pre` and `post` parameters.

```{r}
# Clinical significance distribution analysis with more than two measurements
cs_results <- claus_2020 |>
  cs_percentage(
    id,
    time,
    bdi,
    pre = 1,
    post = 4,
    pct_improvement = 0.3
  )

# Display the results
cs_results
summary(cs_results)
plot(cs_results)
```


## Setting Different Thresholds
You can set different thresholds for improvement and deterioration by adjusting the `pct_deterioration` argument Let's see an example:
```{r}
# Clinical significance analysis with different improvement and deterioration thresholds
cs_results_2 <- claus_2020 |>
  cs_percentage(
    id = id,
    time = time,
    outcome = hamd,
    pre = 1,
    post = 4,
    pct_improvement = 0.3,
    pct_deterioration = 0.2
  )

# Display the results
cs_results_2

# Visualize the results
plot(cs_results_2)
```


## Grouped Analysis
You can also perform a grouped analysis by providing a group column from the data. This is useful when comparing treatment groups or other categories.
```{r}
cs_results_grouped <- claus_2020 |>
  cs_percentage(
    id = id,
    time = time,
    outcome = hamd,
    pre = 1,
    post = 4,
    pct_improvement = 0.3,
    group = treatment
  )

# Display and visualize the results
cs_results_grouped
plot(cs_results_grouped)
```


## Analyzing Positive Outcomes
In some cases, higher values of an outcome may be considered better. You can specify this using the `better_is` argument. Let's see an example with the WHO-5 score where higher values are considered better.
```{r}
# Clinical significance analysis for outcomes where higher values are better
cs_results_who <- claus_2020 |>
  cs_percentage(
    id = id,
    time = time,
    outcome = who,
    pre = 1,
    post = 4,
    pct_improvement = 0.3,
    better_is = "higher"
  )

# Display the results
cs_results_who
```


# Conclusion
In this tutorial, you've learned how to perform clinical significance analysis using the `cs_percentage()` function in R. This analysis may be crucial for determining the practical importance of changes in patient outcomes. By adjusting thresholds and considering grouped analyses, you can gain valuable insights for healthcare and clinical research applications.
