time series - R Moving Window Model fit with variable window size -


currently, working data frames in r, first column of numeric date. have data sorted in ascending order of date. want fit model (the code i've provided simple ols model) 20 day period i've had assume have 124 observations per day, requiring me use loop, not case. there way me include 20 day window without making assumption? current algorithm have below. appreciated. inputs data set , 2 integers, predict , predictor.

rollerols <- function(data, predict, predictor){   res <- list()   alpha <- c()   beta <- c()   m <- dim(data)[1]   for(i in 1:(floor(m/124)-10)){     data.new <- as.data.frame(data[c((1+(124*(i-1))):((i+9)*124)),])     data.pred <- as.data.frame(data[c((1+(124*(i+9))):((i+10)*124)-1),])     n <- dim(data.new)[1]     k <- dim(data.pred)[1]     x <- data.new[-1,predictor]     y <- data.new[-n, predict]     mod <- lm(y ~ x)     ts <- mod$coefficients[1] + mod$coefficients[2]*data.pred[-1,predictor]      actual <- data.pred[-k,predict]     alpha[i] <- mod$coefficients[1]     beta[i] <- mod$coefficients[2]   }   coef <- as.data.frame(cbind(alpha, beta))   res$coefs <- coef   res <- as.data.frame(res)   return(res) } 


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -