-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNA_replacer.R
More file actions
executable file
·59 lines (45 loc) · 1.55 KB
/
NA_replacer.R
File metadata and controls
executable file
·59 lines (45 loc) · 1.55 KB
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
# Ths function finds and replaces NA in df and dfs (maize, wheat and soybeans 2005-2016 prices
# and soybeans 1994-2005 prices) using ARIMA models.
# First df column must contain date.
NA_replacer_ARMA <- function(df, pq=c(1,1)) {
if(!is.data.frame(df)){
df <- data.frame(df)
}
commodity.columns <- colnames(df)[colnames(df)!='Date']
## NA SEEK & REPLACE ##
# Models fitting and BIC estimation
data.frame('Date'=df[,'Date'],
sapply(df[,commodity.columns], function(column){
## DATA SCALING ##
column <- column / 100
NA.ind <- which(is.na(column))
min.BIC <- -1
opt.pq <- c(NA, NA)
for (j in 1:length(NA.ind)) {
for (p in 1:pq[1]) {
for (q in 1:pq[2]) {
bic <- BIC(arima(column[1:NA.ind[j]],c(p,0,q)))
if(bic < min.BIC){
min.BIC <- bic
opt.pq <- c(p, q)
}
}
}
column[NA.ind[j]] <- as.numeric(
predict(arima(column[1:NA.ind[j]], c(opt.pq[1], 0, opt.pq[2])), n.ahead=1))[1]
}
## DATA RESCALING ##
column * 100
})
)
}
NA_replacer_splines <- function(df) {
for (i in 2:length(df)) {
NA.ind <- which(is.na(df[,i]))
for (j in 1:length(NA.ind)) {
df[NA.ind[j],i] <-
predict(smooth.spline(df[1:NA.ind[j]-1,i], spar = NULL),NA.ind[j])$y
}
}
return(df)
}