-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
90 lines (62 loc) · 2.71 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
---
title: "CVglasso"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(magrittr)
```
[![Build Status](https://travis-ci.org/MGallow/CVglasso.svg?branch=master)](https://travis-ci.org/MGallow/CVglasso)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/CVglasso)](https://cran.r-project.org/package=CVglasso)
## Overview
`CVglasso` is an R package that estimates a lasso-penalized precision matrix via block-wise coordinate descent -- also known as the graphical lasso (glasso) algorithm. This package is a simple wrapper around the popular [glasso](https://cran.r-project.org/web/packages/glasso/index.html) package and extends and enhances its capabilities. These enhancements include built-in cross validation and visualizations.
<p align="center">
<img src = "https://github.com/MGallow/CVglasso/raw/master/vignettes/images/gif.gif"/>
</p>
A (possibly incomplete) list of functions contained in the package can be found below:
* `CVglasso()` computes the estimated precision matrix
* `plot.CVglasso()` produces a heat map or line graph for cross validation errors
See package [website](https://mgallow.github.io/CVglasso/) or [manual](https://github.com/MGallow/CVglasso/blob/master/CVglasso.pdf).
## Installation
```{r, eval = FALSE}
# The easiest way to install is from CRAN
install.packages("CVglasso")
# You can also install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("MGallow/CVglasso")
```
If there are any issues/bugs, please let me know: [github](https://github.com/MGallow/CVglasso/issues). You can also contact me via my [website](https://mgallow.github.io/). Pull requests are welcome!
## Usage
```{r, message = FALSE}
library(CVglasso)
set.seed(123)
# generate data from a sparse oracle precision matrix
# we will try to estimate this matrix from the data
# first compute the oracle covariance matrix
S = matrix(0.7, nrow = 5, ncol = 5)
for (i in 1:5){
for (j in 1:5){
S[i, j] = S[i, j]^abs(i - j)
}
}
# print oracle precision matrix (shrinkage might be useful)
(Omega = round(qr.solve(S), 3))
# generate 100 x 5 matrix with rows drawn from iid N_p(0, S)
set.seed(123)
Z = matrix(rnorm(100*5), nrow = 100, ncol = 5)
out = eigen(S, symmetric = TRUE)
S.sqrt = out$vectors %*% diag(out$values^0.5) %*% t(out$vectors)
X = Z %*% S.sqrt
# calculate sample covariance
sample = (nrow(X) - 1)/nrow(X)*cov(X)
# print sample precision matrix (perhaps a bad estimate)
round(qr.solve(cov(X)), 5)
# CVglasso (lam = 0.5)
CVglasso(S = sample, lam = 0.5)
# cross validation
(CV = CVglasso(X, trace = "none"))
# produce line graph for CV errors for CVGLASSO
plot(CV)
# produce CV heat map for CVGLASSO
plot(CV, type = "heatmap")
```