[R] Struggling with zoo and aggregate

Gabor Grothendieck ggrothendieck at gmail.com
Sun Mar 25 04:58:33 CEST 2012


On Sat, Mar 24, 2012 at 10:44 PM, Thomas Adams <thomas.adams at noaa.gov> wrote:
> All:
>
> I have a SQlite database where I have stored some verification data by date
> & time (cycle Z/UTC), lead_time as well as type, duration, etc. I would
> like to analyze & plot the data as monthly averages. I have looked at a
> bunch of examples which use some combination of zoo and aggregate, but I
> have not been able to successfully apply bits and pieces from the examples
> I have found. Any help is appreciated. BTW, I calculate mae (mean absolute
> error), mse (mean squared error), me (mean error), and other measures
> obtained by using the R verification package.
>
> The example below is limited to 20 records and shows lead_time, r_squared,
> (forecast) cycle, fcst_date (forecast date) -- the full data set is just
> over 2 years of daily data with 3 forecast cycles (00Z, 12Z, and 18Z) daily.
>
> >From my query, below) how do I construct an appropriate data structure to
> analyze & plot the data as monthly averages?
>
> Regards,
> Tom
>
>> q<-dbGetQuery(con,"select lead_time,r_squared,cycle,fcst_date from
> verify_table where duration=6 limit 20")
>> q
>   lead_time    r_squared cycle  fcst_date
> 1          6 5.405095e-02    00 07/31/2010
> 2         12 5.521620e-06    00 07/31/2010
> 3         18 1.565910e-04    00 07/31/2010
> 4         24 8.646822e-02    00 07/31/2010
> 5         30 1.719604e-02    00 07/31/2010
> 6         36 5.768113e-04    00 07/31/2010
> 7         42 2.501269e-06    00 07/31/2010
> 8         48 6.451727e-02    00 07/31/2010
> 9          6 2.857931e-01    12 07/31/2010
> 10        12 1.138635e-01    12 07/31/2010
> 11        18 2.225503e-02    12 07/31/2010
> 12        24 1.182031e-03    12 07/31/2010
> 13        30 8.841142e-04    12 07/31/2010
> 14        36 1.082490e-01    12 07/31/2010
> 15        42 1.502887e-05    12 07/31/2010
> 16        48           NA    12 07/31/2010
> 17         6 8.689588e-02    00 08/01/2010
> 18        12 5.884336e-04    00 08/01/2010
> 19        18 2.219316e-07    00 08/01/2010
> 20        24 3.960752e-02    00 08/01/2010
>

Try this:

Lines <- "lead_time    r_squared cycle  fcst_date
1          6 5.405095e-02    00 07/31/2010
2         12 5.521620e-06    00 07/31/2010
3         18 1.565910e-04    00 07/31/2010
4         24 8.646822e-02    00 07/31/2010
5         30 1.719604e-02    00 07/31/2010
6         36 5.768113e-04    00 07/31/2010
7         42 2.501269e-06    00 07/31/2010
8         48 6.451727e-02    00 07/31/2010
9          6 2.857931e-01    12 07/31/2010
10        12 1.138635e-01    12 07/31/2010
11        18 2.225503e-02    12 07/31/2010
12        24 1.182031e-03    12 07/31/2010
13        30 8.841142e-04    12 07/31/2010
14        36 1.082490e-01    12 07/31/2010"

library(zoo)
q <- read.table(text = Lines)

z <- read.zoo(q, index = 4, FUN = as.yearmon, format = "%m/%d/%Y",
aggregate = mean)
plot(z)

See the 5 vignettes that come with zoo as well as ?read.zoo, ?plot.zoo
 and ?xyplot.zoo


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list