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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:28:33+00:00 2026-06-15T00:28:33+00:00

I am attempting to perform a rolling 100 day regression on an xts object

  • 0

I am attempting to perform a rolling 100 day regression on an xts object and return the t statistic of the slope coefficient for all dates. I have an xts object, prices:

> tail(prices)
             DBC   EEM   EFA    GLD   HYG    IEF   IWM   IYR    MDY    TLT
2012-11-02 27.14 41.60 53.69 162.60 92.41 107.62 81.19 64.50 179.99 122.26
2012-11-05 27.37 41.80 53.56 163.23 92.26 107.88 81.73 64.02 181.10 122.95
2012-11-06 27.86 42.13 54.07 166.30 92.40 107.39 82.34 64.16 182.69 121.79
2012-11-07 27.34 41.44 53.26 166.49 91.85 108.29 80.34 63.84 178.90 124.00
2012-11-08 27.38 40.92 52.78 167.99 91.55 108.77 79.21 63.19 176.37 125.84
2012-11-09 27.60 41.00 52.80 167.82 91.39 108.78 79.38 62.98 176.80 125.98

And a function to generate the t value from a regression of the last 100 prices and return as xts object with the same column names as prices:

compute.regression <- function(x) 
{
  last100 = last(x,100)
  fit <- lm (last100~time(last100))
  tval <- unlist(lapply( coef(summary(fit)), "[" ,"(Intercept)"  ,"t value"))
  tval.mat <-as.matrix(tval)
  tval.mat2 <- matrix(c(tval.mat), nrow=1, ncol=10)
  new2 <- xts(tval.mat2, Sys.Date())
  colnames(new2) <- tickers
  return(new2)
}

> compute.regression(prices)
                 IWM       EFA       HYG       EEM       IYR      IEF       TLT            DBC       GLD      MDY
2012-11-10 -7.642781 -14.33474 -16.28911 -14.52982 -15.85337 5.732489 -8.026495 -1.960392 -11.82474 8.686045

However, this function only returns the t values for the current date. I need a xts object that includes the t values for all dates included in the prices xts object. I am attempting to use rollapply but not getting anywhere:

fit.r <- function (x) 
{
      hard <- lm (x~time(x))
      return(hard)
}

compute.r <- function(x)
{  
  l100.ra <- rollapply(x, 100, fit.r, fill=NA, align='right')
  tval <- unlist(lapply( coef(summary(l100.ra)), "[" ,"(Intercept)"  ,"t value"))
  tval.mat <-as.matrix(tval)
  tval.mat2 <- matrix(c(tval.mat), nrow=1, ncol=10)
  new2 <- xts(tval.mat2, Sys.Date())
  colnames(new2) <- tickers
  return(new2)
}

But this returns the error:

compute.r(prices)
Error in zoo(rval, index(x)[i]) :
“x” : attempt to define invalid zoo object

I know the problem is in the l100.ra line but cannot fix it. Any thoughts?

EDIT 1

The function provided below:

x <- prices
f <- function (x) {
  res <- coef(summary(lm(x ~ time(x)))
  sapply(res, "[" ,"time(x)"  ,"t value")
}
r <- rollapplyr(x, 100, f, by.column=FALSE)

works well but changes the date/time format to that below:

> last(r,5)
           Response MDY Response TLT
1352160000     15.38380    -7.913764
1352246400     14.81888    -7.957261
1352332800     13.96203    -7.666699
1352419200     13.28624    -7.299532
1352678400     12.75266    -7.100558

I’ve been reading up on timestamps but don’t understand why the conversion took place. Also, when I try:

last(index(r),5)
[1] "2012-11-06 GMT" "2012-11-07 GMT" "2012-11-08 GMT" "2012-11-09 GMT" "2012-11-12 GMT"

It shows the timestamps in POSIXct format. Any clues here? I need to convert back to the format in prices above.

  • 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-15T00:28:34+00:00Added an answer on June 15, 2026 at 12:28 am

    Your rollapply call should call a function that returns the t-values. Right now it returns a lm object, which is not a valid value for the coredata of a zoo object.

    Make your function only return the t-values and it will work.

    library(quantmod)
    getSymbols("SPY;IEF")
    x <- merge(Cl(SPY),Cl(IEF))
    f <- function (x) {
      res <- coef(summary(lm(x ~ time(x))))
      sapply(res, "[" ,"(Intercept)"  ,"t value")
    }
    r <- rollapplyr(x, 100, f, by.column=FALSE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to perform integration testing on services that I have developed. Part
I'm attempting to perform some simple panning on a WPF Canvas object with its
I have been attempting to perform multiple (different) string replacement with recursion and I
I'm attempting to perform the most basic navigation with JQuery and I'm failing miserably.
I'm attempting to perform a DML operation against an Entity Framework model, specifically, an
I am attempting to perform a Storyboard segue from a UIbutton that is a
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files
I am attempting to perform String Interpolation in C#. The input string I am
I am attempting to perform some text canonicalization to replace some contractions. Here is

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.