-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 20.8 KB
/
.Rhistory
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
tempdata$spline <- a(miss)
View(tempdata)
a <- splinefun(tempdata$temp_missing)
b <- a(tempdata$temp_missing(1:70))
View(tempdata)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("ambtemp", "ambtemp_missing", "namav"),
lty = 1,col = tsRainbow)
rm(list=ls())
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time))
tempdata <- xts(dd$temp, order.by = dt)
colnames(tempdata) <- "temp"
tempdata$temp_missing <- tempdata$temp
# create a column with some missing values
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
namav <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
tempdata$temp_namav <- namav(tempdata$temp_missing)
View(tempdata)
miss <- which(is.na(tempdata$temp_missing))
a <- splinefun(tempdata$temp_missing)
tempdata$temp_spline[miss] <- a(miss)
miss <- which(is.na(tempdata$temp_missing))
a <- splinefun(tempdata$temp_missing)
a(miss)
tempdata[miss]
tempdata$temp_missing[miss]
length(tempdata$temp_missing[miss])
length(a(miss))
tempdata$temp_missing[miss] <- a(miss)
# Set a color scheme:
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_spline"),
lty = 1,col = tsRainbow)
View(tempdata)
rm(list=ls())
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
View(dd)
dt <- as.POSIXct(paste(dd$date, dd$time))
dt
tempdata <- xts(dd$temp, order.by = dt)
View(tempdata)
colnames(tempdata) <- "temp"
View(tempdata)
row.names[1,1]
tempdata[1,1]
tempdata[1,2]
tempdata[2,1]
tempdata$temp_missing <- tempdata$temp
View(tempdata)
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
# Set a color scheme:
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing"),
lty = 1,col = tsRainbow)
View(tempdata)
namav <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
tempdata$temp_namav <- namav(tempdata$temp_missing)
View(tempdata)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_namav"),
lty = 1,col = tsRainbow)
miss <- which(is.na(tempdata$temp_missing))
a <- splinefun(tempdata$temp_missing)
tempdata$temp_missing[miss] <- a(miss)
View(tempdata)
tempdata$temp_spline[miss] <- a(miss)
# Set a color scheme:
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_namav"),
lty = 1,col = tsRainbow)
View(tempdata)
rm(list=ls())
# datafill example
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time)) # create a vector of datetimes (dt)
tempdata <- xts(dd$temp, order.by = dt) # create a time series ordered by dt
colnames(tempdata) <- "temp" # rename the column
tempdata$temp_missing <- tempdata$temp # create a new column for missing data
# create some missing values
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
##### first approach
# create a moving average function (namav)
namav <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
# apply namav to fill missing data in new column
tempdata$temp_namav <- namav(tempdata$temp_missing)
View(tempdata)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_namav"),
lty = 1,col = tsRainbow)
miss <- which(is.na(tempdata$temp_missing))
a <- splinefun(tempdata$temp_missing)
a
tempdata$temp_spline <- tempdata$temp_missing # create new column for spline filling
View(tempdata)
tempdata$temp_spline[miss] <- a(miss)
View(tempdata)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_namav", "temp_spline"),
lty = 1,col = tsRainbow)
rm(list=ls())
# datafill example
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time)) # create a vector of datetimes (dt)
tempdata <- xts(dd$temp, order.by = dt) # create a time series ordered by dt
colnames(tempdata) <- "temp" # rename the column
tempdata$temp_missing <- tempdata$temp # create a new column for missing data
# create some missing values
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
##### first approach
# create a moving average function (namav)
namav <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
# apply namav to fill missing data in new column
tempdata$temp_namav <- namav(tempdata$temp_missing)
View(tempdata)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_namav", "temp_spline"),
lty = 1,col = tsRainbow)
miss <- which(is.na(tempdata$temp_missing))
a <- splinefun(tempdata$temp_missing)
tempdata$temp_spline <- NA
View(tempdata)
tempdata$temp_spline[miss] <- a(miss) # fill the missing values with spline function (a)
View(tempdata)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_namav", "temp_spline"),
lty = 1,col = tsRainbow)
rm(list=ls())
# datafill example
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time)) # create a vector of datetimes (dt)
tempdata <- xts(dd$temp, order.by = dt) # create a time series ordered by dt
colnames(tempdata) <- "temp" # rename the column
tempdata$temp_missing <- tempdata$temp # create a new column for missing data
# create some missing values
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
##### first approach
# create a moving average function (namav)
namav <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
# apply namav to fill missing data in new column
tempdata$temp_namav <- namav(tempdata$temp_missing)
View(tempdata)
View(tempdata)
tempdata$temp_spline <- NA # create new column for spline filling
View(tempdata)
miss <- which(is.na(tempdata$temp_missing)) # vector of missing data positions
View(tempdata)
tempdata$temp_spline <- tempdata$temp_missing # create new column for spline filling
View(tempdata)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_namav", "temp_spline"),
lty = 1,col = tsRainbow)
View(tempdata)
tempdata$temp_spline[miss] <- a(miss) # fill the missing values with spline function (a)
a <- splinefun(tempdata$temp_missing) # create spline function (a) on temp_missing
tempdata$temp_spline <- tempdata$temp_missing # create new column for spline filling
View(tempdata)
tempdata$temp_spline[miss] <- a(miss) # fill the missing values with spline function (a)
View(tempdata)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_namav", "temp_spline"),
lty = 1,col = tsRainbow)
tempdata$mi <- mice(tempdata$temp_missing)
tempdata$mi <- mice(tempdata$temp_missing, tempdata$row.names)
rm(list=ls())
# datafill example
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time)) # create a vector of datetimes (dt)
tempdata <- xts(dd$temp, order.by = dt) # create a time series ordered by dt
colnames(tempdata) <- "temp" # rename the column
tempdata$temp_missing <- tempdata$temp # create a new column for missing data
# create some missing values
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
View(tempdata)
rm(list=ls())
# datafill example
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time)) # create a vector of datetimes (dt)
tempdata <- xts(dd$temp, order.by = dt) # create a time series ordered by dt
colnames(tempdata) <- "temp" # rename the column
# create some missing values
tempdata$temp[10,1] <- NA
tempdata$temp[15:16,1] <- NA
tempdata$temp[30:32,1] <- NA
tempdata$temp[40:44,1] <- NA
tempdata$temp[54:60,1] <- NA
View(tempdata)
tempdata$mi <- mice(tempdata)
rm(list=ls())
# datafill example
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time)) # create a vector of datetimes (dt)
tempdata <- xts(dd$temp, order.by = dt) # create a time series ordered by dt
colnames(tempdata) <- "temp" # rename the column
tempdata$temp_missing <- tempdata$temp # create a new column for missing data
# create some missing values
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
View(tempdata)
mav2 <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav3 <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav5 <- function(x,k=5){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav10 <- function(x,k=10){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
tempdata$temp_mav2 <- mav2(tempdata$temp_missing)
tempdata$temp_mav3 <- mav3(tempdata$temp_missing)
tempdata$temp_mav5 <- mav5(tempdata$temp_missing)
tempdata$temp_mav10 <- mav10(tempdata$temp_missing)
View(tempdata)
# third approach, use splines() to fill missing data
# tempdata$temp_spline <- NA # create new column for spline filling
miss <- which(is.na(tempdata$temp_missing)) # vector of missing data positions
a <- splinefun(tempdata$temp_missing) # create spline function (a) on temp_missing
tempdata$temp_spline <- tempdata$temp_missing # create new column for spline filling
tempdata$temp_spline[miss] <- a(miss) # fill the missing values with spline function (a)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_mav2", "temp_mav3",
"temp_mav5", "temp_mav10","temp_spline"),
lty = 1,col = tsRainbow)
rm(list=ls())
# datafill example
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time)) # create a vector of datetimes (dt)
tempdata <- xts(dd$temp, order.by = dt) # create a time series ordered by dt
colnames(tempdata) <- "temp" # rename the column
tempdata$temp_missing <- tempdata$temp # create a new column for missing data
# create some missing values
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
##### first approach, use multiple imputation (mice) to fill missing data NEEDS TWO COLUMNS
# tempdata$mi <- mice(tempdata)
##### second approach
# create a moving average function (mav) with different window widths
mav2 <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav3 <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav5 <- function(x,k=5){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav10 <- function(x,k=10){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
# apply mav functions to fill missing data in new columns
tempdata$temp_mav2 <- mav2(tempdata$temp_missing)
# tempdata$temp_mav3 <- mav3(tempdata$temp_missing)
tempdata$temp_mav5 <- mav5(tempdata$temp_missing)
tempdata$temp_mav10 <- mav10(tempdata$temp_missing)
View(tempdata)
t the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_mav2",
"temp_mav5", "temp_mav10"),
lty = 1,col = tsRainbow)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_mav2",
"temp_mav5", "temp_mav10"),
lty = 1,col = tsRainbow)
rm(list=ls())
# datafill example
require(xts)
require(TTR)
require(PerformanceAnalytics)
require(RColorBrewer)
require(mice)
dd <- read.table("~/Downloads/ts.txt", skip=1)
dd <- dd[,-1] # cut off the row numbers
colnames(dd) <- c("temp", "date", "time") # name the three remaining columns
dt <- as.POSIXct(paste(dd$date, dd$time)) # create a vector of datetimes (dt)
tempdata <- xts(dd$temp, order.by = dt) # create a time series ordered by dt
colnames(tempdata) <- "temp" # rename the column
tempdata$temp_missing <- tempdata$temp # create a new column for missing data
# create some missing values
tempdata$temp_missing[10,1] <- NA
tempdata$temp_missing[15:16,1] <- NA
tempdata$temp_missing[30:32,1] <- NA
tempdata$temp_missing[40:44,1] <- NA
tempdata$temp_missing[54:60,1] <- NA
##### first approach, use multiple imputation (mice) to fill missing data NEEDS TWO COLUMNS
# tempdata$mi <- mice(tempdata)
##### second approach
# create a moving average function (mav) with different window widths
mav2 <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav3 <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav5 <- function(x,k=5){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
mav10 <- function(x,k=10){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum(is.na(x[(i-k):(i+k)])))))
}
# apply mav functions to fill missing data in new columns
tempdata$temp_mav2 <- mav2(tempdata$temp_missing)
# tempdata$temp_mav3 <- mav3(tempdata$temp_missing)
# tempdata$temp_mav5 <- mav5(tempdata$temp_missing)
tempdata$temp_mav10 <- mav10(tempdata$temp_missing)
# third approach, use splines() to fill missing data
# tempdata$temp_spline <- NA # create new column for spline filling
miss <- which(is.na(tempdata$temp_missing)) # vector of missing data positions
a <- splinefun(tempdata$temp_missing) # create spline function (a) on temp_missing
tempdata$temp_spline <- tempdata$temp_missing # create new column for spline filling
tempdata$temp_spline[miss] <- a(miss) # fill the missing values with spline function (a)
tsRainbow <- rainbow(ncol(tempdata))
# Plot the overlayed series
plot(as.zoo(tempdata), ylab = "Temperature", xlab = "Time", main = "Ambient Temps",
col = tsRainbow, screens = 1)
# Set a legend in the upper left hand corner to match color to return series
legend(x = "topleft", legend = c("temp", "temp_missing", "temp_mav2", "temp_mav10", "temp_spline"),
lty = 1,col = tsRainbow)