R/MeanSeasonalCycle.R

MeanSeasonalCycle <- structure(function(
	##title<< 
	## Calculate the mean seasonal cycle of a time series
	
	##description<<
	## The function calculates the mean seasonal cycle of a time series. 
	
	ts
	### univariate time series of class \code{\link{ts}}
		
	##seealso<<
	## \code{\link{Decompose}}, \code{\link{TrendSeasonalAdjusted}}
) {

	if (class(ts) != "ts") stop("ts should be of class ts.")
	time <- time(ts)
	years <- as.integer(time)
	nyears <- length(unique(years))
	
	# estimate sesonal cycle based on mean seasonal cycle
	St_est <- aggregate(as.vector(ts), list(as.vector(cycle(ts))), "mean", na.rm=TRUE)$x
	St_est <- ts(St_est[as.vector(cycle(ts))], start=start(ts), freq=frequency(ts))
	St_est <- St_est - mean(ts, na.rm=TRUE)
	return(St_est)
	### Mean seasonal cycle of time series ts with the same length as ts, i.e. the mean seasonal cycle is repeated for each year. The mean seasonal cycle is centered to 0.
}, ex=function() {
ndvi.cycle <- MeanSeasonalCycle(ndvi)
plot(ndvi.cycle)

# the mean seasonal cycle is centered to 0, 
# add the mean of the time series if you want to overlay it with the original data:
plot(ndvi)
lines(ndvi.cycle + mean(ndvi, na.rm=TRUE), col="blue")
})

Try the greenbrown package in your browser

Any scripts or data that you put into this service are public.

greenbrown documentation built on Dec. 18, 2020, 3:02 p.m.