Created
July 27, 2016 15:03
-
-
Save jlsutherland/2b98ce5185b65172a4f584f696a2ebb2 to your computer and use it in GitHub Desktop.
Performs daily pooling, fill forward, and loess on bank data. Can set bandwidth with variable `days`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| vector.is.empty <- function (x) return(length(x) == 0 ) | |
| smoothFillForward <- function (bank, days) { | |
| ## Assumes there is a csv of raw scores in the working directory for the bank. | |
| ## Assumes that the name of that csv is ${BANK}_scores.csv. | |
| ## Assumes that the csv has a score, a date, and a residual column. | |
| ## Get data from CSV. | |
| all_scores <- read.csv(sprintf('%s_scores.csv', bank)) | |
| all_scores$date <- as.Date(all_scores$date, format="%Y-%m-%d") | |
| all_scores <- all_scores[order(all_scores$date),] | |
| raw_scores <- all_scores$score | |
| residuals <- all_scores$residual | |
| date <- all_scores$date | |
| ## Set up the date range. | |
| beginning <- date[1] | |
| end <- tail(date, n=1) | |
| range <- seq(as.Date(beginning), as.Date(end), by="1 day") | |
| ## Carry-forward or pool raw. | |
| previous <- 0 | |
| pooledraw <- rep(NA, length(range)) | |
| for (i in 1:length(range)) { | |
| rawsub <- raw_scores[date==range[i]] | |
| if (vector.is.empty(rawsub)) { | |
| pooledraw[i] <- previous | |
| } | |
| else { | |
| if (length(rawsub) == 1) { | |
| previous <- rawsub | |
| pooledraw[i] <- rawsub | |
| } | |
| else { | |
| previous <- rawsub | |
| pooledraw[i] <- mean(rawsub, na.rm=T) | |
| } | |
| } | |
| } | |
| ## Carry-forward or pool residuals. | |
| previous <- 0 | |
| pooledres <- rep(NA, length(range)) | |
| for (i in 1:length(range)) { | |
| rawsub <- residuals[date==range[i]] | |
| if (vector.is.empty(rawsub)) { | |
| pooledres[i] <- previous | |
| } | |
| else { | |
| if (length(rawsub) == 1) { | |
| previous <- rawsub | |
| pooledres[i] <- rawsub | |
| } | |
| else { | |
| previous <- rawsub | |
| pooledres[i] <- mean(rawsub, na.rm=T) | |
| } | |
| } | |
| } | |
| ## Perform loess. | |
| spanparam <- days/length(range) | |
| num <- as.numeric(range) | |
| rawloess <- loess(pooledraw ~ num, span=spanparam) | |
| rawpred <- predict(rawloess, data.frame(x=num)) | |
| resloess <- loess(pooledres ~ num, span=spanparam) | |
| respred <- predict(resloess, data.frame(x=num)) | |
| ## Write the smoothed data. | |
| newdf <- data.frame(date=range, raw_smoothed=rawpred, residual_smoothed=respred) | |
| write.csv(newdf, file=sprintf("%s_scores_smoothed.csv", bank)) | |
| ## Spot checks. | |
| ## par(mfrow=c(2,1)) | |
| ## plot(range, rawpred, type='l') | |
| ## plot(range, respred, type='l') | |
| } | |
| ## Iterate banks. | |
| banks <- c('kor', 'ecb', 'mex', 'isr', 'rba', 'boj', 'boe', 'rnz', 'boc', 'swe', 'bra', 'saf', 'snb', 'nor', 'tai', 'frc', 'rbi', 'rus', 'tur') | |
| for (bank in banks) { | |
| days <- 90 # Set your bandwidth window. | |
| smoothFillForward(bank, days) | |
| } | |
| ## Or, just do for one bank. | |
| ## smoothFillForward('frc', 90) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment