[R] Re: how to remove time series trend in R?

Vito Ricci vito_ricci at yahoo.com
Thu Nov 25 15:03:24 CET 2004


Hi Terry,

If I understood your problem you would estimate trend
and seasonal (as sum of sin and cos) in a ts.

If t is time, Y is your ts, T=f(t) is trend function
of time (it could be linear, quadratic, etc. as better
is for your data), e=errors/residuals  

Your model to fit will'be:

Y(t)=T(t)+a*cos(2*pi*t/12)+b*sin(2*pi*t/12)+e(t) 

using lm() function to estimate a linear/polinomial
trend and sin/cos seasonal:

cos.t <- cos(2*pi*t/12)
sin.t <- sin(2*pi*t/12)
gfit<-lm(y~t+cos.t+sin.t, data=yourdf)

see this example:

> t<-seq(1:48)
> t
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
42 43 44 45 46 47 48

y<-10+5*t+0.5*cos(2*pi*t/12)+0.2*sin(2*pi*t/12)+rnorm(48)
> cos.t <- cos(2*pi*t/12)
> sin.t <- sin(2*pi*t/12)
> gfit<-lm(y~t+cos.t+sin.t)
> summary(gfit)

Call:
lm(formula = y ~ t + cos.t + sin.t)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.10222 -0.62184 -0.09387  0.50586  2.74299 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 10.30466    0.30009  34.339   <2e-16 ***
t            4.98987    0.01071 465.793   <2e-16 ***
cos.t        0.30207    0.20604   1.466    0.150    
sin.t        0.08699    0.20961   0.415    0.680    
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.'
0.1 ` ' 1 

Residual standard error: 1.008 on 44 degrees of
freedom
Multiple R-Squared: 0.9998,     Adjusted R-squared:
0.9998 
F-statistic: 7.525e+04 on 3 and 44 DF,  p-value: <
2.2e-16 

I hope I helped you.

Best
Vito
 



=====
Diventare costruttori di soluzioni
Became solutions' constructors

"The business of the statistician is to catalyze 
the scientific learning process."  
George E. P. Box


Visitate il portale http://www.modugno.it/
e in particolare la sezione su Palese  http://www.modugno.it/archivio/palese/




More information about the R-help mailing list