Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8121307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:26:47+00:00 2026-06-06T05:26:47+00:00

I’m fitting least squares models with the same predictor and a large number of

  • 0

I’m fitting least squares models with the same predictor and a large number of responses, and all I need are the residuals. The qr.resid function is the simplest I’ve found to do this, but it takes more memory than necessary because the Fortran code inside of it returns some unnecessary things which are discarded before qr.resid returns the result.

This means that when the number of responses is big enough, my system has to start swapping memory and it takes a very long time to get to the answer. Doing it one by one (with a loop in R) is faster because it doesn’t swap, but is (presumably) slower than it would be to do the whole thing in a vectorized way.

That is, this version is faster when y is small enough not to cause swapping:

reslsfit1 <- function (x, y) {
  qr.resid(qr(x),y)
}

But this version is faster for large y because it doesn’t swap:

reslsfit2 <- function (x, y) {
  x <- unname(x)
  y <- unname(y)
  out <- matrix(NA, ncol=ncol(y), nrow=nrow(y))
  qrx <- qr(x)
  for(i in 1:ncol(y)) {out[,i] <- qr.resid(qrx, y[,i])}
  out
}

Any suggestions? I’d prefer to use an existing function (or at least, an existing algorithm) rather than rolling my own because of the potential for numerical issues.

Here’s code that recreates the issue; you’ll have to make N big enough for your system.

set.seed(5)
n <- 1000
N <- 10000 # make this big enough for your system to swap
y <- matrix(rnorm(n*N), ncol=N)
x <- rnorm(n)
r1 <- reslsfit1(x,y)
r2 <- reslsfit2(x,y)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:26:48+00:00Added an answer on June 6, 2026 at 5:26 am

    Interestingly, qr.fitted and qr.coef don’t seem to take as much memory, so either of these work for me without running out memory. The qr.coef version seems slightly faster.

    reslsfit3 <- function(x,y) { y - qr.fitted(qr(x), y) }
    reslsfit4 <- function(x,y) { y - A %*% qr.coef(qr(x), y) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
Thanks in advance for your help. I have a need within an application to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.