Skip to content

Commit

Permalink
trend added to narrate_forecast()
Browse files Browse the repository at this point in the history
  • Loading branch information
denisabd committed May 7, 2023
1 parent 2a126d0 commit 781322f
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 109 deletions.
104 changes: 75 additions & 29 deletions R/narrate_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ narrate_forecast <- function(

# Current Year
max_actuals_date <- df %>%
dplyr::filter(!is.na(Actuals)) %>%
dplyr::filter(!is.na(base::get(actuals))) %>%
dplyr::select(dplyr::all_of(date)) %>%
as.matrix() %>%
as.Date() %>%
Expand Down Expand Up @@ -199,23 +199,27 @@ narrate_forecast <- function(

cy_forecast <- df %>%
dplyr::filter(lubridate::year(base::get(date)) == current_year) %>%
dplyr::mutate(Forecast = ifelse(is.na(Actuals), Forecast, Actuals)) %>%
dplyr::summarise(Forecast = sum(Forecast, na.rm = TRUE)) %>%
dplyr::mutate(!!forecast := ifelse(is.na(base::get(actuals)), base::get(forecast), base::get(actuals))) %>%
dplyr::summarise(!!forecast := sum(base::get(forecast), na.rm = TRUE)) %>%
as.numeric() %>%
round()
round(2)

narrative_cy <- glue::glue(template_cy, ...)
narrative_cy <- glue::glue(template_cy)

narrative <- list(narrative_cy) %>%
rlang::set_names("Current Year Actuals")

variables <- list(
template_cy = narrative_cy,
current_year = current_year,
max_actuals_date = max_actuals_date,
max_forecast_date = max_forecast_date,
frequency = frequency,
cy_forecast = cy_forecast)
list(
template_cy = narrative_cy,
current_year = current_year,
max_actuals_date = max_actuals_date,
max_forecast_date = max_forecast_date,
frequency = frequency,
cy_forecast = cy_forecast
)
) %>%
rlang::set_names("Current Year Actuals")

# Next 12 months
# In case we have at least one year of forecast values narrate FTM
Expand All @@ -224,47 +228,89 @@ narrate_forecast <- function(
ftm_forecast <- df %>%
dplyr::filter(base::get(date) > max_actuals_date,
base::get(date) <= max_actuals_date + months(12)) %>%
dplyr::summarise(Forecast = sum(Forecast, na.rm = TRUE)) %>%
dplyr::summarise(!!forecast := sum(base::get(forecast), na.rm = TRUE)) %>%
as.numeric() %>%
round()
round(2)

narrative_ftm <- glue::glue(template_ftm, ...)
narrative_ftm <- glue::glue(template_ftm)

narrative <- list(narrative_ftm) %>%
rlang::set_names(glue::glue("Overall {forecast} in the next 12 months")) %>%
rlang::set_names("12 Month Projection") %>%
append(narrative, after = 0)

variables <- append(variables, list(narrative_ftm = narrative_ftm), 1)
variables <- append(variables, list(template_ftm = template_ftm), 1)
variables <- append(variables, list(ftm_forecast = ftm_forecast))
variables_ftm <- list(
list(
narrative_ftm = narrative_ftm,
template_ftm = template_ftm,
ftm_forecast = ftm_forecast
)
) %>%
rlang::set_names("12 Month Projection")

variables <- append(variables, variables_ftm)

if (!is.null(template_ftm_change)) {
ltm_actuals <- df %>%
dplyr::filter(!is.na(Actuals),
dplyr::filter(!is.na(base::get(actuals)),
base::get(date) > max_actuals_date - months(12)) %>%
dplyr::summarise(Forecast = sum(Forecast, na.rm = TRUE)) %>%
dplyr::summarise(!!forecast := sum(base::get(actuals), na.rm = TRUE)) %>%
as.numeric() %>%
round()
round(2)

trend <- ifelse(ftm_forecast > ltm_actuals, "increase", "decrease")
ftm_change <- ftm_forecast - ltm_actuals
ftm_change_p <- round(ftm_change/ltm_actuals*100, 1)
ftm_change_p <- round(ftm_change/ltm_actuals*100, 2)

narrative_ftm_change <- glue::glue(template_ftm_change, ...)
narrative_ftm_change <- glue::glue(template_ftm_change)

narrative <- list(narrative_ftm_change) %>%
rlang::set_names(glue::glue("Overall {trend} the next 12 months")) %>%
append(narrative, after = 0)

variables <- append(variables, list(narrative_ftm_change = narrative_ftm_change), 1)
variables <- append(variables, list(template_ftm_change = template_ftm_change), 1)
variables <- append(variables,
list(trend = trend,
ltm_actuals = ltm_actuals)
)
variables_ftm_change <- list(
list(
narrative_ftm_change = narrative_ftm_change,
template_ftm_change = template_ftm_change,
trend = trend,
ltm_actuals = ltm_actuals
)
) %>%
rlang::set_names(glue::glue("Overall {trend} the next 12 months"))

variables <- append(variables, variables_ftm_change)
}
}

# Trend -------------------------------------------------------------------
trend_df <- df %>%
dplyr::mutate(!!forecast := ifelse(
!is.na(base::get(actuals)),
base::get(actuals),
base::get(forecast)
)
) %>%
dplyr::select(-dplyr::all_of(actuals))

trend_narrative <- trend_df %>%
narrator::narrate_trend(
type = "yoy",
coverage = coverage,
coverage_limit = coverage_limit,
narration_depth = narration_depth
)

variables_trend <- trend_df %>%
narrator::narrate_trend(
type = "yoy",
coverage = coverage,
coverage_limit = coverage_limit,
narration_depth = narration_depth,
return_data = TRUE
)

narrative <- append(narrative, trend_narrative)
variables <- append(variables, variables_trend)

# ChatGPT -----------------------------------------------------------------
if (use_chatgpt) {
narrative <- enhance_narrative(narrative, openai_api_key = openai_api_key)
Expand Down
100 changes: 53 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,27 +135,28 @@ narrative_enhanced <- enhance_narrative(narrative_one)
cat(narrative_enhanced)
```

The company’s Total Sales across all Regions have reached a remarkable
figure of 38790478.4. Outlying Regions by Sales are NA, which has
generated sales figures of 18079736.4, accounting for 46.6% of the total
sales. Following closely is EMEA, responsible for sales figures of
13555412.7, representing 34.9% of the total sales.

Analysing the sales figures for the NA market, we can identify two
significant products that have achieved excellent results. Food &
Beverage led the way with an outstanding 7392821 in sales, comprising
40.9% of the market share. Electronics followed closely with sales
figures of 3789132.7, accounting for an estimated 21% market share.

In the EMEA market, we also see impressive figures of sales for the Food
& Beverage sector with 5265113.2 accounting for 38.8% market share, and
Electronics achieving sales of 3182803.4, accounting for an estimated
23.5% of the market.

Combined, Food & Beverage and Electronics products have driven the
Outlying Products by Sales with notable figures of 15543469.7 and
8608962.8, which represent 40.1% and 22.2% of the market share,
respectively.
The company’s Total Sales across all Regions has reached an impressive
38790478.4, showcasing its strong market presence. Among the Outlying
Regions, North America (NA) dominates with a total sales value of
18079736.4, accounting for 46.6% of total sales. Additionally, the
Europe, Middle East, and Africa (EMEA) region follows, with a sales
value of 13555412.7, contributing to 34.9% of total sales.

Analyzing the sales data further indicates that in North America,
notable product categories by sales are Food & Beverage, generating a
substantial sales value of 7392821 and accounting for 40.9% of total
sales. Electronics, with sales value of 3789132.7, is also a significant
product category, contributing 21% to total sales. Similarly, in the
EMEA region, Food & Beverage is the top product category, responsible
for generating a sales value of 5265113.2, contributing to 38.8% of
total sales. Electronics come in second, with a sales value of
3182803.4, accounting for 23.5% of sales in the EMEA region.

Overall, when considering outlying products by sales for all regions,
Food & Beverage category remains dominant. It has registered a sales
value of 15543469.7, contributing to 40.1% of total sales. Electronics
come in second among outlying products with a sales value of 8608962.8,
representing 22.2% of total outlying sales.

### Translation

Expand All @@ -167,26 +168,30 @@ translation <- translate_narrative(narrative_enhanced, language = "Czech")
cat(translation)
```

Celkové Spojené Prodeje Společnosti napříč všemi regiony dosáhly
pozoruhodné sumy 38790478.4. Regiony nad rámec očekávání jsou Severní
Amerika (NA), která vygenerovala prodej ve výši 18079736.4, což
představuje 46,6% celkového prodeje. Těsně na druhém místě je EMEA,
zodpovědná za prodej ve výši 13555412.7, což odpovídá 34,9% celkových
prodejů.

Analýzou prodejů na trhu Severní Ameriky můžeme identifikovat dva
významné produkty, které dosáhly vynikajících výsledků. Vedením šla
kategorie Jídla a Nápojů se skvělými prodeji za 7392821, což tvoří 40.9%
tržního podílu. Elektronika následovala blízko za prodejovými čísly ve
výši 3789132.7, což odpovídá přibližně 21% tržního podílu.

Na trhu EMEA vidíme také impozantní prodejová čísla od kategorie Jídla a
Nápojů (5265113.2), což tudíž tvoří 38.8% tržního podílu, a Elektronika
s prodejem ve výši 3182803.4, což představuje 23.5% tržního podílu.

Celkově kategorie Jídla a Nápojů a Elektronika pohánějí Outlying
Produkty s významnými čísly ve výši 15543469.7 a 8608962.8, což odpovídá
40.1% a 22.2% tržního podílu.
Celkový objem prodeje společnosti přes všechny regiony dosáhl
impresivního čísla 38790478,4, což ukazuje na její silnou pozici na
trhu. Mezi vzdálenými regiony tvoří Severní Amerika (NA) dominantní
tržbu s celkovou hodnotou prodeje 18079736,4, což odpovídá 46,6%
celkového objemu prodeje. Dále následuje oblast Evropy, Středního
východu a Afriky (EMEA) s hodnotou prodeje 13555412,7, přispívající k
34,9% z celkového prodeje.

Podrobnější analýza prodejních dat ukazuje, že v Severní Americe jsou
významné kategorie produktů podle prodeje potraviny a nápoje, generující
obrovskou tržbu 7392821, což odpovídá 40,9% celkového prodeje.
Elektronika s prodejem ve výši 3789132,7 je také významnou kategorií
produktů, přispívající k 21% celkového prodeje. Obdobně v oblasti EMEA
jsou potraviny a nápoje nejvýznamnější kategorií produktů a jsou
zodpovědné za generování prodeje v hodnotě 5265113,2 což představuje
38,8% z celkového prodeje. Elektronika se umisťuje na druhé pozici s
prodejem ve výši 3182803,4, což odpovídá 23,5% prodeje v oblasti EMEA.

Celkově, pokud se budou posuzovat vzdálené produkty podle prodeje ve
všech oblastech, kategorie potravin a nápojů zůstává dominantní.
Zaregistrovala prodej v hodnotě 15543469,7, přispívající 40,1% k
celkovému prodeji. Elektronika se umisťuje na druhém místě mezi
vzdálenými produkty s prodejem ve výši 8608962,8, což odpovídá 22,2%
celkového prodeje.

### Summarization

Expand All @@ -198,12 +203,13 @@ summarization <- summarize_narrative(narrative_enhanced)
cat(summarization)
```

Total Sales of 38790478.4 have been achieved by the company across all
regions. NA and EMEA are the top regions with sales figures of
18079736.4 and 13555412.7, respectively. Food & Beverage and Electronics
are the leading products, with impressive sales figures in both regions
driving the market share. They account for 40.1% and 22.2% of the market
share respectively, combining to contribute to the overall success.
Company’s total sales reach 38790478.4. North America leads with 46.6%
of total sales, followed by EMEA with 34.9%. Notable product categories
in NA are Food & Beverage and Electronics, generating 40.9% and 21% of
sales, respectively. In EMEA, Food & Beverage and Electronics account
for 38.8% and 23.5% of sales, respectively. Food & Beverage is the
dominant category across all regions, generating 40.1% of total outlying
sales. Electronics follow with 22.2%.

# Python

Expand Down
Loading

0 comments on commit 781322f

Please sign in to comment.