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 6828419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:22:50+00:00 2026-05-26T22:22:50+00:00

I’m looking to speed up the following algorithm. I give the function an xts

  • 0

I’m looking to speed up the following algorithm. I give the function an xts time series and then want to perform a principal components analysis for each time point on the previous X points (I’m using 500 at the moment) and then use the results of that PCA (5 principal components in the following code) to compute some value. Something like this:

lookback <- 500
for(i in (lookback+1):nrow(x))
{   
        x.now <- x[(i-lookback):i]        
        x.prcomp <- prcomp(x.now)
        ans[i] <- (some R code on x.prcomp)
}

I assume this would require me to replicate the lookback rows as columns so that x would be something like cbind(x,lag(x),lag(x,k=2),lag(x,k=3)...lag(x,k=lookback)), and then run prcomp on each line? This seems expensive though. Perhaps some variant of apply? I’m willing to look into Rcpp but wanted to run this by you guys before that.

Edit: Wow thanks for all the responses. Info on my dataset/algorithm:

  1. dim(x.xts) currently = 2000×24. But eventually, if this shows promise, it will have to run fast (I’ll give it multiple datasets).
  2. func(x.xts) takes ~70 seconds. That’s 2000-500 prcomp calls with 1500 500×24 dataframe creations.

I attempted to use Rprof to see what was the most expensive part of the algo but it’s my first time using Rprof so I need some more experience with this tool to get intelligible results (thanks for the suggestion).

I think I will first attempt to roll this into an _apply type loop, and then look at parallelizing.

  • 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-05-26T22:22:51+00:00Added an answer on May 26, 2026 at 10:22 pm

    On my 4 core desktop, if this wouldn’t complete in a reasonable time-frame, I would run the chunk using something along the lines of (not tested):

    library(snowfall)
    sfInit(parallel = TRUE, cpus = 4, type = "SOCK")
    lookback <- 500
    sfExport(list = c("lookback", "x"))
    sfLibrary(xts)
    
    output.object <- sfSapply(x = (lookback+1):nrow(x),
        fun = function(i, my.object = x, lb = lookback) {
            x.now <- my.object[(i-lb):i]      
            x.prcomp <- prcomp(x.now)
            ans <- ("some R code on x.prcomp")
    
            return(ans)
        }, simplify = FALSE) # or maybe it's TRUE? depends on what ans is
    
    • 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 want to construct a data frame in an Rcpp function, but when I
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the

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.