[R] How to find out the trend during a time period?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Oct 28 13:56:16 CEST 2010


2010/10/28 Uwe Ligges <ligges at statistik.tu-dortmund.de>:
>
>
> On 28.10.2010 06:50, Lao Meng wrote:
>>
>> Hi all:
>> I have a dataset which contains two variables: Y and time
>> y<-c(228,189,232,198,252,315)
>> time<-2003:2008
>>
>> How can I find out the trend(increase/decrease) of y along the time
>> period?
>>
>> If I use:
>> lm(y~time)
>>
>> The "lm" command treats time as natural number,but not date.
>
> What is "date"? WHere is the reproducible example?

The code the poster displayed is reproducible -- the problem is more
that its not entirely clear what is desired.  If the worry is that
there can be different numbers of days in different years due to leap
years then one could use the number of days between January 1st of
each year and January 1st of the first year like this (where y and
time are from the original post):

> d <- as.Date(paste(time, 1, 1, sep = "-"))
> d <- as.numeric(d - d[1])
> d
[1]    0  365  731 1096 1461 1826
> lm(y ~ d)

Call:
lm(formula = y ~ d)

Coefficients:
(Intercept)            d
  193.52454      0.04615



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