-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
80 lines (57 loc) · 2 KB
/
README.Rmd
File metadata and controls
80 lines (57 loc) · 2 KB
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# SmoothHOOI
<!-- badges: start -->
<!-- badges: end -->
SmoothHOOI is an R package that implements methods described in "Smooth Tensor Decomposition with Application to Ambulatory Blood Pressure Monitoring Data". The application to real and synthetic ABPM data can be found [here](https://github.com/IrinaStatsLab/SmoothTensorDecompositionABPM).
## Installation
You can install the development version of SmoothHOOI from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("IrinaStatsLab/SmoothHOOI")
```
## Example
This is a simple example which shows you how to use this package.
```{r}
library(SmoothHOOI)
set.seed(1234321)
# Generate a random tensor with missing data, in the array format
dims <- c(24, 3, 207)
tnsr <- array(rnorm(prod(dims), mean = 0, sd = 1), dim = dims)
missing_prob <- 0.2
missing_indices <- sample(length(tnsr), size = floor(length(tnsr) * missing_prob))
tnsr[missing_indices] <- NA
```
```{r}
# Make the second order difference matrix with circular nature
D2 <- SecDiffMat(24)
```
```{r}
# Find optimal hyperparameter with 5-fold cross-validation
kcv_res <- kcv(tnsr, rank_grid=as.matrix(expand.grid(r1<-seq(3,6,by=1), r2<-c(2,3))), lambda_seq=seq(1,10,by=1), k=5, L0=NULL, D=D2, tol=0.01, max_iter=500, init=0)
kcv_res$MSE_mat # matrix for CV error (rows representing ranks, cols representing lambda)
kcv_res$opt_para # optimal hyperparameters
```
```{r}
# Run SmoothHOOI algorithm with the optimal hyperparameters
res <- mglram(tnsr, ranks = c(3, 2), init=0, D = D2,
lambda = 10, max_iter = 500, tol = 1e-5, L0 = NULL)
res$conv # check convergence
```
```{r}
# Rotation for Identifiability
tilde <- MakeIdent(L=res$L, G=res$G, R=res$R)
tilde$L_tilde
tilde$R_tilde
tilde$G_tilde[ , ,1]
```