Gabor Grothendieck <ggrothendieck <at> myway.com> writes: > movingWindow <- function(formula, data, width, ...) { > nr <- nrow(data) > width <- as.integer(width)[1] > stopifnot( width > 0, width <= nr ) > indices <- as.data.frame( t( embed( 1:nr, width ) ) ) > lapply(indices, function(st) summary(lm(formula, data[st,])) ) > } >
Just one further simplification using apply instead of lapply to eliminate having to transform embed: movingWindow <- function(formula, data, width, ...) { nr <- nrow(data) width <- as.integer(width)[1] stopifnot( width > 0, width <= nr ) apply( embed(1:nr, width), 1, # rows are indices of successive windows function(st) summary(lm(formula, data[st,])) ) } This could also be used in movingWindow2, as well. ______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html