R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("kza-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('kza') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "kz" > > ### * kz > > flush(stderr()); flush(stdout()) > > ### Name: kz > ### Title: Kolmogorov-Zurbenko filter > ### Aliases: kz > ### Keywords: ts > > ### ** Examples > > #seperation of signals > yrs <- 20 > t <- seq(0,yrs,length=yrs*365) > y <- sin(2*pi*t) + sin(3*pi*t) > k.kz <- kz(y,365/4) > > par(mfrow=c(3,1)) > plot(y,type="l",main="y = sin(2*pi*t)+sin(3*pi*t)") > plot(k.kz,type="l",main="KZ filter") > > r <- y - 4*k.kz > plot(r,type="l",main="(y - 4*kz) ~ sin(3*pi*t)") > > #another example > #remove noise and high frequency > yrs <- 20 > t <- seq(0,yrs,length=yrs*365) > set.seed(6); e <- rnorm(n = length(t), sd = 1.0) > y <- sin(2*pi*t) + sin(3*pi*t) + e > > k.kz <- kz(y,365/4) > > par(mfrow=c(2,1)) > plot(y,type="l",main="y = sin(2*pi*t)+sin(3*pi*t) + noise") > plot(k.kz,type="l",main="KZ filter") > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "kza" > > ### * kza > > flush(stderr()); flush(stdout()) > > ### Name: kza > ### Title: Kolmogorov-Zurbenko Adaptive > ### Aliases: kza > ### Keywords: ts > > ### ** Examples > > # > # image detection (2d) > # > a <- matrix(rep(0,100*100),nrow=100) > a[35:70,35:70]<-1 > a <- a + matrix(rnorm(100*100,0,2),nrow=100) > k <- kza(a,q=15) [1] "Determine moving average with KZ." > > x <- seq(1,100) > y <- x > op <- par(bg = "white") > > #noise > persp(x, y, a, zlab="z", zlim=c(-4,6), ticktype="detailed", theta = 30, phi = 30, col = "lightblue") > > #kza filtered > persp(x, y, k, zlab="z", zlim=c(0,2), ticktype="detailed", theta = 30, phi = 30, col = "lightblue") > > par(mfrow=c(1,2)) > image(a,col=gray(seq(0,1,1/255))) > image(k,col=gray(seq(0,1,1/255))) > > # > #wedge example (3d) > # > m<-array(data=0, dim=c(50,50,50)) > for (i in 15:35) { + m[i:35,15:35,i] <- 1 + } > m<-m+rnorm(n = 50*50*50, sd = 5) > a<-kz(m,7,3) > b<-kza(m,kz=a,q=7,k=3) > > #movie of noisy 3d object > for(i in 1:50) { + image(matrix(m[,,i],50,50),col=gray(seq(0,min(max(m[,,i]),1),1/255))) + } > #movie of kza filtered > for(i in 1:50) { + image(matrix(b[,,i],50,50),col=gray(seq(0,min(max(b[,,i]),1),1/255))) + } > > # > # this is an example of detection of a break point in a time series > # seasonal data > yrs <- 20 > t <- seq(0,yrs,length=yrs*365) > q <- 365 > > #noise > e <- rnorm(n = length(t),0,1) > trend <- seq(0,-1,length=length(t)) > > #signal > bkpt <- 3452 > brk <- c(rep(0,bkpt),rep(0.5,length(t)-bkpt)) > signal <- trend + brk > > # y = seasonal + trend + break point + noise > y <- sin(2*pi*t) + signal + e > > k.kz <- kz(y,q) > > # kza reconstruction of the signal > k.kza <- kza(y,q,m=10) [1] "Determine moving average with KZ." > > par(mfrow=c(1,2)) > plot(y,type="l", ylim=c(-3,3)) > plot(signal,type="l",ylim=c(-3,3), + main="Signal and KZA Reconstruction") > lines(k.kza, col=4) > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "kzsv" > > ### * kzsv > > flush(stderr()); flush(stdout()) > > ### Name: kzsv > ### Title: Kolmogorov-Zurbenko Adaptive filter with Sample Variance. > ### Aliases: kzsv > ### Keywords: ts > > ### ** Examples > > x <- c(rep(0,1000),rep(0.5,1000),rep(0,1000)) > v <- x + rnorm(n = 3000, sd = 1.0) # normally-distributed random variates > z <- kza(v,q=200,m=10) [1] "Determine moving average with KZ." > par(mfrow=c(3,1)) > plot(v,type="l") > plot(z,type="l") > > s <- kzsv(y=z,kza=z,q=200) [1] "finding moving average with kz" > plot(s/mean(s),type="l") > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > ### *