---
title: "Weight of partitions"
author: "Ingo Rohlfing"
date: "`r format(Sys.Date())`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Weight of partitions}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r setup, message = F, warning = F}
library(QCAcluster)
library(knitr) # nicer html tables
```

### Conservative and parsimonious solution
We use the data from [Thiem (2011)](https://doi.org/10.1017/S1755773910000251) for illustrating how the function `wop()` calculates the weights of partitions. The weight of a partition is defined on the level of individual models and can be calculated for the *consistency* and *coverage* value of a model that has been derived from the pooled data. The weight of a partition for the consistency value of the pooled solution is calculated by applying the consistency formula only to the cases that belong to a partition. The weight of partition is calculated in *absolute* terms by calculating separately its contribution to the numerator and denominator of the formula. When one divides the partition-specific absolute contribution to the numerator by the contribution to the denominator, then one receives the partition-specific consistency or coverage score (depending on the type of formula).

The arguments of the functions are:

- `n_cut`: Frequency threshold for pooled data
- `incl_cut`: Inclusion threshold (a.k.a. consistency threshold) for pooled data
- `solution`: Either `C` for conservative solution (a.k.a. complex solution) or `P` for parsimonious solution
- `amb_selector`: Numerical value for selecting a single model in the presence of model ambiguity. Models are numbered according to their order produced by minimize by the QCA package. 

```{r}
# load data (see data description for details)
data("Thiem2011")
# calculate weight of partitions
wop_pars <- wop(
  dataset = Thiem2011,
  units = "country", time = "year",
  cond = c("fedismfs", "homogtyfs", "powdifffs", "comptvnsfs", "pubsupfs", "ecodpcefs"),
  out = "memberfs",
  n_cut = 6, incl_cut = 0.8,
  solution = "P",
  amb_selector = 1)
kable(wop_pars)
```

When one aggregates the partition-specific absolute weights for the between-dimension or within-dimension, one gets the absolute value for the pooled solution. We illustrate this with the following chunk
```{r}
# sum over all cross-sections for consistency denominator
sum(wop_pars[wop_pars$type == "between", ]$denom_cons)
# sum over all time series for coverage  numerator
sum(wop_pars[wop_pars$type == "within", ]$num_cov)
```

On the basis of the absolute weights, one can calculate the *relative weight* of a partition by dividing its absolute contribution by the corresponding value for the pooled solution.
```{r}
# relative contribution of cross sections to denominator for consistency
wop_between  <- wop_pars[wop_pars$type == "between", ]
wop_between$rel_denom_cons <- round(wop_between$denom_cons / 
  sum(wop_between$denom_cons), digits = 2)
kable(wop_between)
```


### Intermediate solution
The weight of partitions for intermediate solutions is produced with `wop_inter()`. We use data from [Schwarz 2016](https://doi.org/10.1080/07036337.2016.1203309) to illustrate the function.

```{r, eval = F}
# load data (see data description for details)
data("Schwarz2016")
# calculating weight of partitions
Schwarz_wop_inter <- partition_min_inter(
  Schwarz2016,
  units = "country", time = "year",
  cond = c("poltrans", "ecotrans", "reform", "conflict", "attention"),
  out = "enlarge",
  n_cut = 1, incl_cut = 0.8,
  intermediate = c("1", "1", "1", "1", "1"))
kable(Schwarz_wop_inter)
```

### Other packages used in this vignette
 Yihui Xie (2021): *knitr: A General-Purpose Package for Dynamic Report Generation in
 R.* R package version 1.33.

 Yihui Xie (2015): *Dynamic Documents with R and knitr.* 2nd edition. Chapman and
 Hall/CRC. ISBN 978-1498716963

 Yihui Xie (2014): *knitr: A Comprehensive Tool for Reproducible Research in R.* In
 Victoria Stodden, Friedrich Leisch and Roger D. Peng, editors, Implementing
 Reproducible Computational Research. Chapman and Hall/CRC. ISBN 978-1466561595
