Skip to content

Instantly share code, notes, and snippets.

View jroberayalas's full-sized avatar

Jose Roberto Ayala Solares jroberayalas

View GitHub Profile
Model RMSE MAE MAPE
Without exogenous inputs 1050.4 857.3 14.2%
With exogenous inputs 875.7 656.2 11.5%
results <- vector(mode = 'numeric', length = nrow(prophetGrid))
# Search best parameters
for (i in seq_len(nrow(prophetGrid))) {
# Select the parameters to try
parameters <- prophetGrid[i, ]
if (parameters$growth == 'logistic') {
train$cap <- parameters$capacity
}
# Search grid
prophetGrid <- expand.grid(changepoint_prior_scale = c(0.05, 0.5),
seasonality_prior_scale = c(1, 10),
holidays_prior_scale = c(1, 10),
regressor_prior_scale = c(1, 10),
capacity = c(6000, 7000, 8000),
growth = 'logistic')
# Libraries
library(tidyverse)
library(lubridate)
library(prophet)
library(forecast)
# Read data
bikes <- read_csv('bikes.csv') %>%
mutate(datetime = date(datetime))
# Load packages
library(rvest)
library(stringr)
library(dplyr)
library(lubridate)
library(readr)
# Read web page
webpage <- read_html("https://www.nytimes.com/interactive/2017/06/23/opinion/trumps-lies.html")
library(dplyr)
records <- vector("list", length = length(results))
for (i in seq_along(results)) {
date <- str_c(results[i] %>% html_nodes("strong") %>% html_text(trim = TRUE), ", 2017")
lie <- str_sub(xml_contents(results[i])[2] %>% html_text(trim = TRUE), 2, -2)
explanation <- str_sub(results[i] %>% html_nodes(".short-truth") %>% html_text(trim = TRUE), 2, -2)
url <- results[i] %>% html_nodes("a") %>% html_attr("href")
records[[i]] <- data_frame(date = date, lie = lie, explanation = explanation, url = url)
}
df <- read_csv("trump_lies.csv")
#> Parsed with column specification:
#> cols(
#> date = col_date(format = ""),
#> lie = col_character(),
#> explanation = col_character(),
#> url = col_character()
#> )
url <- first_result %>% html_node("a") %>% html_attr("href")
url
#> [1] "https://www.buzzfeed.com/andrewkaczynski/in-2002-donald-trump-said-he-supported-invading-iraq-on-the"
write_csv(df, "trump_lies.csv")
library(lubridate)
df$date <- mdy(df$date)
glimpse(df)
#> Observations: 116
#> Variables: 4
#> $ date <date> 2017-01-21, 2017-01-21, 2017-01-23, 2017-01-25, 2017-01-25, 2017-01-...
#> $ lie <chr> "I wasn't a fan of Iraq. I didn't want to go into Iraq.", "A reporter...
#> $ explanation <chr> "He was for an invasion before he was against it.", "Trump was on the...
#> $ url <chr> "https://www.buzzfeed.com/andrewkaczynski/in-2002-donald-trump-said-h...