Note that there are some explanatory texts on larger screens.

plurals
  1. PORolling regression return multiple objects
    primarykey
    data
    text
    <p>I am trying to build a rolling regression function based on the example <a href="https://stackoverflow.com/questions/11583177/dynamic-time-series-prediction-and-rollapply">here</a>, but in addition to returning the predicted values, I would like to return the some rolling model diagnostics (i.e. coefficients, t-values, and mabye R^2). I would like the results to be returned in discrete objects based on the type of results. The example provided in the link above sucessfully creates thr rolling predictions, but I need some assistance packaging and writing out the rolling model diagnostics: </p> <p>In the end, I would like the function to return three (3) objects: </p> <ol> <li>Predictions </li> <li>Coefficients</li> <li>T values</li> <li>R^2</li> </ol> <p>Below is the code: </p> <pre><code>require(zoo) require(dynlm) ## Create Some Dummy Data set.seed(12345) x &lt;- rnorm(mean=3,sd=2,100) y &lt;- rep(NA,100) y[1] &lt;- x[1] for(i in 2:100) y[i]=1+x[i-1]+0.5*y[i-1]+rnorm(1,0,0.5) int &lt;- 1:100 dummydata &lt;- data.frame(int=int,x=x,y=y) zoodata &lt;- as.zoo(dummydata) 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 &lt;- predict(mod,newdata=data.frame(x=zoodata[nextOb,'x'],y=zoodata[nextOb,'y'])) attributes(predicted) &lt;- NULL c(predicted=predicted,square.res &lt;-(predicted-zoodata[nextOb,'y'])^2) # 2) Extract coefficients #coefficients &lt;- coef(mod) # 3) Extract rolling coefficient t values #tvalues &lt;- ????(mod) # 4) Extract rolling R^2 #rsq &lt;- } } rolling.window &lt;- 20 results.z &lt;- rollapply(zoodata, width=rolling.window, FUN=rolling.regression, by.column=F, align='right') </code></pre> <p>So after figuring out how to extract t values from model (i.e. mod) , what do I need to do to make the function return three (3) seperate objects (i.e. Predictions, Coefficients, and T-values)?</p> <p>I am fairly new to R, really new to functions, and extreemly new to zoo, and I'm stuck. </p> <p>Any assistance would be greatly appreciated.</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.
 

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