On Wed, Aug 11, 2010 at 12:22 PM, steven mosher <mosherste...@gmail.com> wrote: > Given a long zoo matrix, the goal is to "sweep" out a statistic from the > entire length of the > sequences. > > longzoomatrix<-zoo(matrix(rnorm(720),ncol=6),as.yearmon(outer(1900,seq(0,length=120)/12,"+"))) > cnames<-c(12345,23456,34567,45678,56789,67890) > colnames(longzoomatrix)<-cnames > longzoomatrix[1:24,] > 12345 23456 34567 45678 56789 > 67890 > Jan 1900 -0.17123165 1.02087086 0.79514870 -0.54519494 -0.13025459 > -0.009980402 > Feb 1900 1.21729926 -0.74541038 -0.08138406 -2.01180775 0.19256998 > 0.551965871 > Mar 1900 1.13222481 -1.25315703 0.01013473 0.08366155 -0.84246010 > -1.405959298 > Apr 1900 -0.02352559 -1.25001473 -1.53570550 -0.17945324 0.33368133 > 2.045125104 > May 1900 2.08204920 1.28091067 -0.80888146 0.31796730 0.83248551 > 1.439049603 > Jun 1900 0.62209570 -0.66189249 -0.57923119 -0.04346112 -2.71353384 > -0.346826902 > Jul 1900 -1.39758918 -0.54525469 -0.05230070 -0.36725079 1.28281798 > 1.391174712 > Aug 1900 0.12594069 0.09303970 0.69916411 -1.01902352 -0.82720898 > -0.208113626 > Sep 1900 -0.34310543 0.41718435 0.79455765 1.13234707 0.14652667 > -0.551426097 > Oct 1900 1.70634123 -1.20073104 -1.08771551 -0.01715296 0.24931996 > -0.753481196 > Nov 1900 0.15224070 -0.05108370 -0.97410069 0.51130170 0.13880814 > -2.160811186 > Dec 1900 0.34726817 0.61830719 0.84429979 -0.26253635 0.95243068 > -0.533562966 > Jan 1901 0.28647563 -0.40650198 -1.19640622 0.70267162 0.18867804 > 0.098855045 > Feb 1901 1.27269836 0.31797472 -1.13038040 1.33654480 0.08885501 > -0.134690872 > Mar 1901 -1.36934330 -0.17244539 0.81705554 -0.09113888 0.90241413 > 0.473939164 > Apr 1901 -0.89768498 0.82497595 0.15684387 2.25294476 -1.72886103 > -0.104769411 > May 1901 -0.27898445 -1.24348285 1.36203180 0.02422083 -1.33745980 > 1.098856752 > Jun 1901 -0.67968801 0.42082064 0.47056133 -0.12981223 0.19445803 > -0.284638114 > Jul 1901 0.03791761 -0.22118130 1.96044737 -1.18280989 0.90075205 > 0.055720535 > Aug 1901 1.12904079 0.57177055 0.64300572 -0.16284983 0.07951656 > -0.159396821 > Sep 1901 -1.43513934 0.03036697 1.09039400 0.99201776 0.98744827 > -0.057234838 > Oct 1901 0.73828382 0.53967835 2.16608282 -0.82929778 -1.99666687 > 0.352778450 > Nov 1901 0.06561583 -1.20126258 0.67427027 0.15493106 0.08867697 > 1.223073528 > Dec 1901 -1.23347027 -1.09699304 0.59398031 -0.22269292 -0.21569543 > 1.389667825 > > The statistic to be swept out is itself a zoo series with matching column > names. > There are twelve valies for each column representing an monthly average for > that > series. > > The average is to be subtracted > > sweepzoo<-zoo(matrix(rnorm(72),ncol=6), frequency=12) > colnames(sweepzoo)<-cnames > sweepzoo > 12345 23456 34567 45678 56789 67890 > 1(1) -2.5569706 -0.4375741 -0.1803866 -0.6303760 -0.08995198 2.7293244 > 2(1) 1.4154202 0.2559212 0.2104513 0.7439446 0.84897905 -0.4144865 > 3(1) -1.3709275 1.0472759 1.5975148 0.3190503 1.10430959 -1.8285194 > 4(1) -1.1436430 2.2071763 -0.2637954 -0.4915366 -0.03925020 1.3311624 > 5(1) -0.8003656 1.6421541 -1.4603128 0.4493069 0.28194066 -0.4728086 > 6(1) 0.9236015 0.3780122 -1.3848196 0.4263684 0.99584590 -1.4536475 > 7(1) 0.8810281 0.0381152 0.3810457 -0.6884233 -0.11018089 0.4221188 > 8(1) 0.3819421 -0.8431364 1.9876901 0.7072257 0.45524929 2.7013515 > 9(1) -1.1247988 1.3083178 -0.3438442 0.3300832 0.67013503 1.2912443 > 10(1) -0.3643043 1.0756782 -1.2026318 0.4477054 0.54486700 -0.3369889 > 11(1) 0.8294049 1.8170357 0.5691249 1.9213791 -0.29295754 -0.2617228 > 12(1) -1.0085265 -0.7556545 -1.4033321 -0.4646647 -0.14984913 -0.4848657 > > A brute force way to do this is to repeat the 12 values for each column so > that > the number of rows in the "sweepzoo" is equal to the nmber of rows on the > long zoo, object and then just subtract them. longzoomatrix-sweepzoo > > As a function sweep() wont work because it expects a vector whose > dimensions > matches the dimension of the MARGIN. > > Is there a elegant way to do this short of creating a "sweep zoo" that > matches > the row dimension of longzoo? ( would be a nice addition to sweep) >
If the question is how to demean each column then try this: > z <- zoo(cbind(a = 1:3, b = 4:6)) > scale(z, scale = FALSE) a b 1 -1 -1 2 0 0 3 1 1 attr(,"scaled:center") a b 2 5 > # or > sweep(z, 2, colMeans(z)) a b 1 -1 -1 2 0 0 3 1 1 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.