R/timeSeries.R

Defines functions getYearCountForUniverse plotTimeSeriesForUniverse plotTimeSeriesForAllScope plotTimeSeriesForEsgEnv plotTimeSeriesForProviders

getYearCountForUniverse <- function(universe, keyword="", yearLimit=1930) {
  dateU <- universe %>%
    filter(grepl(keyword, Name)) %>%
    filter(grepl("/",as.character(Inception.Date))) %>%
    mutate(date=as.character(Inception.Date))
  yearVec <- dateU$date
  for (i in 1:length(yearVec)) {
    d <- yearVec[i]
    st <- substring(d, nchar(d)-1, nchar(d))
    if (as.numeric(st) < 17) {
      yearVec[i] <- as.numeric(paste("20", st, sep=""))
    } else {
      yearVec[i] <- as.numeric(paste("19", st, sep=""))
    }
  }
  yearVec <- as.numeric(yearVec)
  yearDF <- data.frame(year=yearVec) %>%
    group_by(year) %>%
    filter(year >= yearLimit) %>%
    summarize(count=n())
  return(yearDF)
}

plotTimeSeriesForUniverse <- function(universe, keyword="",  yearLimit=1930) {
  getYearCountForUniverse(universe, keyword, yearLimit) %>%
    ggplot(aes(x=year, y=count)) +
    geom_line() +
    scale_x_continuous(breaks=seq(yearLimit, 2016, 5)) +
    colorTheme
}

plotTimeSeriesForAllScope <- function(universe, esgU, envU, keyword="",  yearLimit=1930) {
  yearDF <- getYearCountForUniverse(universe, keyword, yearLimit)
  esgDF <- getYearCountForUniverse(esgU, keyword, yearLimit)
  envDF <- getYearCountForUniverse(envU, keyword, yearLimit)
  colnames(esgDF) <- c('year', 'esg')
  colnames(envDF) <- c('year', 'env')
  fullYears <- yearDF %>%
    full_join(esgDF, by="year") %>%
    full_join(envDF, by="year")
  fullYears[is.na(fullYears)] <- 0
  fullYearsNarrow <- gather(fullYears, year)
  colnames(fullYearsNarrow) <- c("year", "type", "count")
  fullYearsNarrow %>%
    filter(year >= yearLimit) %>%
    ggplot(aes(x=year, y=count, group=type, col=type)) +
    geom_line() +
    colorTheme
}

plotTimeSeriesForEsgEnv <- function(esgU, envU, keyword="",  yearLimit=1930) {
  esgDF <- getYearCountForUniverse(esgU, keyword, yearLimit)
  envDF <- getYearCountForUniverse(envU, keyword, yearLimit)
  colnames(esgDF) <- c('year', 'esg')
  colnames(envDF) <- c('year', 'env')
  fullYears <- esgDF %>%
    full_join(envDF, by="year")
  fullYears[is.na(fullYears)] <- 0
  fullYearsNarrow <- gather(fullYears, year)
  colnames(fullYearsNarrow) <- c("year", "type", "count")
  fullYearsNarrow %>%
    filter(year >= yearLimit) %>%
    ggplot(aes(x=year, y=count, group=type, col=type)) +
    geom_line() +
    colorTheme
}

plotTimeSeriesForProviders <- function(MSCI, FTSE, SPDJ, STOXX, keyword="",  yearLimit=1930) {
  msciDF <- getYearCountForUniverse(MSCI, keyword, yearLimit)
  ftseDF <- getYearCountForUniverse(FTSE, keyword, yearLimit)
  spdjDF <- getYearCountForUniverse(SPDJ, keyword, yearLimit)
  stoxxDF <- getYearCountForUniverse(STOXX, keyword, yearLimit)
  colnames(msciDF) <- c('year', 'MSCI')
  colnames(ftseDF) <- c('year', 'FTSE')
  colnames(spdjDF) <- c('year', 'SPDJ')
  colnames(stoxxDF) <- c('year', 'STOXX')
  fullYears <- msciDF %>%
    full_join(ftseDF, by="year") %>%
    full_join(spdjDF, by="year") %>%
    full_join(stoxxDF, by="year")
  fullYears[is.na(fullYears)] <- 0
  fullYearsNarrow <- gather(fullYears, year)
  colnames(fullYearsNarrow) <- c("year", "type", "count")
  fullYearsNarrow %>%
    filter(year >= yearLimit) %>%
    ggplot(aes(x=year, y=count, group=type, col=type)) +
    geom_line() +
    colorTheme
}
yezhaoqin/morningStarAnalytics documentation built on May 4, 2019, 2:32 p.m.