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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:41:05+00:00 2026-06-13T18:41:05+00:00

I posted this question yesterday but received some valuable feedback that my post left

  • 0

I posted this question yesterday but received some valuable feedback that my post left a little to be desired :). Here is an updated attempt that hopefully is much clearer:

I have a xts zoo object and would like to determine the t-statistic of the slope coefficient over the last 20 periods using R and then test whether that t value is > 2.

class(prices)

[1] "xts" "zoo"

tail(prices)
             IWM    SPY    TLT
2012-10-24 81.20 141.02 121.48
2012-10-25 81.53 141.43 120.86
2012-10-26 81.14 141.35 122.64
2012-10-31 81.63 141.35 123.36
2012-11-01 82.49 142.83 122.35
2012-11-02 81.19 141.56 122.26

I could not figure out how to perform a regression for each column versus time so I created a new column (daynumber) with the index and performed the regression versus the index:

lastprices = last(prices,20)
prices.data.frame = as.data.frame(lastprices)
daynumber = index(prices.data.frame)
pdfd = data.frame(prices.data.frame, daynumber)
pdfd.lm = lm(pdfd$daynumber ~ ., data=pdfd)
tstat = coef(summary(pdfd.lm))
tstat[,"t value"]
(Intercept)         IWM         SPY         TLT 
  4.5426630  -0.1788975  -1.3521969  -2.2362345 
tstattest = ifelse(tstat[,"t value"]>2,1,0)
tstattest
(Intercept)         IWM         SPY         TLT 
          1           0           0           0 

I cant help but think this is not the most efficient way to accomplish this task. Does anyone have ideas on how to just perform the regression for each column versus time without creating the daynumber column?

Thanks – take it easy on me I just started learning!

  • 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-13T18:41:06+00:00Added an answer on June 13, 2026 at 6:41 pm

    I think your original problem had made useful progress. What I thought was an output format that came from a unnamed package was really the output of lm where there were multiple columns on hte LHS of the formula. What I have done with your second version is this:

    require(zoo)
    require(xts)
    prices <- read.zoo(text="dt           IWM    SPY    TLT
     2012-10-24 81.20 141.02 121.48
     2012-10-25 81.53 141.43 120.86
     2012-10-26 81.14 141.35 122.64
     2012-10-31 81.63 141.35 123.36
     2012-11-01 82.49 142.83 122.35
     2012-11-02 81.19 141.56 122.26", index=1, header=TRUE, FUN=as.Date, format="%Y-%m-%d")
     prices <- as.xts(prices)
    dprice <- as.data.frame(prices)
    dprice$dat <- 1:6
    
    lm( cbind(IWM,SPY , TLT) ~dat , data=dprice)
    #-------------
    Call:
    lm(formula = cbind(IWM, SPY, TLT) ~ dat, data = dprice)
    
    Coefficients:
                 IWM        SPY        TLT      
    (Intercept)   81.19800  140.90000  121.24933
    dat            0.09486    0.19714    0.25971
    

    Notice that there are no t-values in that output. the summary.lm function will create those:

    tstat = coef(summary(fit))
    tstat
    #--------------
    Response IWM :
                   Estimate Std. Error     t value     Pr(>|t|)
    (Intercept) 81.19800000  0.4993259 162.6152292 8.578215e-09
    dat          0.09485714  0.1282151   0.7398284 5.004726e-01
    
    Response SPY :
                   Estimate Std. Error    t value     Pr(>|t|)
    (Intercept) 140.9000000  0.5356109 263.064096 1.252746e-09
    dat           0.1971429  0.1375322   1.433431 2.250283e-01
    
    Response TLT :
                   Estimate Std. Error   t value     Pr(>|t|)
    (Intercept) 121.2493333  0.7632198 158.86556 9.417108e-09
    dat           0.2597143  0.1959767   1.32523 2.557232e-01
    

    To extract the t-values from that list of three matrices you can use lapply:

    lapply( coef(summary(fit)), "[" , ,"t value")
    #----------
    $`Response IWM`
    (Intercept)         dat 
    162.6152292   0.7398284 
    
    $`Response SPY`
    (Intercept)         dat 
     263.064096    1.433431 
    
    $`Response TLT`
    (Intercept)         dat 
      158.86556     1.3252
    

    To get just the intercepts you can use this:

     lapply( coef(summary(fit)), "[" , "(Intercept)","t value")
    #-----
    $`Response IWM`
    [1] 162.6152
    
    $`Response SPY`
    [1] 263.0641
    
    $`Response TLT`
    [1] 158.8656
    

    PLEASE note that the Intercept value has almost no meaning in this context. If you use the numbers 1:6 you get one intercept and if you use the Dates-classes value you get a completely different value. The t-test is on whether the intercept is zero, and since the scale is rather arbitrary it has no interpretation unless you are very sure of the coding and what a zero-value would imply.

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

Sidebar

Related Questions

I posted this yesterday on SO, and I received an answer that works great,
Yesterday, I posted a question on some tips doing this Remote Client-Server Application in
I've posted a question yesterday but I just realized that the answer doesn't seem
I posted this question yesterday and the answer I go mentioned that I should
Here is my JSFiddle . Yesterday I posted question that how to create hashtag
ok, yesterday I posted almost identical question here , but I wasn't able to
I posted a question up yesterday that asked for some help on an image
Similar to the question I posted yesterday , I have this problem that I
i posted a question like this yesterday but I was not clear in the
I posted this question at math.stackexchange.com but I am not entirely sure if that

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.