-
Notifications
You must be signed in to change notification settings - Fork 8
/
01-Part2.r
96 lines (51 loc) · 1.57 KB
/
01-Part2.r
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
library(ggplot2);
library(gridExtra);
library(reshape2);
ensure.installed <- function(package){
if (!package %in% installed.packages()) install.packages(package)
}
###
### Worksheet Exercise 1.1
###
source('setup_data.R', echo = TRUE);
class(AP.ts);
str(AP.ts);
start(AP.ts); end(AP.ts); frequency(AP.ts);
plot(AP.ts, ylab = "Air Passengers (\'000s)");
### Using ggplot2 looks better, but you have to work hard for the
### labels on the x-axis so I am leaving this out for now.
#qplot(1:length(AP.ts), as.vector(AP.ts), geom = 'line', ylab = 'Air Passengers (\'000s)');
###
### Worksheet Exercise 2.1
###
head(CBE.df);
tail(CBE.df);
str(CBE.df);
###
### Worksheet Exercise 2.2
###
beer.ts <- ts(CBE.df$beer, start = 1958, freq = 12);
choc.ts <- ts(CBE.df$choc, start = 1958, freq = 12);
elec.ts <- ts(CBE.df$elec, start = 1958, freq = 12);
plot(cbind(beer.ts, choc.ts, elec.ts));
###
### Worksheet Exercise 2.3
###
source('setup_data.R', echo = TRUE);
elec.ts <- ts(CBE.df$elec, start = 1958, freq = 12);
AP.elec.ts <- ts.intersect(AP.ts, elec.ts);
head(AP.elec.ts); tail(AP.elec.ts);
str(AP.elec.ts);
plot(AP.elec.ts);
### Create a plot in ggplot2
#qplot(Var1, value, data = melt(AP.elec.ts), geom = 'line', colour = Var2);
###
### Worksheet Exercise 2.4
###
source('setup_data.R', echo = TRUE);
elec.ts <- ts(CBE.df$elec, start = 1958, freq = 12);
AP.elec.ts <- ts.intersect(AP.ts, elec.ts);
AP.elec.cor <- cor(AP.elec.ts);
str(AP.elec.cor);
### Show the scaled plot
qplot(Var1, value, data = melt(scale(AP.elec.ts)), geom = 'line', colour = Var2);