# My first R package on CRAN

A couple of weeks ago I have released my first R package on CRAN. For me it turned out to be a far less painful process than many people on the internet portray it to be (even though the package uses quite a lot of C++ code via Rcpp and RcppEigen, and even though R CMD check returns two NOTEs). Some of the most helpful resources for publishing the package were:

- Of course, the chapter “Releasing a package” from Hadley Wickham’s book
- Julia Silge’s post “How I Learned to Stop Worrying and Love R CMD Check”
- Karl Broman’s post on getting an R package to CRAN

### grpSLOPE

My package is called `grpSLOPE`

(github, CRAN), and I will very briefly introduce some of it’s functionality. The purpose of the Group SLOPE method is to fit a sparse linear regression model, while controlling the *group false discovery rate*. I illustrate what it means with a very simple example below (for some more detail see the vignette).

We generate a random model with a design matrix $X \in \mathbb{R}^{1000 \times 2000}$, whose entries are sampled from the standard normal distribution, and a coefficient vector $\beta$, whose entries are mostly zeros.

```
<span class="n">set.seed</span><span class="p">(</span><span class="m">1</span><span class="p">)</span>
<span class="n">X</span> <span class="o"><-</span> <span class="n">matrix</span><span class="p">(</span><span class="n">rnorm</span><span class="p">(</span><span class="m">1000</span><span class="o">*</span><span class="m">2000</span><span class="p">),</span> <span class="m">1000</span><span class="p">,</span> <span class="m">2000</span><span class="p">)</span>
<span class="n">beta</span> <span class="o"><-</span> <span class="n">rep</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">2000</span><span class="p">)</span>
<span class="n">beta</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">20</span><span class="p">]</span> <span class="o"><-</span> <span class="n">runif</span><span class="p">(</span><span class="m">20</span><span class="p">)</span>
<span class="n">beta</span><span class="p">[</span><span class="m">1901</span><span class="o">:</span><span class="m">1980</span><span class="p">]</span> <span class="o"><-</span> <span class="n">runif</span><span class="p">(</span><span class="m">80</span><span class="p">)</span>
<span class="n">y</span> <span class="o"><-</span> <span class="n">X</span> <span class="o">%*%</span> <span class="n">beta</span> <span class="o">+</span> <span class="n">rnorm</span><span class="p">(</span><span class="m">1000</span><span class="p">)</span>
```

We assume that the predictors (i.e., the columns of $X$) are divided into 200 blocks, each of size 10, and we want to determine which groups of predictors have an effect on the response variable $y$. That is, given the noisy observations of $y$, we want to find out, which blocks of the vector $\beta$ contain non-zero entries. As you can see above, $\beta$ has 10 non-zero blocks. Additionally, we want to make sure that we don’t make too many false discoveries in the process (i.e., we want the method to keep the expected proportion of falsely selected groups below, say, 10%). That’s exactly what the Group SLOPE method is for!

```
<span class="c1">#install.packages("grpSLOPE") # Install the package now easily from CRAN!
</span><span class="n">library</span><span class="p">(</span><span class="n">grpSLOPE</span><span class="p">)</span>
<span class="n">group</span> <span class="o"><-</span> <span class="n">rep</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">200</span><span class="p">,</span> <span class="n">each</span><span class="o">=</span><span class="m">10</span><span class="p">)</span>
<span class="n">grpSLOPE.result</span> <span class="o"><-</span> <span class="n">grpSLOPE</span><span class="p">(</span><span class="n">X</span><span class="o">=</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">y</span><span class="p">,</span> <span class="n">group</span><span class="o">=</span><span class="n">group</span><span class="p">,</span> <span class="n">fdr</span><span class="o">=</span><span class="m">0.1</span><span class="p">)</span>
```

We observe that the method has indeed selected the correct groups of predictors:

```
<span class="n">grpSLOPE.result</span><span class="o">$</span><span class="n">selected</span>
<span class="c1"># [1] "1" "2" "191" "192" "193" "194" "195" "196" "197" "198"
</span>
```

And the false discovery proportion is indeed below the target of 10% in this case, while the power is at 100%:

```
<span class="n">truly.significant.groups</span> <span class="o"><-</span> <span class="n">unique</span><span class="p">(</span><span class="n">group</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">beta</span> <span class="o">!=</span> <span class="m">0</span><span class="p">)])</span>
<span class="n">truly.significant.groups</span>
<span class="c1"># [1] 1 2 191 192 193 194 195 196 197 198
</span>
<span class="n">false.discoveries</span> <span class="o"><-</span> <span class="n">setdiff</span><span class="p">(</span><span class="n">grpSLOPE.result</span><span class="o">$</span><span class="n">selected</span><span class="p">,</span> <span class="n">truly.significant.groups</span><span class="p">)</span>
<span class="n">fdp</span> <span class="o"><-</span> <span class="n">length</span><span class="p">(</span><span class="n">false.discoveries</span><span class="p">)</span> <span class="o">/</span> <span class="n">length</span><span class="p">(</span><span class="n">grpSLOPE.result</span><span class="o">$</span><span class="n">selected</span><span class="p">)</span>
<span class="n">fdp</span>
<span class="c1"># [1] 0
</span>
<span class="n">true.discoveries</span> <span class="o"><-</span> <span class="n">intersect</span><span class="p">(</span><span class="n">grpSLOPE.result</span><span class="o">$</span><span class="n">selected</span><span class="p">,</span> <span class="n">truly.significant.groups</span><span class="p">)</span>
<span class="n">power</span> <span class="o"><-</span> <span class="n">length</span><span class="p">(</span><span class="n">true.discoveries</span><span class="p">)</span> <span class="o">/</span> <span class="n">length</span><span class="p">(</span><span class="n">grpSLOPE.result</span><span class="o">$</span><span class="n">selected</span><span class="p">)</span>
<span class="n">power</span>
<span class="c1"># [1] 1
</span>
```

Of course, the method does not perform this good under all circumstances — the interested reader can learn more from the papers referred to in the grpSLOPE README on github (unfortunately, the “real” journal publication is still under preparation, and another related journal publication is currently under review, so, not publicly available yet either).