Skip to content

Instantly share code, notes, and snippets.

View sillasgonzaga's full-sized avatar

Sillas Teixeira Gonzaga sillasgonzaga

View GitHub Profile
cdb <- function(capital, data_aplicacao, data_resgate, di, pct_cdi){
library(dplyr)
library(bizdays)
dias_corridos <- as.numeric(difftime(data_resgate, data_aplicacao, units = "days"))
# computar biz days entre datas
bizdays::create.calendar("Brazil/ANBIMA",
bizdays::holidaysANBIMA,
weekdays=c("saturday", "sunday"))
library(BatchGetSymbols)
library(tidyverse)
library(varngc)
x <- GetSP500Stocks()
stocks <- c("AAPL", "GOOG", "MSFT", "AMZN", "ADBE", "EBAY", "FB", "HPQ")
st <- as.Date("2010-01-01")
end <- as.Date("2018-01-01")
@sillasgonzaga
sillasgonzaga / plot_rf_tree.R
Last active November 6, 2020 18:32
Plot a tree from a Random Forest Model
plot_rf_tree <- function(final_model, tree_num, shorten_label = TRUE) {
# source: https://shiring.github.io/machine_learning/2017/03/16/rf_plot_ggraph
# get tree by index
tree <- randomForest::getTree(final_model,
k = tree_num,
labelVar = TRUE) %>%
tibble::rownames_to_column() %>%
# make leaf split points to NA, so the 0s won't get plotted
# Criando um Correlation Plot
def visualize_correlation_matrix(data, hurdle = 0.0):
# fonte: data science academy
R = np.corrcoef(data, rowvar = 0)
R[np.where(np.abs(R) < hurdle)] = 0.0
heatmap = plt.pcolor(R, cmap = mpl.cm.coolwarm, alpha = 0.8)
heatmap.axes.set_frame_on(False)
heatmap.axes.set_yticks(np.arange(R.shape[0]) + 0.5, minor = False)
heatmap.axes.set_xticks(np.arange(R.shape[1]) + 0.5, minor = False)
heatmap.axes.set_xticklabels(variables, minor = False)
@sillasgonzaga
sillasgonzaga / EmotionOfStrangerThings.R
Created November 8, 2017 01:46
Stranger Things Analysis
#Code for "A Statistical Curiosity Voyage Through the Emotion of Stranger Things"
#Article and code written by Jordan Dworkin (https://medium.com/@jordandworkin/)
###############################################################################
####### Pull script data (would need to be edited based on data source) #######
###############################################################################
library(rvest)
library(tibble)
@sillasgonzaga
sillasgonzaga / exemplo_curso_st.R
Created October 28, 2017 18:50
Exemplo de agrupar forecasts
library(tidyverse)
library(purrr)
library(forecast)
library(lubridate)
vetor_uf <- c("RJ", "SP", "MG", "ES")
vetor_data <- seq.Date(as.Date("2010-01-01"), by = "month",
length.out = 60)
grafico_tfidf <- function(data, n_grams = 1, remover_nomes_proprios,
agregar_por_noticia, remover_stop_words = TRUE){
data <- data %>%
unnest_tokens(palavra, corpo_noticia, token = "ngrams", n = n_grams, to_lower = FALSE)
if (agregar_por_noticia){
# remover as duplicatas dentre de uma mesma noticia
data %<>% distinct(url, palavra, .keep_all = TRUE)
}
# source: https://gist.github.com/corynissen/5389426
getLongURL.curl <- function(shorturl){
# uses curl statement to get expanded url from t.co links (or any link)
# loop through until there's no location attribute... that's the long link.
newurl <- shorturl
url <- ""
while(url != newurl){
data <- system(paste0("curl -I ", newurl), intern=T)
if(sum(grepl("location: ", tolower(data))) == 0){
library(stringr)
library(magrittr)
library(lubridate)
library(glue)
library(dplyr)
library(readr)
# exemplo:
extrair_ans_UF <- function(uf, limpar_pasta_temporaria = TRUE) {
# projeto analise de sentimento
library(tidyr)
library(dplyr)
library(magrittr)
library(stringr)
library(lubridate)
library(ggplot2)
library(tm)
library(SnowballC)
library(wordcloud)