[R] lm() on a matrix of zoo series

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 21 21:21:04 CET 2010


On Tue, Dec 21, 2010 at 3:02 PM, steven mosher <moshersteven at gmail.com> wrote:
> I have a matrix of zoo series. each series is in a column.
>  x <- as.yearmon(2000 + seq(0, 23)/12)
> # 24 months of data, lets make 20 sets of random data
>  testData <- matrix(rnorm(480),ncol=20)
> # make a zoo object and columns will hold the 20 series
> TestZoo  <- zoo(testData,order.by=x)
> # now run lm for just one series.
>  m <- lm(TestZoo[,1]~time(TestZoo))$coeff[2]
>  m
> time(TestZoo)
>    0.3443124
>  m2 <- lm(TestZoo[,2]~time(TestZoo))$coeff[2]
>  m2
> time(TestZoo)
>   -0.1192866
>
> I've been struggling trying to use apply ( or something equally suitable) to
> get a vector of "m" for this entire matrix
> without resorting to a loop.
>

Try this:

   lm(TestZoo ~ time(TestZoo))


-- 
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