Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It should be pretty quick if you go down to level of the math of the linear regression. If X is the independent variable and Y is the dependent variable. The coefficients are given by </p> <p><code>Beta = inv(t(X) %*% X) %*% (t(X) %*% Y)</code></p> <p>I'm a little confused about which variable you want to be the dependent and which one is the independent but hopefully solving a similar problem below will help you as well. </p> <p>In the example below I take 1000 variables instead of the original 5 and do not introduce any NA's. </p> <pre><code>require(xts) data &lt;- matrix(sample(1:10000, 1500000, replace=T), 1500, 1000, byrow = TRUE) # Random data data &lt;- xts(data, order.by = as.Date(1:1500, origin = "2000-01-01")) NR &lt;- nrow(data) # number of observations NC &lt;- ncol(data) # number of factors obs &lt;- 30 # required number of observations for rolling regression analysis </code></pre> <p>Now we can calculate the coefficients using Joshua's TTR package. </p> <pre><code>library(TTR) loop.begin.time &lt;- Sys.time() in.dep.var &lt;- data[,1] xx &lt;- TTR::runSum(in.dep.var*in.dep.var, obs) coeffs &lt;- do.call(cbind, lapply(data, function(z) { xy &lt;- TTR::runSum(z * in.dep.var, obs) xy/xx })) loop.end.time &lt;- Sys.time() print(loop.end.time - loop.begin.time) # prints the loop runtime </code></pre> <p>Time difference of 3.934461 secs</p> <pre><code>res.array = array(NA, dim=c(NC, NR, obs)) for(z in seq(obs)) { res.array[,,z] = coredata(data - lag.xts(coeffs, z-1) * as.numeric(in.dep.var)) } res.sd &lt;- apply(res.array, c(1,2), function(z) z / sd(z)) </code></pre> <p>If I haven't made any errors in the indexing <code>res.sd</code> should give you the standardized residuals. Please feel free to fix this solution to correct any bugs. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload