Advertisement
coltonpeltier

Untitled

Jan 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. # Imports
  2. library(tidyverse)
  3. library(ggplot2)
  4.  
  5. # Read in the CSV file
  6. gdp <- read.csv('PATH_TO_CSV',header=T)
  7.  
  8. # Format the date column
  9. gdp$Date <- strptime(gdp$Date, format = "%b %d %Y")
  10.  
  11. # Format Date
  12. gdp$Date <- as.POSIXct(gdp$Date)
  13.  
  14. # Which dates to consider?
  15. threshold <- "1970-01-01"
  16.  
  17. gdpReduced <-
  18. gdp %>%
  19. filter(Date > threshold) %>%
  20. mutate(isNegative = ifelse(Value < 0, TRUE, FALSE)) %>%
  21. mutate(prior1QuarterNegative = ifelse(lead(Value < 0), TRUE, FALSE)) %>%
  22. mutate(twoNegativeInARow = ifelse(isNegative & prior1QuarterNegative, TRUE, FALSE)) %>%
  23. transmute(Date = Date,
  24. Value = Value,
  25. twoNegativeInARow = twoNegativeInARow)
  26.  
  27. # Percentage occurence
  28. occurences <-
  29. gdpReduced %>% filter(twoNegativeInARow)
  30. occurenceRate <- nrow(occurences) / nrow(gdpReduced) # 7.2% as a prior probability
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement