Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I hope I got you correctly, but here is a small edit of your function:</p> <pre><code>rolling.regression &lt;- function(series) { mod &lt;- dynlm(formula = y ~ L(y) + L(x), data = as.zoo(series)) # get model nextOb &lt;- max(series[,'int'])+1 # To get the first row that follows the window if (nextOb&lt;=nrow(zoodata)) { # You won't predict the last one # 1) Make Predictions predicted=predict(mod,newdata=data.frame(x=zoodata[nextOb,'x'],y=zoodata[nextOb,'y'])) attributes(predicted)&lt;-NULL #Solution 1; Quicker to write # c(predicted=predicted, # square.res=(predicted-zoodata[nextOb,'y'])^2, # summary(mod)$coef[, 1], # summary(mod)$coef[, 3], # AdjR = summary(mod)$adj.r.squared) #Solution 2; Get column names right c(predicted=predicted, square.res=(predicted-zoodata[nextOb,'y'])^2, coef_intercept = summary(mod)$coef[1, 1], coef_Ly = summary(mod)$coef[2, 1], coef_Lx = summary(mod)$coef[3, 1], tValue_intercept = summary(mod)$coef[1, 3], tValue_Ly = summary(mod)$coef[2, 3], tValue_Lx = summary(mod)$coef[3, 3], AdjR = summary(mod)$adj.r.squared) } } rolling.window &lt;- 20 results.z &lt;- rollapply(zoodata, width=rolling.window, FUN=rolling.regression, by.column=F, align='right') head(results.z) predicted square.res coef_intercept coef_Ly coef_Lx tValue_intercept tValue_Ly tValue_Lx AdjR 20 10.849344 0.721452 0.26596465 0.5798046 1.049594 0.38309211 7.977627 13.59831 0.9140886 21 12.978791 2.713053 0.26262820 0.5796883 1.039882 0.37741499 7.993014 13.80632 0.9190757 22 9.814676 11.719999 0.08050796 0.5964808 1.073941 0.12523824 8.888657 15.01353 0.9340732 23 5.616781 15.013297 0.05084124 0.5984748 1.077133 0.08964998 9.881614 16.48967 0.9509550 24 3.763645 6.976454 0.26466039 0.5788949 1.068493 0.51810115 11.558724 17.22875 0.9542983 25 9.433157 31.772658 0.38577698 0.5812665 1.034862 0.70969330 10.728395 16.88175 0.9511061 </code></pre> <p>To see how it works, make a small example with a regression:</p> <pre><code>x &lt;- rnorm(1000); y &lt;- 2*x + rnorm(1000) reg &lt;- lm(y ~ x) summary(reg)$coef Estimate Std. Error t value Pr(&gt;|t|) (Intercept) 0.02694322 0.03035502 0.8876033 0.374968 x 1.97572544 0.03177346 62.1816310 0.000000 </code></pre> <p>As you can see, calling <code>summary</code> first and then getting the coefficients of it (<code>coef(summary(reg))</code> works as well) gives you a table with estimates, standard errors, and t-values. So estimates are saved in column 1 of that table, t-values in column 3. And that's how I obtain them in the updated <code>rolling.regression</code> function.</p> <p><strong>EDIT</strong></p> <p>I updated my solution; now it also contains the adjusted R2. If you just want the normal R2, get rid of the <code>.adj</code>.</p> <p><strong>EDIT 2</strong></p> <p>Quick and dirty hack how to name the columns:</p> <pre><code>rolling.regression &lt;- function(series) { mod &lt;- dynlm(formula = y ~ L(y) + L(x), data = as.zoo(series)) # get model nextOb &lt;- max(series[,'int'])+1 # To get the first row that follows the window if (nextOb&lt;=nrow(zoodata)) { # You won't predict the last one # 1) Make Predictions predicted=predict(mod,newdata=data.frame(x=zoodata[nextOb,'x'],y=zoodata[nextOb,'y'])) attributes(predicted)&lt;-NULL #Get variable names strVar &lt;- c("Intercept", paste0("L", 1:(nrow(summary(mod)$coef)-1))) vec &lt;- c(predicted=predicted, square.res=(predicted-zoodata[nextOb,'y'])^2, AdjR = summary(mod)$adj.r.squared, summary(mod)$coef[, 1], summary(mod)$coef[, 3]) names(vec)[4:length(vec)] &lt;- c(paste0("Coef_", strVar), paste0("tValue_", strVar)) vec } } </code></pre>
    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. 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