Kássio Camelo Ferreira da Silva
14 de janeiro de 2019
Para extrair a métrica do Google Trends utilizando R, fazemos:
library(prophet)
library(gtrendsR)
library(pageviews)
library(reshape2)
library(dplyr)
library(tidyr)
library(tsoutliers)
library(plotly)
google.trends = gtrends(c("Ufersa"),
gprop = c("web", "news", "images", "froogle", "youtube"),
time = "all")[[1]]
google.trends = dcast(google.trends, date ~ keyword + geo, value.var = "hits")
colnames(google.trends) = c("ds", "views")
df = google.trends %>% mutate(y = views) %>% select(., ds, y)
tail(df, 3)## ds y
## 179 2018-11-01 47
## 180 2018-12-01 34
## 181 2019-01-01 17
today = paste(gsub("[[:punct:]]", "", as.character(Sys.Date())),"00", sep="")
wiki_ufersa = article_pageviews(project = "pt.wikipedia",
article = "Universidade_Federal_Rural_do_Semi-Árido",
end = today)
df = data.frame(ds = wiki_ufersa$date, y = wiki_ufersa$views)
tail(df, 3)## ds y
## 1202 2019-01-14 40
## 1203 2019-01-15 42
## 1204 2019-01-16 42
Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.
# Prophet (Facebook tool for time series)
m <- prophet(df)
future <- make_future_dataframe(m, periods = 365)
forecast <- predict(m, future)
forecast_summary <- forecast[c('ds', 'yhat', 'yhat_lower', 'yhat_upper')]
prophet_plot_components(m, forecast)trace1 <- list(
x = df$ds,
y = df$y,
mode = "markers",
name = "Observado",
type = "scatter"
)
trace2 <- list(
x = forecast_summary$ds,
y = forecast_summary$yhat,
line = list(color = 'red'),
mode = "lines",
name = "Estimado",
type = "scatter"
)
trace3 <- list(
x = forecast_summary$ds,
y = forecast_summary$yhat_lower,
fill = "tonexty",
fillcolor = '#ab63fa',
line = list(color = "blue"),
mode = "lines",
name = "Limite inferior",
type = "scatter"
) trace4 <- list(
x = forecast_summary$ds,
y = forecast_summary$yhat_upper,
fill = "tonexty",
fillcolor = '#ab63fa',
line = list(color = "blue"),
mode = "lines",
name = "Limite superior",
type = "scatter"
)
data <- list(trace1, trace2)
layout <- list(
paper_bgcolor = "rgb(255, 255, 255)",
plot_bgcolor = "rgb(243, 243, 243)",
xaxis = list(
gridcolor = "rgb(255, 255, 255)",
gridwidth = 2,
ticklen = 5,
title = "Meses",
zerolinewidth = 1
))
yaxis = list(
gridcolor = "rgb(255, 255, 255)",
gridwidth = 2,
ticklen = 5,
title = "Visualizações",
zerolinewidth = 1
)p <- plot_ly()
p <- add_trace(p, x=trace1$x, y=trace1$y, mode=trace1$mode,
name=trace1$name, type=trace1$type)
p <- add_trace(p, x=trace2$x, y=trace2$y, fill=trace2$fill,
line=trace2$line,mode=trace2$mode, name=trace2$name,
type=trace2$type)
p <- add_trace(p, x=trace3$x, y=trace3$y, fill=trace3$fill,
line=trace3$line, mode=trace3$mode, name=trace3$name,
type=trace3$type, visible = "legendonly",
fillcolor = 'rgba(168, 216, 234, 0.5)')
p <- add_trace(p, x=trace4$x, y=trace4$y, fill=trace4$fill,
line=trace4$line, mode=trace4$mode, name=trace4$name,
type=trace4$type, visible = "legendonly",
fillcolor = 'rgba(168, 216, 234, 0.5)')
p <- layout(p, paper_bgcolor=layout$paper_bgcolor,
plot_bgcolor=layout$plot_bgcolor, hovermode="compare",
title=layout$title, xaxis=layout$xaxis,
yaxis=layout$yaxis, height=400, width=900,
align="center")# Google trends
google.trends = gtrends(c("Ufersa"),
gprop = c("web", "news", "images", "froogle", "youtube"),
time = "all")[[1]]
google.trends = dcast(google.trends, date ~ keyword + geo, value.var = "hits")
colnames(google.trends) = c("ds", "views")
df = google.trends %>% mutate(y = views) %>% select(., ds, y)
df = df[-c(1:19) ,]
# Prophet (Facebook tool for time series)
m <- prophet(df)
future <- make_future_dataframe(m, periods = 12, freq="month")
forecast <- predict(m, future)
forecast_summary <- forecast[c('ds', 'yhat', 'yhat_lower', 'yhat_upper')]
prophet_plot_components(m, forecast)## [1] "Using direct authentication"
## character(0)
##
## cassiaRN igornascz JuniorOlivaci LudimillaOli
## 2 1 1 1
## MarcosBMedeiros ninicabulous tcmmossoro Thiago_Idalgo
## 1 1 1 1
## umlinguista
## 1
##
## ARI_UFERSA ccostah cecropianervosa porravitor
## 1 2 1 1
## ufersa YouTube
## 4 1
## character(0)
##
## jorjauffiana duardapss narcisoyago LuS1lva AscomUFPE
## 305 55 39 22 3