Skip to content

Commit

Permalink
Merge pull request #1 from traitecoevo/skeleton
Browse files Browse the repository at this point in the history
- Added rstan package infrastructure
- Added 1st precompiled linear model template
- Added more details in README
- Updated DESCRIPTION and NAMESPACE
  • Loading branch information
fontikar authored Oct 1, 2023
2 parents a2e3101 + d3a758a commit 8cbe705
Show file tree
Hide file tree
Showing 22 changed files with 818 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^rmot\.Rproj$
^\.Rproj\.user$
^README\.Rmd$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ po/*~

# RStudio Connect folder
rsconnect/

29 changes: 29 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Package: rmot
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Biarch: true
Depends:
R (>= 3.4.0)
Imports:
methods,
Rcpp (>= 0.12.0),
RcppParallel (>= 5.0.1),
rstan (>= 2.18.1),
rstantools (>= 2.3.1.1)
LinkingTo:
BH (>= 1.66.0),
Rcpp (>= 0.12.0),
RcppEigen (>= 0.3.3.3.0),
RcppParallel (>= 5.0.1),
rstan (>= 2.18.1),
StanHeaders (>= 2.18.0)
SystemRequirements: GNU make
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(rmot_lm)
import(Rcpp)
import(methods)
importFrom(RcppParallel,RcppParallelLibs)
importFrom(rstan,sampling)
importFrom(rstantools,rstan_config)
useDynLib(rmot, .registration = TRUE)
18 changes: 18 additions & 0 deletions R/rmot-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' The 'rmot' package.
#'
#' @description A DESCRIPTION OF THE PACKAGE
#'
#' @docType package
#' @name rmot-package
#' @aliases rmot
#' @useDynLib rmot, .registration = TRUE
#' @import methods
#' @import Rcpp
#' @importFrom rstan sampling
#' @importFrom rstantools rstan_config
#' @importFrom RcppParallel RcppParallelLibs
#'
#' @references
#' Stan Development Team (NA). RStan: the R interface to Stan. R package version 2.26.23. https://mc-stan.org
#'
NULL
17 changes: 17 additions & 0 deletions R/rmot_lm.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#' Run a linear model in Stan
#'
#' @param x vector of numeric or integer predictor variable
#' @param y vector of numeric or integer response variable
#' @param ... additional arguments passed to rstan::sampling
#'
#' @return stanfit model output
#' @export
#'
#' @examples
#' mtcars
#' rmot_lm(mtcars$mpg, mtcars$disp)
rmot_lm <- function(x, y, ...) {
standata <- list(x = x, y = y, N = length(y))
out <- rstan::sampling(stanmodels$lm, data = standata, ...)
return(out)
}
25 changes: 25 additions & 0 deletions R/stanmodels.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by rstantools. Do not edit by hand.

# names of stan models
stanmodels <- c("lm")

# load each stan module
Rcpp::loadModule("stan_fit4lm_mod", what = TRUE)

# instantiate each stanmodel object
stanmodels <- sapply(stanmodels, function(model_name) {
# create C++ code for stan model
stan_file <- if(dir.exists("stan")) "stan" else file.path("inst", "stan")
stan_file <- file.path(stan_file, paste0(model_name, ".stan"))
stanfit <- rstan::stanc_builder(stan_file,
allow_undefined = TRUE,
obfuscate_model_name = FALSE)
stanfit$model_cpp <- list(model_cppname = stanfit$model_name,
model_cppcode = stanfit$cppcode)
# create stanmodel object
methods::new(Class = "stanmodel",
model_name = stanfit$model_name,
model_code = stanfit$model_code,
model_cpp = stanfit$model_cpp,
mk_cppmodule = function(x) get(paste0("rstantools_model_", model_name)))
})
37 changes: 37 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
eval = FALSE,
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# rmot

<!-- badges: start -->
<!-- badges: end -->

The goal of 'rmot' is to ...

## Installation

You can install the development version of 'rmot' from [GitHub](https://github.com/) with:

``` r
# install.packages("remotes")
remotes::install_github("traitecoevo/rmot")
```

## Demo

```{r example}
```
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# rmot
<!-- README.md is generated from README.Rmd. Please edit that file -->

# rmot

<!-- badges: start -->
<!-- badges: end -->

The goal of ‘rmot’ is to …

## Installation

You can install the development version of ‘rmot’ from
[GitHub](https://github.com/) with:

``` r
# install.packages("remotes")
remotes::install_github("traitecoevo/rmot")
```

## Demo
10 changes: 10 additions & 0 deletions Read-and-delete-me
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Stan-specific notes:

* All '.stan' files containing stanmodel definitions must be placed in 'inst/stan'.
* Additional files to be included by stanmodel definition files
(via e.g., #include "mylib.stan") must be placed in any subfolder of 'inst/stan'.
* Additional C++ files needed by any '.stan' file must be placed in 'inst/include',
and can only interact with the Stan C++ library via '#include' directives
placed in the file 'inst/include/stan_meta_header.hpp'.
* The precompiled stanmodel objects will appear in a named list called 'stanmodels',
and you can call them with e.g., 'rstan::sampling(stanmodels$foo, ...)'
5 changes: 5 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/sh

# Generated by rstantools. Do not edit by hand.

"${R_HOME}/bin/Rscript" -e "rstantools::rstan_config()"
5 changes: 5 additions & 0 deletions configure.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/sh

# Generated by rstantools. Do not edit by hand.

"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "rstantools::rstan_config()"
1 change: 1 addition & 0 deletions inst/include/stan_meta_header.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Insert all #include<foo.hpp> statements here
14 changes: 14 additions & 0 deletions inst/stan/include/license.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rmot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
rmot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with rmot. If not, see <http://www.gnu.org/licenses/>.
*/
16 changes: 16 additions & 0 deletions inst/stan/lm.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// A linear model
data {
int<lower=1> N;
vector[N] x;
vector[N] y;
}
parameters {
real intercept;
real beta;
real<lower=0> sigma;
}
model {
// ... priors, etc.

y ~ normal(intercept + beta * x, sigma);
}
13 changes: 13 additions & 0 deletions man/rmot-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/rmot_lm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions rmot.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
10 changes: 10 additions & 0 deletions src/Makevars
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by rstantools. Do not edit by hand.

STANHEADERS_SRC = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "message()" -e "cat(system.file('include', 'src', package = 'StanHeaders', mustWork = TRUE))" -e "message()" | grep "StanHeaders")

STANC_FLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "cat(ifelse(utils::packageVersion('rstan') >= '2.26', '-DUSE_STANC3',''))")
PKG_CPPFLAGS = -I"../inst/include" -I"$(STANHEADERS_SRC)" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error $(STANC_FLAGS) -D_HAS_AUTO_PTR_ETC=0
PKG_CXXFLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::CxxFlags()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::CxxFlags()")
PKG_LIBS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::RcppParallelLibs()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::LdFlags()")

CXX_STD = CXX17
25 changes: 25 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include <RcppEigen.h>
#include <Rcpp.h>

using namespace Rcpp;

#ifdef RCPP_USE_GLOBAL_ROSTREAM
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif


RcppExport SEXP _rcpp_module_boot_stan_fit4lm_mod();

static const R_CallMethodDef CallEntries[] = {
{"_rcpp_module_boot_stan_fit4lm_mod", (DL_FUNC) &_rcpp_module_boot_stan_fit4lm_mod, 0},
{NULL, NULL, 0}
};

RcppExport void R_init_rmot(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
32 changes: 32 additions & 0 deletions src/stanExports_lm.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Generated by rstantools. Do not edit by hand.

#include <Rcpp.h>
using namespace Rcpp ;
#include "stanExports_lm.h"

RCPP_MODULE(stan_fit4lm_mod) {


class_<rstan::stan_fit<stan_model, boost::random::ecuyer1988> >("rstantools_model_lm")

.constructor<SEXP,SEXP,SEXP>()


.method("call_sampler", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::call_sampler)
.method("param_names", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::param_names)
.method("param_names_oi", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::param_names_oi)
.method("param_fnames_oi", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::param_fnames_oi)
.method("param_dims", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::param_dims)
.method("param_dims_oi", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::param_dims_oi)
.method("update_param_oi", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::update_param_oi)
.method("param_oi_tidx", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::param_oi_tidx)
.method("grad_log_prob", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::grad_log_prob)
.method("log_prob", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::log_prob)
.method("unconstrain_pars", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::unconstrain_pars)
.method("constrain_pars", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::constrain_pars)
.method("num_pars_unconstrained", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::num_pars_unconstrained)
.method("unconstrained_param_names", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::unconstrained_param_names)
.method("constrained_param_names", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::constrained_param_names)
.method("standalone_gqs", &rstan::stan_fit<stan_model, boost::random::ecuyer1988> ::standalone_gqs)
;
}
Loading

0 comments on commit 8cbe705

Please sign in to comment.