# My first R package on CRAN

**Alexej's blog**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 , whose entries are sampled from the standard normal distribution, and a coefficient vector , 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="w">
</span><span class="n">X</span><span class="w"> </span><span class="o"><-</span><span class="w"> </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="w"> </span><span class="m">1000</span><span class="p">,</span><span class="w"> </span><span class="m">2000</span><span class="p">)</span><span class="w">
</span><span class="n">beta</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">rep</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">2000</span><span class="p">)</span><span class="w">
</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="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">runif</span><span class="p">(</span><span class="m">20</span><span class="p">)</span><span class="w">
</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="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">runif</span><span class="p">(</span><span class="m">80</span><span class="p">)</span><span class="w">
</span><span class="n">y</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="o">%*%</span><span class="w"> </span><span class="n">beta</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">rnorm</span><span class="p">(</span><span class="m">1000</span><span class="p">)</span><span class="w">
</span>
```

We assume that the predictors (i.e., the columns of ) 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 . That is, given the noisy observations of , we want to find out, which blocks of the vector contain non-zero entries. As you can see above, 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="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">grpSLOPE</span><span class="p">)</span><span class="w">
</span><span class="n">group</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">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="w"> </span><span class="n">each</span><span class="o">=</span><span class="m">10</span><span class="p">)</span><span class="w">
</span><span class="n">grpSLOPE.result</span><span class="w"> </span><span class="o"><-</span><span class="w"> </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="w"> </span><span class="n">y</span><span class="o">=</span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">group</span><span class="o">=</span><span class="n">group</span><span class="p">,</span><span class="w"> </span><span class="n">fdr</span><span class="o">=</span><span class="m">0.1</span><span class="p">)</span><span class="w">
</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="w">
</span><span class="c1"># [1] "1" "2" "191" "192" "193" "194" "195" "196" "197" "198"</span><span class="w">
</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="w"> </span><span class="o"><-</span><span class="w"> </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="w"> </span><span class="o">!=</span><span class="w"> </span><span class="m">0</span><span class="p">)])</span><span class="w">
</span><span class="n">truly.significant.groups</span><span class="w">
</span><span class="c1"># [1] 1 2 191 192 193 194 195 196 197 198</span><span class="w">
</span><span class="n">false.discoveries</span><span class="w"> </span><span class="o"><-</span><span class="w"> </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="w"> </span><span class="n">truly.significant.groups</span><span class="p">)</span><span class="w">
</span><span class="n">fdp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">false.discoveries</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nf">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="w">
</span><span class="n">fdp</span><span class="w">
</span><span class="c1"># [1] 0</span><span class="w">
</span><span class="n">true.discoveries</span><span class="w"> </span><span class="o"><-</span><span class="w"> </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="w"> </span><span class="n">truly.significant.groups</span><span class="p">)</span><span class="w">
</span><span class="n">power</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">true.discoveries</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nf">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="w">
</span><span class="n">power</span><span class="w">
</span><span class="c1"># [1] 1</span><span class="w">
</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).

**leave a comment**for the author, please follow the link and comment on their blog:

**Alexej's blog**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.