From ggrothendieck at gmail.com  Wed Apr 30 18:54:12 2008
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Wed, 30 Apr 2008 12:54:12 -0400
Subject: [Rd] apply and monthly time series (PR#11352)
In-Reply-To: <20080430130004.74F412834154@mail.pubhealth.ku.dk>
References: <20080430130004.74F412834154@mail.pubhealth.ku.dk>
Message-ID: <971536df0804300954l2a562f92h8e829381ff49c4f9@mail.gmail.com>

See ?apply

If X is not an array but has a dimension attribute, apply attempts to coerce
it to an array via as.matrix if it is two-dimensional (e.g., data
frames) or via as.array.

So for example, try this noting that time is 1, 2, 3, ...

apply(monthly, 2, time)

Try

lm(monthly ~ time(monthly))


On Wed, Apr 30, 2008 at 9:00 AM,  <stephen.mcintyre at utoronto.ca> wrote:
> Full_Name: Stephen McIntyre
> Version: 2.7
> OS: Windows XP
> Submission from: (NULL) (99.231.2.44)
>
>
> When I use the apply function to calculate a trend for a matrix of monthly time
> series, it yields a different answer than when the trend is calculated one at a
> time (by a factor of 12) rather than the identical answer as it should. Here's
> an example:
>
> download.file("http://www.climateaudit.org/data/models/monthly.tab","temp.dat",mode="wb")
> load("temp.dat")
> trend= function(x)  lm(x~c(time(x)))$coef[2]
> b= apply(monthly,2,trend)
> a= c(trend(monthly[,1]),trend(monthly[,2]),trend(monthly[,3]))
> a/b  #12 12 12
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] apply and monthly time series (PR#11352)
Gabor Grothendieck