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

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

...

We will first load an example dataset:

```{r}
dat <- get(data("BBC_pp15.1", package="behaviorchange"));
```

Then, for convenience, we will select all expectations (sometimes also called beliefs) relating to the effects of using a high dose of MDMA, and store those variable names in a vector:

```{r}
subdets <-
  grep(
    "highDose_AttBeliefs_",
    names(dat),
    value=TRUE
  );
target <- 'highDose_intention';
```

Here, we specify the survey questions (i.e. the subdeterminants). We do that manually, because the original study was in Dutch (so they can't be taken from the questionnaire automatically):

```{r labels}

translations <- matrix(
  c("If I use a high dose of ecstasy,\nmy trip is...", "shorter", "longer",
    "If I use a high dose of ecstasy,\nmy trip is...", "more mild", "more intense",
    "If I use a high dose of ecstasy,\nI get...", "much less\nintoxicated", "much more\nintoxicated",
    "A high dose of ecstasy gives me...", "much less\nenergy", "much more\nenergy",
    "With a high dose of ecstasy,\nthe happy, euphoric feeling is...", "much weaker", "much stronger",
    "If I use a high dose of ecstasy,\nI learn...", "much less\nabout myself", "much more\nabout myself",
    "If I use a high dose of ecstasy,\nI feel...", "much less\nconnected\n to others", "much more\nconnected\nto others",
    "If I use a high dose of ecstasy,\nI can make contact with others...", "much harder", "much easier",
    "If I use a high dose of ecstasy,\nI feel like having sex...", "much less", "much more",
    "If I use a high dose of ecstasy,\nI forget my problems...", "much slower", "much faster",
    "If I use a high dose of ecstasy,\nI feel...", "much less\nself-absorbed", "much more\nself-absorbed",
    "With a high dose of ecstasy,\nI can test my boundaries...", "much less", "much better",
    "If I use a high dose of ecstasy,\nthe music sounds...", "much worse", "much better",
    "If I use a high dose of ecstasy,\nI hallucinate...", "much less", "much more",
    "If I use a high dose of ecstasy,\nI feel time passes...", "much slower", "much faster",
    "If I use a high dose of ecstasy,\nafterwards I remember...", "much less", "much more",
    "For my health, using a high\ndose of ecstasy is...", "much worse", "much better",
    "My experience with using a high\ndose of ecstasy is...", "worse", "better",
    "Do you worry as much, more, or less\nabout the physical side effects of\nusing a high dose of ecstasy?", "worry\nmuch less", "worry\nmuch more",
    "Do you worry as much, more, or less\nabout the emotional and psychic side\neffects of using a high dose of ecstasy?", "worry\nmuch less", "worry\nmuch more",
    "Do you have as much, more, or less\nregret after using a high dose of ecstasy?", "much less\nregret", "much more\nregret"), ncol=3, byrow=TRUE);

subQuestions <- translations[, 1];
leftAnchors <- translations[, 2];
rightAnchors <- translations[, 3];

subDeterminantLabels <-
  paste0(subQuestions, " [ ",
         leftAnchors, " - ",
         rightAnchors, " ]");

```










```{r CIBER-1}

### Construct determinant importance plots
unsortedCIBERObject <-
  behaviorchange::CIBER(
    dat,
    determinants=subdets,
    targets=target,
    subQuestions=subQuestions,
    leftAnchors=leftAnchors,
    rightAnchors=rightAnchors,
    titleVarLabels=c('attitude', 'intention'),
    returnPlotOnly = FALSE,
    drawPlot=FALSE
  );

unsortedCIBER <- unsortedCIBERObject$output$plot;

sortedCIBER <-
  behaviorchange::CIBER(
    dat,
    determinants=subdets,
    targets=target,
    subQuestions=subQuestions,
    leftAnchors=leftAnchors,
    rightAnchors=rightAnchors,
    orderBy=target,
    numberSubQuestions = TRUE,
    titleVarLabels=c('attitude', 'intention'),
    drawPlot=FALSE
  );

```

## Unsorted Confidence Interval-Based Estimation of Relevance plot

```{r fig.cap="Unsorted Confidence Interval-Based Estimation of Relevance plot", fig.width=attr(unsortedCIBER, 'width'), fig.height=attr(unsortedCIBER, 'height'), results='asis'}
  grid::grid.draw(unsortedCIBER);
```

## Sorted Confidence Interval-Based Estimation of Relevance plot

```{r fig.cap="Sorted Confidence Interval-Based Estimation of Relevance plot", fig.width=attr(sortedCIBER, 'width'), fig.height=attr(sortedCIBER, 'height'), results='asis'}
  grid::grid.draw(sortedCIBER);
```




<!----------------------------------------------------------------------------->

## Determinant Selection Table

To select determinants it is important to inspect the univariate distributions, ideally while looking at the raw data, while simultaneously inspecting bivariate associations with the ultimate target of the intervention (usually a target behavior or proxy thereof). For this purpose, Confidence Interval-Based Estimation of Relevance plots have been developed ([`behaviorchange::CIBER()`](https://r-packages.gitlab.io/behaviorchange/reference/CIBER.html)).

However, sometimes too many determinants have to be compared, and it can be helpful to reduce the available information to facilitate retaining an overview. For this goal, the Potential for Change Index can be used. However, keep in mind that the practice of reducing relatively rich data to single quantitative estimates obfuscates many features of the determinant structures, and so should never be used without also inspecting the raw data.

The `behaviorchange` package has a function that allows you to quickly produce a table with the Potential for Change Indices for a series of (sub-)determinants. This table can be created with the [`behaviorchange::determinant_selection_table()`](https://r-packages.gitlab.io/behaviorchange/reference/potential_for_change.html) command.



Then we can produce the Determinant Selection Table, using the intention measure as target:

```{r}
behaviorchange::determinant_selection_table(
  data=dat,
  determinants = subdets,
  target = 'highDose_intention'
);
```

We can also easily sort by one of the columns to pass that column number as `sortBy`. For example, we can sort by the Potential for Change Index (which is in column 6) like this:

```{r}
behaviorchange::determinant_selection_table(
  data=dat,
  determinants = subdets,
  target = 'highDose_intention',
  sortBy=6
);
```

