- h_endo = ifelse(simtype=="mix", h[1], h)
-
- # Distances from last observed day to days in the past
- distances2 = rep(NA, length(fdays))
- for (i in seq_along(fdays))
+ # Neighbors: days in "same season"; TODO: 60 == magic number...
+ fdays = getSimilarDaysIndices(today, data, limit=60, same_season=TRUE,
+ days_in=fdays_cut)
+ if (length(fdays) <= 1)
+ return (NA)
+ levelToday = data$getLevel(today)
+ distances = sapply(fdays, function(i) abs(data$getLevel(i)-levelToday))
+ #TODO: 2, 10, 3, 12 magic numbers here...
+ dist_thresh = 2
+ min_neighbs = min(10,length(fdays))
+ repeat
+ {
+ same_pollution = (distances <= dist_thresh)
+ nb_neighbs = sum(same_pollution)
+ if (nb_neighbs >= min_neighbs) #will eventually happen
+ break
+ dist_thresh = dist_thresh + 3
+ }
+ fdays = fdays[same_pollution]
+ max_neighbs = 12
+ if (nb_neighbs > max_neighbs)
+ {
+ # Keep only max_neighbs closest neighbors
+ fdays = fdays[
+ sort(distances[same_pollution],index.return=TRUE)$ix[1:max_neighbs] ]
+ }
+ if (length(fdays) == 1) #the other extreme...