---
title: "COCOREG basics"
author: "Jussi Korpela"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{COCOREG basics}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


```{r, include = FALSE}
library(cocoreg)
```

COCOREG is an R package for extracting shared variation between datasets using regression models. The details of the algorithm are to be described in a paper:

Korpela J, Henelius A, Ahonen L, Klami A, Puolamäki K (2016) Using regression makes extraction of shared variation in multiple datasets easy. Data Mining And Knowledge Discovery, submitted 2015.

In short, a chain of regression models is used to "filter" the data such that the output contains only variation that is shared by all input datasets. The shared variation is presented in the same space as the input data i.e. using the same variables as the input data.

## Basic usage

The following is a minimal usage example. It creates a toy data collection (i.e. a set of datasets), runs cocoreg on it and visualizes:

```{r, fig.show='hold', fig.height = 5}
library(cocoreg)
dc <- create_syn_data_toy()
ccr <- cocoreg(dc$data)
shared.by.all.df <- variation_shared_by(dc, 'all') #only on synthetic datasets

ggplot_dflst(dc$data, ncol = 1)
ggplot_dflst(ccr$data, ncol = 1)
```

To plot several data collections sidy-by-side use: 
```{r, fig.width = 7.1, fig.height = 7.5}
ggplot_dclst(list(observed = dc$data, shared = shared.by.all.df, cocoreg = ccr$data))
```

To compare two or more data collections variable by variable: 
```{r, fig.width = 7.1, fig.height = 7.5, message = FALSE}
library(reshape) #importing from namespace does not work as expected
ggcompare_dclst(list(shared = shared.by.all.df, cocoreg = ccr$data))
```

Other visualizations:
```{r, fig.width = 7.1, fig.height = 7.5}
ggplot_dclst(list(observed = dc$data, shared = shared.by.all.df, cocoreg = ccr$data),
              legendMode = 'all')
```
```{r, message = FALSE, fig.width = 7.1, fig.height = 5}
ggplot_dflst(dc$data, ncol=1)
ggplot_df(dc$data[[1]])
```
