1
votes

Rolling prediction in a data frame using dplyr and rollapply

Solution is great. However, how is it possible to show a coefficients and compute RMSE of the regression? I know that it is possible to compute coefficients without prediction, but I would like both in the same code.

Thanks in advance!

1
And what have you tried to do so far? - Miha
The method from the linked question. - Tony

1 Answers

1
votes

Using the data DF shown reproducibly in the answer to the linked-to question. We have shown literally the square root of the mean of the squared residuals as rmse but if what you wanted was the square root of the mean square residual from the anova table use sqrt(anova(fm)["Residuals", "Mean Sq"]) instead.

stats <- function(x) {
  fm <- lm(as.data.frame(x))
  c(pred = unname(tail(fitted(fm), 1)), coef(fm), rmse = sqrt(mean(resid(fm)^2)))
}
cbind(DF, rollapplyr(DF, 10, stats, by.column = FALSE, fill = NA))