-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRnetlogo_stat.R
291 lines (214 loc) · 11.2 KB
/
Rnetlogo_stat.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
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
######################################
##1.Import multiple files to R env--##
######################################
library(tidyverse)
setwd("~/Dropbox (Cambridge University)/2018_Cambridge/[Conferences and Journals]/J_JASSS/JASSS_Data")
files <- list.files(pattern= "seoul_|gg_.*.csv")
tables <- lapply(files, read.csv, header = TRUE,fileEncoding="CP949", encoding="UTF-8")
aq <- do.call(rbind, tables)
aq[aq == -999] <- NA
setwd("~/Dropbox (Cambridge University)/2018_Cambridge/[Conferences and Journals]/J_JASSS/JASSS_Data")
for(i in c(3,5:10)){
aq[,i] <- as.numeric(aq[,i])
}
aq$time1 <- lubridate::parse_date_time(as.character(aq$Date), "ymdH", tz = "Asia/Seoul")
aq$DATE <- lubridate::as_date(as.character(aq$Date), format = "%Y%m%d",tz = "Asia/Seoul")
aq$hour <- lubridate::hour(aq$time)
aq <- aq %>% arrange(Station.ID)
# Gangnam Background station
aq.g <- aq %>% filter (Station.ID == 111261)
gn <- aq.g %>% select(time1, DATE, hour,pm10)
# Gwanak Background station
aq.gw <- aq %>% filter (Station.ID == 111251)
gw <- aq.gw %>% select(time1, DATE, hour,pm10)
####################
#-- Missing Data --#
####################
library(imputeTS)
library(xts)
library(forecast)
## In the package, there are three steps that one can follow
## 1) Plot NA, 2) Use various NA functions, 3) Analyse after imputation
#-- 1. Transform a dataframe to a multi-sequence timeseries data
time_index <- seq(from = as.POSIXct("2010-01-01 01:00"),
to = as.POSIXct("2018-01-01 00:00"), by = "hour", tz = "Asia/Seoul")
gn.NA <- gn[[4]]
df_ts <- msts(gn.NA, seasonal.periods = c(24*7) )
gw.NA <- gw[[4]]
gw_ts <- msts(gw.NA, seasonal.periods = c(24*7) )
#-- 2. NA distribution
# 1) Gangnam
plotNA.distribution(df_ts)
plotNA.distribution(df_ts[17520:26303], main = "Distribution of NAs in 2012") # year 2012
plotNA.distributionBar(df_ts, breaks=20,main = "Distribution of NAs\n 2010-2018") # For long time-series
plotNA.distributionBar(df_ts[17520:26303], main = "Distribution of NAs\n 2012", breaks=20)
# 2) Gwanak
plotNA.distribution(gw_ts)
plotNA.distribution(gw_ts[17520:26303], main = "Distribution of NAs in 2012") # year 2012
plotNA.distributionBar(gw_ts, breaks=20,main = "Distribution of NAs\n 2010-2018") # For long time-series
plotNA.distributionBar(gw_ts[17520:26303], main = "Distribution of NAs\n 2012", breaks=20)
statsNA(df_ts) # NA Statistics summary
statsNA(df_ts[17520:26303])
statsNA(gw_ts) # NA Statistics summary
statsNA(gw_ts[17520:26303])
#-- 3. Imputation Options
#-- 1)Basic imputation
na.mean(df_ts, option = "mean")
na.mean(df_ts, option = "median") # Median Imputation
na.mean(df_ts, option = "mode") # Mode Imputation
#-- 2) Replace Missing values by a defined value
na.replace(df_ts, fill = 0)
#-- 3) NA Interpolation
na.l <- na.interpolation(df_ts, option = "linear")
na.p <- na.interpolation(df_ts, option = "spline")
na.t <- na.interpolation(df_ts, option = "stine")
plot(na.l)
#-- 4) NA Kalman: Missing Value Imputation by Kalman Smoothing and State Space Models
# na.k <- na.kalman(df_ts, model = "StructTS", smooth = T) # Don't know why it takes so much time!!
#-- 5) Weighted Moving Average
na.ma(tsAirgap, weighting = "simple")
na.ma(tsAirgap, k = 6, weighting = "simple")
na.ma(df_ts, weighting = "exponential")
#-- 6) Time series imputation
a <- na.random(df_ts) # Random Imputation
b <- na.locf(df_ts, option = "locf") # Last Obs. Carried Forward
c <- na.locf(df_ts, option = "nocb") # Next Obs. Carried Backward
d <- na.interpolation(df_ts) # Linear Interpolation
#-- Missing Value Imputation by seasons
seadec <- na.seadec(df_ts, algorithm = "interpolation") # Seasonal Adjustment then Linear Interpolation
seasplit <- na.seasplit(df_ts, algorithm = "interpolation") # Seasonally Splitted Missing Value Imputation
season <- as.data.frame(cbind(seadec, seasplit))
#a <- gn[580:600,]
# Test: Year 2012 26303
df_12 <- df_ts[17520:18263] %>% msts(seasonal.periods = (24) )
gn.locf <- na.locf(df_12, option = "locf") # Last Obs. Carried Forward
plot(gn.locf, type = "l", main = "LOCF imputation", xlab = "Days", ylab = "PM10")
lines(df_12, col = "blue", lwd = 2)
gn.nocb <- na.locf(df_12, option = "nocb") # Next Obs. Carried Backward
plot(gn.nocb, type = "l", main = "NOCB imputation", xlab = "Days", ylab = "PM10")
lines(df_12, col = "violet", lwd = 2)
gn.ma <- na.ma(df_12, weighting = "simple")
plot(gn.ma, type = "l", main = "Moving average imputation", xlab = "Days", ylab = "PM10")
lines(df_12, col = "red", lwd = 2)
gn.int <- na.interpolation(df_12)
plot(gn.int, type = "l", main = "Linear interpolated imputation", xlab = "Days", ylab = "PM10")
lines(df_12, col = "orange", lwd = 2)
#gn.kal <- na.kalman(df_12, algorithm = "interpolation") # Seasonal Adjustment then Linear Interpolation
#plot(gn.kal, type = "l", main = "Seasonal decomposited imputation", xlab = "Days", ylab = "PM10")
#lines(df_12, col = "khaki2", lwd = 2)
gn.sdec <- na.seadec(df_12, algorithm = "ma") # Seasonal Adjustment then Linear Interpolation
plot(gn.sdec, type = "l", main = "Seasonal decomposited imputation\nMoving Average", xlab = "Days", ylab = "PM10")
lines(df_12, col = "khaki2", lwd = 2)
gn.sdec <- na.seadec(df_12, algorithm = "interpolation") # Seasonal Adjustment then Linear Interpolation
plot(gn.sdec, type = "l", main = "Seasonal decomposited imputation\nInterplation", xlab = "Days", ylab = "PM10")
lines(df_12, col = "khaki2", lwd = 2)
gn.sdec <- na.seadec(df_12, algorithm = "kalman") # Seasonal Adjustment then Linear Interpolation
plot(gn.sdec, type = "l", main = "Seasonal decomposited imputation\nKalman Smoothing", xlab = "Days", ylab = "PM10")
lines(df_12, col = "khaki2", lwd = 2)
gn.sdec <- na.seadec(df_12, algorithm = "mean") # Seasonal Adjustment then Linear Interpolation
plot(gn.sdec, type = "l", main = "Seasonal decomposited imputation\nRandom", xlab = "Days", ylab = "PM10")
lines(df_12, col = "khaki2", lwd = 2)
#### Seasonal Split #####
#1) Gangnam
par(mfrow=c(2,2))
gn.12.sma <- na.seasplit(df_12, algorithm = "ma")
plot(gn.12.sma, type = "l", main = "Seasonal splitted imputation\nMoving Average", xlab = "Days", ylab = "PM10")
lines(df_12, col = "khaki1", lwd = 2)
gn.12.sint <- na.seasplit(df_12, algorithm = "interpolation")
plot(gn.12.sint, type = "l", main = "Seasonal splitted imputation\nInterpolation", xlab = "Days", ylab = "PM10")
lines(df_12, col = "khaki2", lwd = 2)
gn.12.skal <- na.seasplit(df_12, algorithm = "kalman")
plot(gn.12.skal, type = "l", main = "Seasonal splitted imputation\nKalman", xlab = "Days", ylab = "PM10")
lines(df_12, col = "khaki3", lwd = 2)
gn.12.smean <- na.seasplit(df_12, algorithm = "mean")
plot(gn.12.smean, type = "l", main = "Seasonal splitted imputation\nMean", xlab = "Days", ylab = "PM10")
lines(df_12, col = "khaki4", lwd = 2)
par(mfrow=c(1,1))
gn.sma <- na.seasplit(df_ts, algorithm = "ma")
plot(gn.sma, type = "l", main = "Seasonal splitted imputation\nMoving Average", xlab = "Days", ylab = "PM10")
gn.sint <- na.seasplit(df_ts, algorithm = "interpolation")
plot(gn.sint, type = "l", main = "Seasonal splitted imputation\nInterpolation", xlab = "Days", ylab = "PM10")
gn.skal <- na.seasplit(df_ts, algorithm = "kalman")
plot(gn.skal, type = "l", main = "Seasonal splitted imputation\nKalman", xlab = "Days", ylab = "PM10")
gn.smean <- na.seasplit(df_ts, algorithm = "mean")
plot(gn.smean, type = "l", main = "Seasonal splitted imputation\nMean", xlab = "Days", ylab = "PM10")
gn.new <- cbind(gn.smean, gn.sma, gn.sint, gn.skal) %>%
as.data.frame() %>%
rename(ts_mean = gn.smean, ts_ma = gn.sma, ts_int = gn.sint, ts_kal = gn.skal)
gn.new <- cbind(gn, gn.new) %>% select(-c(pm10,time1))
#2) Gwanak
gw.sma <- na.seasplit(gw_ts, algorithm = "ma")
plot(gw.sma, type = "l", main = "Seasonal splitted imputation\nMoving Average", xlab = "Days", ylab = "PM10")
gw.sint <- na.seasplit(gw_ts, algorithm = "interpolation")
plot(gw.sint, type = "l", main = "Seasonal splitted imputation\nInterpolation", xlab = "Days", ylab = "PM10")
gw.skal <- na.seasplit(gw_ts, algorithm = "kalman")
plot(gw.skal, type = "l", main = "Seasonal splitted imputation\nKalman", xlab = "Days", ylab = "PM10")
gw.smean <- na.seasplit(gw_ts, algorithm = "mean")
plot(gw.smean, type = "l", main = "Seasonal splitted imputation\nMean", xlab = "Days", ylab = "PM10")
gw.new <- cbind(gw.smean, gw.sma, gw.sint, gw.skal) %>%
as.data.frame() %>%
rename(ts_mean = gw.smean, ts_ma = gw.sma, ts_int = gw.sint, ts_kal = gw.skal)
gw.new <- cbind(gn, gw.new) %>% select(-c(pm10,time1))
#####################################
#-- Clean dataframe with reshape2 --#
#####################################
# 1. Gangnam
gnclean <- reshape2::melt(gn.new, id = c("DATE", "hour"),
variable.name = "Type",value.name = "Value") %>%
mutate(work = case_when(.$hour >= 9 & .$hour <= 19 ~ "work",
.$hour < 9 | .$hour > 19 ~ "home"),
hour = replace(hour, hour == 0, 24)
)
gncast <- gnclean %>% reshape2::dcast(DATE + Type + work ~ hour + ., value.var = "Value")
df1 = as.data.frame(t(apply(gncast,1, function(x) { return(c(x[!is.na(x)],x[is.na(x)]) )} )))
colnames(df1) = c("dates", "type","work","h1","h2","h3","h4","h5","h6","h7","h8","h9","h10","h11","h12",
"h13","h14","h15","h16","h17","h18","h19","h20","h21","h22","h23","h24")
gndf <- df1 %>% select(1:16) #
for(i in c(4:16)){
gndf[,i] <- as.integer(as.character(gndf[,i]))
}
gndf <- gndf %>% replace_na(list(h12 =-999, h13 = -999)) %>% arrange(type,dates)
# 2. Gwanak
gwclean <- reshape2::melt(gw.new, id = c("DATE", "hour"),
variable.name = "Type",value.name = "Value") %>%
mutate(work = case_when(.$hour >= 9 & .$hour <= 19 ~ "work",
.$hour < 9 | .$hour > 19 ~ "home"),
hour = replace(hour, hour == 0, 24)
)
gwcast <- gwclean %>% reshape2::dcast(DATE + Type + work ~ hour + ., value.var = "Value")
df2 = as.data.frame(t(apply(gwcast,1, function(x) { return(c(x[!is.na(x)],x[is.na(x)]) )} )))
colnames(df2) = c("dates", "type","work","h1","h2","h3","h4","h5","h6","h7","h8","h9","h10","h11","h12",
"h13","h14","h15","h16","h17","h18","h19","h20","h21","h22","h23","h24")
gwdf <- df2 %>% select(1:16) #
for(i in c(4:16)){
gwdf[,i] <- as.integer(as.character(gwdf[,i]))
}
gwdf <- gwdf %>% replace_na(list(h12 =-999, h13 = -999)) %>% arrange(type,dates)
#write.csv(gwdf,"gn.csv")
#################################
gn.tib <- as_tibble(gnclean) %>%
group_by(Type)
gn.quart <- gn.tib %>%
tq_transmute(
select = Value,
mutate_fun = apply.quarterly,
FUN = mean
)
gn.quart$inc10 <- gn.quart$Value * 1.10
gn.quart$inc20 <- gn.quart$Value * 1.20
gn.quart$inc10sub <- round(gn.quart$inc10 - gn.quart$Value, 2)
gn.quart$inc20sub <- round(gn.quart$inc20 - gn.quart$Value, 2)
gw.tib <- as_tibble(gwclean) %>%
group_by(Type)
gw.quart <- gw.tib %>%
tq_transmute(
select = Value,
mutate_fun = apply.quarterly,
FUN = mean
)
gw.quart$inc10 <- gw.quart$Value * 1.10
gw.quart$inc20 <- gw.quart$Value * 1.20
gw.quart$inc10sub <- round(gw.quart$inc10 - gw.quart$Value, 2)
gw.quart$inc20sub <- round(gw.quart$inc20 - gw.quart$Value, 2)
#write.csv(gn.quart,"gnquart.csv")
#write.csv(gw.quart,"gwquart.csv")