-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_descriptive_techniques.Rmd
1236 lines (810 loc) · 28.4 KB
/
simple_descriptive_techniques.Rmd
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Simple Exploration Techniques for Time Series Analysis"
author: "Amir R"
output:
ioslides_presentation:
incremental: true
widescreen: true
toc: true
toc_depth: 2
toc_float: true
df_print: paged
---
```{r Packages, eval=T, include=FALSE}
library(magrittr) # for the pipes
# install.packages("magrittr") if not installed
```
# Intro
---
What's a time series anyway ?
*"Is it recorded or produced with temporal information ?"*
---
frequently encountered in day to day life
* Marketing: Prices, Sales, economic indices
* Central Beauro of Statistics: Demographic information
* Physical: such as Weather history
frequently encountered in research
* Observation Counts: Pop. & community ecology
* Environmental
* recording instrument output
---
Data fit for classic time series analysis in base R
* The Time Variable/Axis
+ No Missing Values
+ Equidistant
* The Response Variable should be continuous or pseudo continuous
---
## Goals For today
1. Learn some basic R tools for exploring series
<!-- * No packages other than the pre-installed ones -->
<!-- * Introduce only things that can be relevant to everybody -->
2. Understand and quantify Serial correlation in time
3. Preparation for further analysis
<!-- construct a set of clues in terms of which lags are important in our system's dynamics. -->
<!-- Without going over methodology, assumptions checking, theory and math notation & formulae -->
---
## Why bother ?
* Variables in Time almost always behave differently than in space
<!-- and the areas of statistics that we usually study in the context of biology is, in many cases, not fit to handle them -->
* Explaining Observations - Multiple regression models are helpful, but they are not really designed to handle real time-series data
* For Real time series - Sample Summary Statistics (e.g.. Sample Mean and Variance ) are potentially misleading by themselves.
+ They do not have their usual properties
+ we can compute them (also for Time Series Statistics), but to describe populations we better remove systematic components from the samples first.
<!-- * Very developed mathematical field with many applications in -->
<!-- + Electromagnetic / Audio Signal Analysis -->
<!-- + Signal Processing -->
<!-- + Sales / Marketing -->
<!-- + Econometrics -->
<!-- + Materials and Manufacturing Control -->
<!-- * Much literature and expertise outside Ecology / Biology -->
<!-- = opportunity -->
---
## Objectives of Time Series Analysis
* Description - what we are here for today
* Explanation
* Predictions, both with and without external variables
* Control
* Detection of anomalies
<!-- ## Approaches -->
<!-- * Simple Descriptive techniques - plotting, looking for cycles etc..... -->
<!-- * Inference by Analysis in the Time Domain - based on the Correlogram -->
<!-- * Inference by Analysis in the Frequency Domain - based on the Spectrogram -->
<!-- * Linear systems - one system is considered the input, another the output -->
<!-- * Modelling and Model Comparison -->
---
Let's load some sample data and examine its structure
```{r}
blowfly <- read.csv("blowfly.csv", header = T)
blowfly
```
---
Time for a first look at Nicholson's flies
```{r}
plot(blowfly$total)
```
We can see some patterns already...
---
and using a time series plotting method:
```{r}
plot.ts(blowfly$total)
```
---
Multiple Time Series
```{r}
plot.ts(blowfly[1:3])
```
---
# The `ts` class
---
`plot.ts` plots the object as if it were of the special class of R objects known simply as *ts*
the `ts()` function is used to convert objects to the ts class
```{r}
flies <- ts(blowfly$total)
eggs<-ts(blowfly$eggs)
nonemerging<- ts(blowfly$nonemerging)
emerging<- ts(blowfly$emerging)
class(flies)
typeof(flies)
```
---
setting the `start`, `end`, and `frequency` parameters and functions
```{r}
start(flies) ; end(flies) ; frequency(flies)
```
---
Add a start year with `start` or `st`
```{r}
flies<-ts(blowfly$total, start=1954)
plot(flies)
print(flies,calendar=T)
```
---
Set time units and frequency with `start` and `frequency`
the frequency will be the number of columns
example: set a quarterly frequency
```{r}
flies<-ts(blowfly$total, start=c(1954,1),frequency = 4)
plot(flies)
print(flies,calendar=T)
```
---
set a monthly frequency
```{r}
flies<-ts(blowfly$total, start=c(1954,1), frequency = 12)
print(flies,calendar=T)
plot(flies)
```
---
explore different persepctives according to frequency with `aggregate` and `boxplot`
```{r}
par(mfrow=c(2,2))
plot(flies)
ag.flies<-aggregate(flies) ; plot(ag.flies)
cyc.flies<- cycle(flies) ; boxplot(flies ~ cyc.flies)
par(mfrow=c(1,1))
```
`cycle` was used to pull up the monthly interval as a categorical variable
---
Draw 2 series that have the same time axis together,
with `cbind`
```{r}
eggs2<-ts(blowfly$eggs, start=c(1954,1), frequency = 12)
plot(cbind(flies , eggs2 ))
```
---
set a frequency of every 2-3 days
```{r}
flies<-ts(blowfly$total, start=(c(1954,1)) , freq = 150)
print(flies,calendar=T)
plot(flies)
```
---
```{r}
par(mfrow=c(2,1))
blowfly$eggs %>% ts(frequency= 1) %>% plot
blowfly$eggs %>% ts(frequency= 52) %>% plot
par(mfrow=c(1,1))
```
---
the 'ts' class allows R to understand
* that a vector is based on a uni-directional time sequence
* convert frequencies easily
many base/stats functions are actually designed for the 'ts' class
but using them on a non- *ts* object may or may not result in an error
Warning: the `ts` class is designed only for equidistant series. other packages have other classes and functions which extend capabilities
---
# Time series exploration & Descriptive techniques
---
```{r echo=FALSE}
plot(flies)
```
* relatively clear cyclical process, at least at the beginning (points 1:200).
* the second part (200:361) looks less regular and with an upward trend.
---
## Exploratory Questions
* what are the cycle periods ?
* is there any hidden periodical behaviour that is not immediately apparent ?
* Do these 2 parts behave similarly or is there a fundamental change ? which attributes of the first part are conserved in the second part
```{r echo=FALSE}
plot(flies)
```
---
We can see some clearer cyclical behaviour of a section if we zoom in and explore a bit with `window`:
```{r}
flies<-ts(blowfly$total)
plot(window(flies, 1, 100, 1))
```
---
more zooming with `deltat`:
```{r}
par(mfrow=c(3,1))
plot(window(flies, 1, 100, deltat = 5))
plot(window(flies, 1, 100, deltat = 10))
plot(window(flies, 1, 100, deltat = 20))
par(mfrow=c(1,1))
```
---
```{r}
plot(window(flies, 1, 100, deltat = 20))
```
We finally lose the cyclical behaviour at ~`deltat=20`. let's remember this...
---
The flies series is unusually clear, and still - its cyclical behaviour is only one possible pattern.
other series may represent processes that behave in a way that is not immediately recognizable, and have many things going at once.
---
examples
```{r synthetic series, include=FALSE}
w <- rnorm(500, 0, 1) # this is actually a perfect white noise
rw <- cumsum(rnorm(100, 0, 1))
rwd <- cumsum(rnorm(200, 0, 1) + 0.05) # random walk with drift
sine.wn <- sin(1:200 / 5) + rnorm(200, mean = 20, sd = 1.5) + seq(0, 3, length.out = 200)
Z <- rnorm(250, 0, 1)
Y <- numeric(250)
Y[1] <- Z[1]
for (i in 2:250) Y[i] <- -0.5 * Y[i - 1] + Z[i]
```
```{r echo=FALSE}
plot.ts(w)
```
---
```{r echo=FALSE}
plot.ts(rwd)
```
---
```{r echo=FALSE}
plot.ts(sine.wn)
```
---
```{r echo=FALSE}
plot.ts(Y)
```
---
We naturally want tools that will describe the entire series' and parts of the series' attributes, be executable fast in R, and guide us in further steps (such as model fitting and forecasting).
for that, we need to understand how a given series evolves through time - via autocorrelation and related attributes of time series
---
## Measures of serial dependence
The Lag Plot
Auto-correlation
Partial Auto-correlation
Cross-Correlation
---
### Background on Autocorrelation - The lag plot.
how does my data behave in relation to itself ?
or 'how does this week's population relate to last week's population ?
```{r}
plot(flies[1:350], flies[2:351])
```
Clearly, there's a pattern at lag =1, at least in the smaller population sizes
---
now let's look at lags 2 and 3:
```{r fig.show='hold'}
par(mfrow=c(1,2))
plot(flies[1:350], flies[3:352])
plot(flies[1:350], flies[4:353])
par(mfrow=c(1,1))
```
we start losing the pattern gradually, as we go to higher lags...
----
more lags, lag = 1 to lag =8, using `lag.plot()`
```{r}
lag.plot(flies, lag = 8, layout = c(2, 4), diag = T)
```
---
however, at lags 9-16:
```{r}
lag.plot(flies, lags = 8, set.lags = 9:16, layout = c(2, 4), diag = T)
```
---
**summary**
* we begin with a pattern, lose it, and then find it again
* at smaller populations, we begin with a positive correlation, lose it, then a negative correlation which peaks at about lag = 10, lose it, and so the cycle goes.
---
### The Correlogram, using `acf()`
```{r}
acf(flies)
plot(flies)
```
---
This summarizes and charts the autocorrelation structure in the series, up to a default lag size, which is 25 in this case.
The vertical axis is the magnitude of the **Sample Autocorrelation Coefficients**. they measure the relative correlation between observations at different distances apart (lags)
and the horizontal axis is simply the lag size.
---
let's have a look at these **Sample Autocorrelation Coefficients**
```{r results='hide'}
a <- acf(flies, plot = F)
```
```{r }
a$acf %>% head(10) # for the actual factors
```
---
there's no better evidence of cycles, the flies exhibit strong positive AND negative correlation at regular intervals throughout the series, with a cycle period of 19 weeks.
```{r echo = FALSE}
acf(flies)
```
---
let's look at the entire series
```{r}
acf(flies, lag.max = 360, col = "red")
```
---
We can see
* the (sort of regular) cyclical pattern, especially at the beginning
* the diminishing positive correlation
* some alternation between positive and negative correlations, which is typical of cycles.
---
```{r}
acf(flies, lag.max = 360, col = "red")
```
warning: there is bias here with increasing lag size. why ?
---
* there's some kind of a diminishing but long term "memory" in the system, up to a lag of 100 weeks.
+ events that happened 50 weeks ago may still have an influence.
* clear cycles with a period of 19 weeks
---
The main point :there's a **LOT** of non-randomness in the sequence
This last point is especially important for linear and other modelling, which assume that samples are independent. we may want to stop and think before going on to perform a conventional `lm()` ! , and at the very least be aware of series' behaviour.
and this illustrates why we need to explore time series internally before we go on to the other, external parameters.
---
The **Sample Auto-covariance Coefficients**
```{r eval=T, include=T}
acf(flies, type = "covariance")
```
---
```{r eval=T, include=T}
acf(flies, type = "covariance", plot = F)$acf %>% head(10)
```
---
### Partial Autocorrelation
Correlograms produced using the sample *autocorrelation* coefficients do not account for the fact that for a given lag size there may be correlation between internal points, (e.g.. values that are 4 points apart were correlated, but so do values that are 2 points apart).
We sometimes want to control for the internal correlations inside the lag, or in other words, to check what would have been the correlation coefficients, had all the internal lags' coefficients were forced to zero.
This helps us later on, when selecting a model
---
the **Partial Autocorrelation ** is the relationship between this week's population and the population at lag n when we have already controlled for the correlations between all of the successive weeks between this week and week n
---
We only need to use `pacf` or `acf(type="partial")` instead of the default
```{r echo=TRUE}
acc <- a$acf %>% head(12) %>% round(2)
partial.acc <- pacf(flies, plot = F) %>% .$acf %>% head(12) %>% round(2)
acc
partial.acc
```
---
```{r}
plot(1:12, acc, xlab = "Lag", ylab = "Coefficient")
points(1:12, partial.acc, col = "blue")
lines(1:12, rep(0, 12))
legend("topright", c("ac coefficients", "Partial ac coefficients"), col = c("black", "green"), pch = c(1, 1))
```
---
```{r}
pacf(flies)
```
---
what can we take from this ?
* the maximal negative and maximal positive lags are important places in the cycle.
* clues for possible underlying processes in lags 3 and 12
the PACF can be thought of as analogous to a derivative of ACF - thus finding points driving change in pitch or direction.
---
Summary till now, using just Correlograms for the entire series
* cycle length - about 19
* Lags 2-4, 12-16 are suspected: we should look for biological mechanisms there, when we construct an explanatory model.
* Note: these are clues generated only from the time series itself, before considering any other data or knowledge in biology
---
### Cross Correlation
The total number of flies is not the only one we have...
we could always scatter-plot the data frame variables against each other
---
```{r}
plot(blowfly, lower.panel = NULL)
```
we get a strong correlation between total, deaths, and emerging. but there's an inherent problem in this analysis...
---
```{r}
flies <- ts(blowfly$total)
ccf(nonemerging, flies)
ccf(emerging, flies)
ccf(eggs, flies)
```
---
Reminder: Lags 2-4 (negative), 12-16 (positive) are suspected from `pacf()` of the total of "flies"
from `ccf()`
example, eggs vs. total-
* negative at lags 2-3
* negative at (-17 - -16)
* positive at 12-13
it can be difficult to construct an explanation from this alone, but at least we now have clues of lead times and delay times between every two variables.
This is a first step in time series stats, towards modelling.
---
findings:
cycle length = still 19
one possible explanation: larval processes *appear* to drive the cycles when they reach a maximum (density dependent competition ?)
---
### Spectral Analysis
an alternative , complementary approach to analysing fluctuations is to analyse how the variance evolves through time, by distributing it to frequencies.
This method can detect important processes that may not be found with `acf() ` to to too much noise in certain lags
#### The Periodogram, using `spectrum`
horizontal axis - frequencies instead of time
vertical - variance
```{r}
spectrum(flies, main = "")
```
---
in this analysis we look for frequency peaks. in this case:
```{r}
spec<-spectrum(flies, main = "")
spec$spec %>% which.max() -> maxloc # where's the maximal value ?
max.freq <- (spec$freq)[maxloc] # what's the maximal value ?
max.freq
```
---
now let's plot and find the exact value in time units
```{r}
plot(spec, main = "")
abline(v = max.freq, col = "red", lwd = 2)
```
---
```{r}
1 / max.freq
```
suggests again cycles of ~19 years.
the information may or may not be easier to interpret than a correlogram.
it's best to use both when exploring.
---
## typical series and interpretations
```{r echo=FALSE}
par(mfrow = c(3, 1))
plot(w)
acf(w, main = "")
spectrum(w, main = "")
par(mfrow = c(1, 1))
```
No definitive pattern in lags beyond 0 or a dominant frequency
typical of a *Pure White Noise* process, which is Normal errors around a steady mean.
---
```{r echo=FALSE}
par(mfrow = c(3, 1))
plot(ts(rw))
acf(rw, main = "")
spectrum(rw, main = "")
par(mfrow = c(1, 1))
```
No definitive pattern in lags, but high correlation for all lags
typical of a *Random Walk* process, where values are correlated to (mostly) only the latest value (the values in the previous step), plus a small *white noise*
---
**Super Important **
Is there a real trend here ? the time plot and our brain say there is. but the underlying process is probably not a *real* trend or combination of trends, but a kind of random 'drift'.
the next value is more likely to be a minor deviation from the last value, with equal odds for moving up or down, than a result of a another external driver !
random walks are notorious in deceiving our brains.
actually, the long term mean of the formula used to create this series,
`rw<-cumsum(rnorm(100,0,1))`
**is 0 !**
---
repeat:
```{r echo=FALSE}
rw <- cumsum(rnorm(100, 0, 1))
par(mfrow = c(3, 1))
plot(ts(rw))
acf(rw, main = "")
spectrum(rw, main = "")
par(mfrow = c(1, 1))
```
The same ACF even though the time plot is completely different
---
```{r echo=FALSE}
par(mfrow = c(3, 1))
plot(Y)
acf(Y)
pacf(Y, main = "")
par(mfrow = c(1, 1))
```
---
repeated negative followed by positive:
return to equilibrium following random departures from it.
This is called an **Autoregressive Process** of order 1 or 'AR(1)' (the order can be determined from the `pacf()`)
in this case a *negative* AR(1) with rapid response.
ecological models of equilibrium population dynamics are typically Autoregressive.
---
## analysing parts of a TS + de-trending
Simplest Approach:
1. visually identify a breakpoint
2. apply the same steps on parts of the series to find their attributes and identify discontinuities
---
now let's adapt the analysis for the two parts, separately
split the series:
```{r, fig.show='hold'}
first <- ts(flies[1:200])
second <- ts(flies[201:361])
```
---
```{r echo=FALSE, fig.show='hold'}
par(mfrow = c(1, 2))
plot(first, ylim = c(0, 14000))
plot(second)
par(mfrow = c(1, 1))
```
what are the notable differences between the 2 parts ?
<!-- e.g same cycle periods ? more 'condensed' ? -->
---
```{r acf on both parts, echo=FALSE, fig.show="hold"}
par(mfrow = c(2, 2))
acf(first)
acf(second)
pacf(first)
pacf(second)
par(mfrow = c(1, 1))
```
same overall shapes, same cycle length
---
Important Lags in the *second* parts are shifted 1 or 2, compared with the *first* - change points are somewhat closer to each other
what if we wanted to analyse the two with a commom baseline ?
<!-- the first and second parts appear to follow different trends -->
---
### de-trending using `lm()`
let's de-trend using linear regression, and have look at the results.
```{r de-trending usin a LM, results='hold'}
firstX <- 1:length(first)
first.lm <- lm(first ~ firstX)
first.detrended <- first - predict(first.lm)
secondX <- 1:length(second)
second.lm <- lm(second ~ secondX)
second.detrended <- second - predict(second.lm)
```
---
```{r de-trending using a LM, results='hold'}
par(mfrow = c(2, 2))
plot(first) ; lines(first.lm$fitted.values, col = "red")
plot(second) ; lines(second.lm$fitted.values, col = "blue")
plot.ts(first.detrended) # reminder Q: why plot.ts ?
plot.ts(second.detrended)
par(mfrow = c(1, 1))
```
---
```{r}
first.lm$coefficients
second.lm$coefficients
```
note: the pitch of of 'second' is 5 times that of first
---
now, let's compare again
```{r acf on both parts, detrended, fig.show="hold"}
par(mfrow = c(2, 2))
acf(first.detrended) ; acf(second.detrended)
pacf(first.detrended); pacf(second.detrended)
par(mfrow = c(1, 1))
```
---
comparing the de-trended versions:
`acf()` - results similar, but `pacf()` are quite different
conclusions:
* suspected trend change
* continuity in cycle period at the same time as a trend change,
* quite a difference in the underlying change points
* also: a new negative partial coefficient at lag 18
---
### de-trending using `diff()`
```{r}
par(mfrow = c(2, 1))
plot(flies)
flies %>% diff() %>% plot()
par(mfrow = c(1, 1))
```
---
```{r echo=FALSE}
first.diff <- diff(first)
second.diff <- diff(second)
par(mfrow = c(3, 2))
plot(first)
lines(first.lm$fitted.values, col = "red")
plot(second)
lines(second.lm$fitted.values, col = "blue")
plot.ts(first.detrended) # reminder Q: why plot.ts ?
plot.ts(second.detrended)
plot(first.diff)
plot(second.diff)
par(mfrow = c(1, 1))
```
```{r echo=FALSE}
par(mfrow = c(3, 2))
plot(first)
plot(second)
acf(first.detrended, main = "")
acf(second.detrended, main = "")
acf(first.diff, main = "")
acf(second.diff, main = "")
par(mfrow = c(1, 1))
```
### De-trending Remarks
De-trending is the goal of many methods in time series analysis that are not presented here
+first order differencing will usual be enough coerce the series to become stationary, and will do it while still keeping the attributes of the signal. de-trending by a `lm()` may create noise that was not there.
it has several uses in model selection, and the advantage that parameters do not need to be estimated (e.g. from a linear model)
***
## Smoothing
let's return to some noisier series
```{r include=FALSE}
w <- ts(w)
rwd <- ts(rwd)
sine.wn <- ts(sine.wn)
Y <- ts(Y)
```
```{r}
par(mfrow=c(2,2))
plot(w)
plot(rwd)
plot(sine.wn)
plot(Y)
par(mfrow=c(1,1))
```
---
last time we used `window()`
```{r}
par(mfrow = c(2, 2))
plot(window(w, 100, 200))
plot(window(w, 100, 150))
plot(window(w, 100, 125))
par(mfrow = c(1, 1))
```
this approach doesn't work
we should probably start looking by smoothing first
### smoothing via `filter()`
`filter` allows many methods for smoothing
#### filtering with a moving average
this is the simplest technique
we use the filter argument with a weights vector, giving equal weights
```{r, fig.show="hold"}
par(mfrow = c(3, 1))
v <- filter(w, sides = 2, filter = rep(1, 3) / 3)
plot(w)
lines(v, col = "red")
rwd.s <- filter(rwd, sides = 2, filter = rep(1, 3) / 3)
plot(rwd)
lines(rwd.s, col = "red")
sine.wn.s <- filter(sine.wn, sides = 2, filter = rep(1, 3) / 3)
plot(sine.wn)
lines(sine.wn.s, col = "red")
Y.s <- filter(Y, sides = 2, filter = rep(1, 3) / 3)
plot(Y)
lines(Y.s, col = "red")
par(mfrow = c(1, 1))
```
let's try a 9 point average
```{r,fig.show="hold"}
v <- filter(w, sides = 2, filter = rep(1, 9) / 9)
plot(w)