-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
92 lines (68 loc) · 2.83 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(SDA4D)
```
# SDA4D
An R package providing an implementation of a variational Bayes algorithm for
four dimensional Parallel Factor Analysis.
## Installation
You can install the released version of SDA4D from the marchinilab github with
``` r
install.packages('devtools')
devtools::install_github("marchinilab/SDA4D")
```
## Example
#### Generating a data tensor
We provide a function to generate a data tensor similar to those we used in simulations, named `generate_example_data` which also returns a list of the matrices used to generate it.
```{r generatedata}
library(SDA4D)
set.seed(42)
generatedData <- generate_example_data()
lapply(generatedData,dim)
```
#### Performing a short run of the tensor decomposition
Now, setting `stopping=FALSE` to ignore the stopping criterion we run the tensor decomposition with the function `RunSDA4D` for 100 iterations as follows:
```{r runmethod,results=FALSE}
res<-RunSDA4D(data_tensor = generatedData$dataTensor,
dimn_vector = c(200,500,16,3),
max_iters = 100,
num_components = 8,
stopping=FALSE)
```
Note that we have suppressed the console output from this run in the file here.
In the output we have the evidence lower bound (ELBO, also sometimes referred to as the negative free energy), recorded at each iteration, the maximum number of iterations, and a number of other parameters. Note that in the code we have omitted constant terms from the ELBO.
```{r plotELBO,fig.width=8,fig.height=6,echo=FALSE}
#res<-list(Neg_FE=exp(rnorm(100)))
# plot(-log(-res$Neg_FE[-1]),col='red',pch=4,xlab='Iteration',main='log(ELBO) over 100 iterations')
# lines(-log(-res$Neg_FE[-1]),col='red')
```
```{r plotELBOqplot,echo=FALSE,fig.width=6,fig.height=4}
library(ggplot2)
startfrom=5
ELBO_plot<- qplot(x=c(startfrom:length(res$ELBO)),
y=res$ELBO[-c(1:(startfrom-1))],
geom=c("point", "line")
)+
ylab('ELBO')+
xlab('Iteration')+
ggtitle('ELBO over 100 iterations')
suppressWarnings(print(ELBO_plot))
```
#### The output
The output of the main function is a list of a number of components:
The main RunSDA4D provides an interface to an implementation of a Bayesian sparse parallel factor analysis model for four-dimensional tensor decomposition. The output therefore consists of a list of the approximate posterior distributions of the main parameters, in the form of their parameters, or point estimates where these were calculated instead.
```{r outputnames}
names(res)
res$maximumiteration
length(res$ELBO)
```
See the vignette in the doc directory for a description of the output.