Giter VIP home page Giter VIP logo

rollregres's Introduction

rollRegres

R-CMD-check CRAN RStudio mirror downloads

This package contains functions for fast rolling and expanding linear regression models. You can install the package from CRAN by calling

install.packages("rollRegres")

or from Github by calling

devtools::install_github("boennecd/rollRegres", upgrade_dependencies = FALSE, 
                         build_vignettes = TRUE)

See vignette("Comparisons", package = "rollRegres") for examples of how to use the functions.

rollregres's People

Contributors

boennecd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

matixr allisterh

rollregres's Issues

Is it possible to extract the residuals from the rolling regression?

Hi, I am performing a rolling regression (i.e., I am using a moving window). I have two variables, the y (called ntl) and x (called tirs20). Here is the code:

block.data = read.csv ("path/block.data.psf.csv")

eq = ntl ~ tirs20

model = roll_regress(eq, block.data, width = 5, do_downdates = TRUE, do_compute = "r.squareds")

coeff = as.data.frame(model$coefs)

The function's output is the model's coefficients (intercept and slope). I was wondering if there is a way to add the residuals of the regression as a new column to the coeff data frame.

If necessary, I can share my csv.

Fatal error when the grp argument is used and the width=1L

The functionality of updating in blocks is very usefull in asset pricing, e.g., rolling regressions of stock returns on market returns. Sometimes we need to do this using the daily returns of last one month/year, and then an fatal error comes. Replication code (in the comparisions vignette):

library(rollRegres)
set.seed(96235555)
n <- 10L * 12L * 21L # x years w/ 12 months of 21 trading days
month <- (seq_len(n) - 1L) %/% 21L + 1L # group by months
set.seed(29478439)
X <- matrix(rnorm(n * 4L), ncol = 4L)
y <- rnorm(n)

randomly drop rows

keep <- seq_along(y) %in% sample.int(nrow(X), as.integer(n * .5))
X <- X [keep, ]
y <- y [keep]
month <- month[keep]

width=2 works well

pck_out <- roll_regres.fit(X, y, width = 2L, grp = month, min_obs = 10L * 2L, do_downdates = TRUE)

while width=1 incurs an fatal error

pck_out <- roll_regres.fit(X, y, width = 1L, grp = month, min_obs = 10L * 1L, do_downdates = TRUE)

Thank you!

weighted roll_regres

would you consider adding support for weights like lm does?
i believe that lm uses a weighted least squares. i.e. minimizing sum(w*e^2), when given an input vector of weights

Removed from CRAN

I noticed this package is removed from CRAN. I have used it a lot, is that possible to publish in CRAN again? Thanks.

Rolling Regression. Residual?

Hello. Thank you for your contribution.

I am using this package very usefully. I have some issues.

I want to calculate the most recent residual of a dataset, during operating rolling windows.

Is there any method? I know do.compute calculate one period ahead prediction error , R2 and

sigma. Is there any way to derive residuals?

rolling rmse

Is it possible to calculate the root mean squared residuals on a cumulative (do_downdates = F) rolling basis using this library? The only idea ive come up with so far is to compute full prior regression line for each data point, then compute the a rolling mean on the residuals, but that seems I have to re-create the cumulative rolling loops and mean function all over again

Crashes with skipped groups

R hard-crashes when I attempt to run the following code using the latest release of roll_regress. Note that groups 11 and 12 are skipped and the width is 12.

ehbSample <- structure(list(
    group_ = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 13L), 
    y = c(7, 0, -1, 1, 3, 13, 9, 4, 32, -15, 28, -23, -40, -15, 57, 7, -24, -4, 10, -6, 7, 10, 16, -2, 16, -10, 4, -6, 5, 1, -2, 2, 4, 10, -28, -11, 5, -8, 17, -9, -4),
    x = c(0, -6, 0, -1, -13, 3, 13, 9, -17, 32, -15, 28, -10, 12, -15, 57, -9, -24, -4, 10, -1, 7, 10, 16, -1, 16, -10, 4, 8, 8, 1, -2, -1, 4, 10, -28, 3, 5, -8, 17, 1)), row.names = c(NA, -41L), 
    groups = structure(list(
      group_ = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 13L), 
      .rows = structure(
        list(1:4, 5:8, 9:12, 13:16, 17:20, 21:24, 25:28, 29:32, 33:36, 37:40, 41L), ptype = integer(0), 
             class = c("vctrs_list_of", "vctrs_vctr", "list"))), 
      row.names = c(NA, -11L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), 
    class = c("grouped_df", "tbl_df", "tbl", "data.frame"))

fit <- roll_regres(
    y ~ x,
    ehbSample,
    width = 12,
    grp = ehbSample$group_,
    do_downdates = TRUE)

Width argument in the manual page

The manual pages states the following about the width argument:

Only used if do_downdates == True.

But it is the minimum number of observations in expanding window regressions.

crash with warning

The code crashes if it enters this if block

rollRegres/src/roll.cpp

Lines 242 to 245 in ddab0ed

if(!have_warned and sample_size < too_low){
have_warned = true;
Rcpp::warning("low sample size relative to number of parameters");
}

A simple example is

library(rollRegres)
x <- matrix(rnorm(4 * 20), ncol = 4)
y <- rnorm(20)
roll_regres.fit(x, y, width = 11L)

which crash while this does not

library(rollRegres)
x <- matrix(rnorm(4 * 20), ncol = 4)
y <- rnorm(20)
roll_regres.fit(x, y, width = 12L)

Maybe reconsider how low or high this should be

const double too_low = p * 3;

Compute standard errors

Add standard errors for the coefficients as a possible output. The Choleksy decompaction of the inner product of the covariance matrix is computed so this should be straight forward to implement.

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.