Skip to content

Instantly share code, notes, and snippets.

@slarge
Created April 20, 2016 11:44
Show Gist options
  • Save slarge/219a408177cc912c2569d9f5bc6c2f4f to your computer and use it in GitHub Desktop.
Save slarge/219a408177cc912c2569d9f5bc6c2f4f to your computer and use it in GitHub Desktop.
Rolling window linear model
tt <- data.frame(YEAR = seq(1981, 2010, by = 1),
INDICATOR = rnorm(n = 30))
library(zoo)
dolm <- function(x) {
SE <- summary(lm(INDICATOR ~ ., data = as.data.frame(x)))$sigma
SLOPE <- summary(lm(INDICATOR ~ ., data = as.data.frame(x)))$coefficients[1]
return(list(standard.error = SE, slope = SLOPE))
}
rollapplyr(tt, width = 5, dolm, by.column = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment