Giter Site home page Giter Site logo

szcf-weiya / frlr Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 2.49 MB

An R package: Fitting Repeated Linear Regressions

Home Page: https://cran.r-project.org/package=fRLR

License: GNU General Public License v3.0

R 14.86% C++ 80.55% Shell 0.40% M4 4.19%
regression gsl r

frlr's Introduction

fRLR

Introduction

This R package aims to fit Repeated Linear Regressions in which there are some same terms.

An Example

Suppose we want to fit a set of regressions which only differ in one variable. Specifically, denote the response variable as , and these regressions are as follows.

where are the same variables among these regressions.

Intuitively, we can finish this task by using a simple loop in R code.

model = vector(mode='list', length=n)
for (i in 1:n)
{
  ...
  model[[i]] = lm(y~x)
}

However, it is not efficient in that situation. Due to the same variables, there are some same calculations among different regressions. In order to reduce the cost of computation, I write this fRLR package which avoiding to repeat the same calculations. See more details in my blog, where I present the whole mathematical formula.

Let me take a simulation example to show how to use this R package.

## use fRLR package
library(fRLR)
set.seed(123)
X = matrix(rnorm(50), 10, 5)
Y = rnorm(10)
COV = matrix(rnorm(40), 10, 4)
frlr1(X, Y, COV)

## use simple loop
res = matrix(nrow = 0, ncol = 2)
for (i in 1:ncol(X))
{
  mat = cbind(X[,i], COV)
  df = as.data.frame(mat)
  model = lm(Y~., data = df)
  tmp = c(i, summary(model)$coefficients[2, 4])
  res = rbind(res, tmp)
}

Then we can obtain the following results

To show the speed of my package, let me increase the number of regressions, and compare the time duration with simple-loop method.

library(fRLR)
set.seed(123)
n = 500
X = matrix(rnorm(10*n), 10, n)
Y = rnorm(10)
COV = matrix(rnorm(40), 10, 4)

#idx1 = c(1, 2, 3, 4, 1, 1, 1, 2, 2, 3)
#idx2 = c(2, 3, 4, 5, 3, 4, 5, 4, 5, 5)
id = combn(n, 2)
idx1 = id[1, ]
idx2 = id[2, ]

system.time(frlr2(X, idx1, idx2, Y, COV))

simpleLoop <- function()
{
  res = matrix(nrow=0, ncol=4)
  for (i in 1:length(idx1))
  {
    mat = cbind(X[, idx1[i]], X[,idx2[i]], COV)
    df = as.data.frame(mat)
    model = lm(Y~., data = df)
    tmp = c(idx1[i], idx2[i], summary(model)$coefficients[2,4], summary(model)$coefficients[3,4])
    res = rbind(res, tmp)
  }
}

system.time(simpleLoop())

The results are as follows.

As you can see, fRLR can speed this task significantly.

Install Instructions

You'd better install this package on Linux or Mac, because they perfectly support the GNU scientific library (GSL). Of course, you can still this package on windows if you setup your GSL environment, and I opened a repository GSL where you can find solutions to install GSL on windows, although it might be cumbersome.

If GSL has been installed on your computer, you can install the package by the following command

devtools::install_github('szcf-weiya/fRLR')

frlr's People

Contributors

szcf-weiya avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

frlr's Issues

frlr.cpp:268:5: error: 'omp_set_num_threads' was not declared in this scope

* checking whether package 'fRLR' can be installed ... ERROR
Installation failed.
See 'd:/RCompile/CRANincoming/R-devel/fRLR.Rcheck/00install.out' for details.

* installing *source* package 'fRLR' ...
** using staged installation

   **********************************************
   WARNING: this package has a configure script
         It probably needs manual configuration
   **********************************************


** libs
using C++ compiler: 'g++.exe (GCC) 12.2.0'
g++ -std=gnu++17  -I"D:/RCompile/recent/R/include" -DNDEBUG -I/include -I'D:/RCompile/CRANpkg/lib/4.4/Rcpp/include'   -I"d:/rtools43/x86_64-w64-mingw32.static.posix/include"     -pedantic -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c RcppExports.cpp -o RcppExports.o
g++ -std=gnu++17  -I"D:/RCompile/recent/R/include" -DNDEBUG -I/include -I'D:/RCompile/CRANpkg/lib/4.4/Rcpp/include'   -I"d:/rtools43/x86_64-w64-mingw32.static.posix/include"     -pedantic -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c frlr.cpp -o frlr.o
frlr.cpp:144: warning: ignoring '#pragma omp parallel' [-Wunknown-pragmas]
  144 |   # pragma omp parallel for schedule(dynamic) // faster!!!
      | 
frlr.cpp:331: warning: ignoring '#pragma omp parallel' [-Wunknown-pragmas]
  331 |   # pragma omp parallel for schedule(dynamic) // faster!!!
      | 
frlr.cpp: In function 'Rcpp::List frlr1(SEXP, SEXP, SEXP, int)':
frlr.cpp:91:5: error: 'omp_set_num_threads' was not declared in this scope
   91 |     omp_set_num_threads(num_threads);
      |     ^~~~~~~~~~~~~~~~~~~
frlr.cpp: In function 'Rcpp::List frlr2(SEXP, SEXP, SEXP, SEXP, SEXP, int)':
frlr.cpp:268:5: error: 'omp_set_num_threads' was not declared in this scope
  268 |     omp_set_num_threads(num_threads);
      |     ^~~~~~~~~~~~~~~~~~~
make: *** [D:/RCompile/recent/R/etc/x64/Makeconf:289: frlr.o] Error 1
ERROR: compilation failed for package 'fRLR'
* removing 'd:/RCompile/CRANincoming/R-devel/lib/fRLR'
* restoring previous 'd:/RCompile/CRANincoming/R-devel/lib/fRLR'

warning during check: A complete check needs the 'checkbashisms' script.

https://stackoverflow.com/questions/63698128/r-package-check-warning-a-complete-check-needs-the-checkbashisms-script

Some checks can be done by the checkbashisms Perl script at https://sourceforge.net/projects/checkbaskisms/files, also available in most Linux distributions in a package named either ‘devscripts’ or ‘devscripts-checkbashisms’:
https://cran.r-project.org/doc/manuals/R-exts.html#Configure-and-cleanup

sudo apt install checkbashisms

warning: stack imbalance

email from CRAN

On 16/07/2021 09:28, Prof Brian Ripley wrote:

Dear maintainer,

Please see the problems shown on
https://cran.r-project.org/web/checks/check_results_fRLR.html.

Please correct before 2021-07-30 to safely retain your package on CRAN.

The CRAN Team

This started with Rcpp's update to 1.0.7, and seems to be (on most platforms) corrupting R's memory allocations which has made it impossible so far to get a handle on it.

--
Brian D. Ripley, [email protected]
Emeritus Professor of Applied Statistics, University of Oxford

and the checking details are https://cran.r-project.org/web/checks/check_results_fRLR.html

checking re-building of vignette outputs ... [30m/35m] WARNING
Error(s) in re-building vignettes:
--- re-building ‘fRLR.Rnw’ using Sweave

error in r-patched-solaris-x86

https://www.r-project.org/nosvn/R.check/r-patched-solaris-x86/fRLR-00check.html

using R version 4.1.0 Patched (2021-07-20 r80649)
using platform: i386-pc-solaris2.10 (32-bit)
using session charset: UTF-8
using option ‘--no-stop-on-test-error’
checking for file ‘fRLR/DESCRIPTION’ ... OK
checking extension type ... Package
this is package ‘fRLR’ version ‘1.2’
package encoding: UTF-8
checking package namespace information ... OK
checking package dependencies ... OK
checking if this is a source package ... OK
checking if there is a namespace ... OK
checking for executable files ... OK
checking for hidden files and directories ... OK
checking for portable file names ... OK
checking for sufficient/correct file permissions ... OK
checking whether package ‘fRLR’ can be installed ... OK
checking installed package size ... OK
checking package directory ... OK
checking ‘build’ directory ... OK
checking DESCRIPTION meta-information ... OK
checking top-level files ... OK
checking for left-over files ... OK
checking index information ... OK
checking package subdirectories ... OK
checking R files for non-ASCII characters ... OK
checking R files for syntax errors ... OK
checking whether the package can be loaded ... ERROR
Loading this package had a fatal error status code 1
Loading log:
Error: package or namespace load failed for ‘fRLR’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/ripley/R/Lib32/fRLR/libs/fRLR.so':
  ld.so.1: R: fatal: relocation error: file /home/ripley/R/Lib32/fRLR/libs/fRLR.so: symbol GOMP_loop_dynamic_start: referenced symbol not found
Execution halted
DONE

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.