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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:02:57+00:00 2026-06-10T02:02:57+00:00

I need to run a bootstrap on a time series with non-standard dependence. So

  • 0

I need to run a bootstrap on a time series with non-standard dependence. So to do this I need to create a function that simulates the time series by making time by time adjustments.

testing<-function(){
  sampleData<-as.zoo(data.frame(index=1:1000,vol=(rnorm(1000))^2,x=NA))
  sampleData[,"x"]<-sampleData[,"vol"]+rnorm(1000) #treat this is completely exognenous and unknown in connection to vol
  sampleData<-cbind(sampleData,mean=rollmean(sampleData[,"vol"],k=3,align="right"))
  sampleData<-cbind(sampleData,vol1=lag(sampleData[,"vol"],k=-1),x1=lag(sampleData[,"x"],k=-1),mean1=lag(sampleData[,"mean"],k=-1))

  #get estimate
  mod<-lm(vol~vol1+x1+mean1,data=sampleData)

  res<-mod$residuals

  for(i in 5:1000){
    #recursively estimate
    sampleData[i,"vol"]<-as.numeric(predict(mod,newdata=data.frame(sampleData[i-1,])))+res[i-3]

    #now must update other paramaters
      #first our rolled average
      sampleData[i,"mean"]<-mean(sampleData[(i-3):i,"vol"])

      #reupdate our lagged variables
      sampleData[i,"vol1"]<-sampleData[i-1,"vol"]
      sampleData[i,"mean1"]<-sampleData[i-1,"mean"]

  }

  lm(vol~vol1+x1+mean1,data=sampleData)
}

When I run this code and measure the run time I get

system.time(testing())
user  system elapsed 
2.711   0.201   2.915 

This is a slight problem for me as a will be integrating this code to construct a bootstrap. This means any time taken here is multiplied by about 100 for each step. And I am updating this at a few thousand times. That means a single run will take hours (to days) to run.

Is there anyway to speed this code up?

Kind regards,

Matthew

  • 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-10T02:02:58+00:00Added an answer on June 10, 2026 at 2:02 am

    Here’s how to avoid the overhead of predict.lm. Also note that I used a matrix instead of a zoo object, which would be a tiny bit slower. You can see just how much this slowed down your code. That’s the price you pay for convenience.

    testing.jmu <- function() {
      if(!require(xts)) stop("xts package not installed")
      set.seed(21)  # for reproducibility
      sampleData <- .xts(data.frame(vol=(rnorm(1000))^2,x=NA), 1:1000)
      sampleData$x <- sampleData$vol+rnorm(1000)
      sampleData$mean <- rollmean(sampleData$vol, k=3, align="right")
      sampleData$vol1 <- lag(sampleData$vol,k=1)
      sampleData$x1 <- lag(sampleData$x,k=1)
      sampleData$mean1 <- lag(sampleData$mean,k=1)
    
      sampleMatrix <- na.omit(cbind(as.matrix(sampleData),constant=1))
      mod.fit <- lm.fit(sampleMatrix[,c("constant","vol1","x1","mean1")],
                        sampleMatrix[,"vol"])
      res.fit <- mod.fit$residuals
    
      for(i in 5:nrow(sampleMatrix)){
        sampleMatrix[i,"vol"] <-
          sum(sampleMatrix[i-1,c("constant","vol1","x1","mean1")] *
              mod.fit$coefficients)+res.fit[i-3]
        sampleMatrix[i,"mean"] <- mean(sampleMatrix[(i-3):i,"vol"])
        sampleMatrix[i,c("vol1","mean1")] <- sampleMatrix[i-1,c("vol","mean")]
      }
    
      lm.fit(sampleMatrix[,c("constant","vol1","x1","mean1")], sampleMatrix[,"vol"])
    }
    system.time(out <- testing.jmu())
    #    user  system elapsed 
    #    0.05    0.00    0.05 
    coef(out)
    #    constant        vol1          x1       mean1 
    #  1.08787779 -0.06487441  0.03416802 -0.02757601
    

    Add the set.seed(21) call to your function and you’ll see that my function returns the same coefficients as yours.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need run code that will create a database and populate tables. i am
This is my problem: I need run some code every time I open a
I need to run the application that I have made in Xcode on my
I need to run a script that will generate the projects.list file when a
I need to create a bootstrap for my WiX project I've tried using setupbld.exe
we need run one function periodically in Java web application . How to call
I need to insert dummy data on test environtment so that I can run
I've created Java annotations (since I need run time retention) under $PROJECT/src/main/java and my
I need run benchmark to test my cpu FLOPS and compare this with raitings
I'm need run a couple of validations that depend on the decisions of 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.