---
title: "BSPBSS-vignette"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{BSPBSS-vignette}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r setup, message=FALSE}
library(BSPBSS)
```

## A toy example

This is a basic example which shows you how to solve a common problem.

First we load the package and generate simulated images with a probabilistic ICA model:

```{r, message=FALSE}
library(BSPBSS)
set.seed(612)
sim = sim_2Dimage(length = 30, sigma = 5e-4, n = 30, smooth = 6)
```

The true source signals are three 2D geometric patterns (set `smooth=0` to generate patterns with sharp edges).

```{r}
levelplot2D(sim$S,lim = c(-0.04,0.04), sim$coords)
```

which generate observed images such as 
```{r}
levelplot2D(sim$X[1:3,], lim = c(-0.12,0.12), sim$coords)
```

Then we generate initial values for mcmc,
```{r}
ini = init_bspbss(sim$X, sim$coords, q = 3, ker_par = c(0.1,50), num_eigen = 50)
```

and run!
```{r, message = TRUE}
res = mcmc_bspbss(ini$X,ini$init,ini$prior,ini$kernel,n.iter=2000,n.burn_in=1000,thin=10,show_step=100)
```

Then the results can be summarized by
```{r}
res_sum = sum_mcmc_bspbss(res, ini$X, ini$kernel, start = 101, end = 200, select_p = 0.5)
```

and shown by 
```{r}
levelplot2D(res_sum$S, lim = c(-1.3,1.3), sim$coords)
```

For comparison, we show the estimated sources provided by informax ICA here.
```{r}
levelplot2D(ini$init$ICA_S, lim = c(-1.7,1.7), sim$coords)
```