---
title: "ACCT 101, Session 9: Financial Statement Analysis"
author: 
  - name: Dr. Richard M. Crowley
    email: rcrowley@smu.edu.sg
    affiliation: https://rmc.link/
title-slide-attributes:
  data-background-video: "../Backgrounds/title.mp4"
  data-background-video-loop: "true"
  data-background-size: stretch
format:
  revealjs:
    theme: default
    css: quarto_white.css
    navigation-mode: vertical
    controls: true
    transition: slide
    background-transition: fade
    transition-speed: default
    width: 1536
    height: 864
    logo: ../Logos/logo2.png
    code-link: true
    code-line-numbers: false
    chalkboard:
      theme: whiteboard
    auto-stretch: false
revealjs-plugins:
  - attribution
execute:
  echo: true
  warning: false
  message: false
editor: source
---

# Front matter {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}


<!--# Note that the downlit and xml2 pacakges are required to compile -->
<!--# Installed extensions include `attribution`, `qrcode`, and `material-icons` -->

```{r helpers, echo=FALSE, warning=FALSE, message=F}
library(knitr)
library(kableExtra)

#' Pretty formatted HTML tables
#' 
#' @param df The dataframe to format as a table
#' @param names Names to use for columns if not using the df column names. Vector
#' @param highlight_cols Columns to highlight. Default is c(1) for highlighting the first row. Use c() to remove.
#' @param highlight_rows Rows to highlight. Default is c() to not highlight any rows.
#' @param color_rows Rows to highlight in color. Default is c() to not highlight any rows.
#' @param highlight_end_rows Rows to highlight. Uses a reverse indexing where 1 is the last row, 2 is second to last, etc.
#' @param full Use to span the full width of a slide. FALSE by default.
#' @param fixed_header Whether the first row should be treated as a header row. TRUE by default.
html_df <- function(df, names=NULL, highlight_cols=c(1), highlight_rows=c(), color_rows=c(), highlight_end_rows=c(), full=F, fixed_header=T) {
  if(!length(names)) {
    names=colnames(df)
  }
  kbl(df,"html", col.names = names, align = c("l",rep('c',length(cols)-1))) %>%
    kable_paper(c("striped","hover"), full_width=full, fixed_thead=F, html_font = "\"Source Sans Pro\", Helvetica, sans-serif") %>%
    {if(length(highlight_cols)) column_spec(., highlight_cols,bold=T, background = "#ffffff99") else .} %>%
    {if(length(highlight_rows)) row_spec(., highlight_rows,bold=T, background = "#ffffff99") else .} %>%
    {if(length(color_rows)) row_spec(., color_rows,bold=T, background = "#aaaaff99") else .} %>%
    {if(length(highlight_end_rows)) row_spec(., nrow(df) + 1 - highlight_end_rows,bold=T, background = "#ffffff99") else .}
}

library(tidyverse)
```

<!--# List packages here that are not being correctly highlighted -->
<!--# The usual cause is calling the library from a echo=FALSE cell -->

```{r}
#| include: false
options(downlit.attached = c("knitr", "kableExtra", "tidyverse"))
```

<!-- "reveal_plugins:..." and "chalkboard:" are handled by prep.py -->
```{r setup, include=FALSE}
library(reticulate)  # To use python in R Markdown
# Python to use is specified in the .Renviron file
```

## Learning objectives {data-background="../Backgrounds/default.jpg"}

:::: {.columns}

::: {.column width="50%"}
![](../Course_Diagrams/ACCT_101_course_map_9.png){width="450px" fig-align="center"}
:::

::: {.column width="50%"}

1. Learn about financial statement analysis
2. Calculate and interpret financial ratios

:::

::::


# Financial statement analysis {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## What matters? {data-background-image="../Backgrounds/default.jpg"}

:::: {.columns}

::: {.column width="50%"}

1. The business environment
    - Economy health
    - Other countries (particularly for multinational firms)
    - Industry demand
    - Resource scarcity or supplier concentration
    - Consumer concentration
2. Historical financials
    - Financial statements and notes
    - Competitors statements

:::

::: {.column width="50%"}

3. Historical non-financials
    - Governance
    - Risk disclosures
    - Audit report
    - Shareholders
    - Supplier relationships

:::

::::





## Predicting firm value {data-background="../Backgrounds/reference.jpg"}

1. Trend analysis (a.k.a. Horizontal analysis)
    - Compare dollar and percent changes across years
2. Common size financial statements (a.k.a. Vertical analysis)
    - Compare financials across years or firms
    - A subset of ratio analysis
3. *Ratio analysis*
4. Analytics

> *Only ratio analysis is on the final* -- we'll cover the others briefly.

# Trend analysis {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}


## What is it? {data-background="../Backgrounds/default.jpg"}

- Comparing different years or quarters of data to see the *trend* in measures.
- Examples:
    - Revenue grew by 3% this year
    - Net income grew by 4% this quarter
    - Quarterly revenue decreased 2% year over year

## Examples {data-background="../Backgrounds/default.jpg"}

- [K-Pop Looks to China to Supercharge Growth After Strong Earnings](https://www.wsj.com/business/k-pop-looks-to-china-to-supercharge-growth-after-strong-earnings-e8741bbd)

> Hybe, the agency behind boy band BTS, saw its *second-quarter revenue rise 10%* from year earlier to about $510 million, a record for the period.<cite>WSJ</cite>

- [Reddit stock soars as company posts fastest quarterly revenue growth in 3 years](https://finance.yahoo.com/news/reddit-stock-soars-as-company-posts-fastest-quarterly-revenue-growth-in-3-years-205631477.html)

> The social media's *revenue grew 78%* to $500 million, its fastest revenue growth in three years<cite>Yahoo! Finance</cite>

## How to do it {data-background="../Backgrounds/reference.jpg"}

1. Get 2 financial statements from the same company (typically the income statement)
2. Find the percentage change from the old figures to the new figures

![](../Diagrams/trend_analysis.png){width="800px" fig-align="center"}

# Common-size financial statements {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## What is it? {data-background="../Backgrounds/default.jpg"}

- Standardizing figures in a financial statement by dividing by another figure.
- Allows for comparing financial statements across companies
- Ex.:
    - Divide an income statement by revenue
        - $\frac{Gross\ profit}{Revenue}=Gross\ Margin$
        - $\frac{Net\ income}{Revenue}=Profit\ Margin$
    - Divide financial statements by total assets

## Examples {data-background="../Backgrounds/default.jpg"}

- [Writing was on the wall for loss-making Jetstar Asia, say analysts](https://www.straitstimes.com/singapore/transport/jetstar-asias-closure-not-a-surprise-say-analysts)

> The regional vice-president for Asia-Pacific at the global trade body representing 350 airlines noted that the net profit margin for Asia-Pacific carriers is expected to be 1.9 per cent in 2025 – a "very thin buffer", he added.<cite>Straits Times</cite>

- [Tesla profit margin worst in five years as price cuts, incentives weigh on bottom line](https://www.straitstimes.com/business/tesla-profit-margins-worst-in-five-years-as-price-cuts-incentives-weigh)

> Tesla recorded automotive *gross margin* excluding regulatory credits *of 14.65* per cent in the second quarter, compared with estimates of 16.29 per cent, according to 20 analysts polled by Visible Alpha.<cite>Straits Times</cite>

## How to do it  {data-background="../Backgrounds/reference.jpg"}

1. Get a financial statement
2. Divide every number by the same amount (sales, total assets, etc.) to get the percent (of sales, of assets, etc.)

![](../Diagrams/common_size.png){width="1000px" fig-align="center"}

> We can compare accross companies or years

# Balance sheet ratios {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## What is ratio analysis? {data-background="../Backgrounds/default.jpg"}

- Using various ratios of numbers from financial statements to better understand companies

All examples use the following data:

![](../Diagrams/ratios_example.png){width="1000px" fig-align="center"}

## Caveats {data-background="../Backgrounds/default.jpg"}

1. There are a few differences between the ratios in these slides and in the book.  These differences are due to simplifications I have made -- you can use these ratios on the final instead of the book's ratios without penalty.
2. Some ratios have many definitions.  If you look online, you may find other definitions for some of these ratios.  Don't use those on the final.

## Inventory turnover {data-background="../Backgrounds/default.jpg"}

$$
\frac{COGS}{\frac{1}{2}\left(Inventory_T + Inventory_{T-1}\right)}
$$

- How many times per year a company sells its inventory on hand
- A similar measure is *Inventory resident period*
    - A.k.a. Number of days' sales in inventory
    - Calculated as $\frac{365}{Inventory\ turnover}$
    - The number of days it take to sell the company's inventory

> Microsoft's 2017 inv. turnover: $\frac{34,261}{\frac{1}{2}\left(2,181 + 2,251\right)} = 15.46$

> Microsoft's 2017 inv. period: $\frac{365}{15.46}=23.6\ days$

## Accounts receivable turnover {data-background="../Backgrounds/default.jpg"}

$$
\frac{Revenue}{\frac{1}{2}\left(A/R_T + A/R_{T-1}\right)}
$$

- How many times per year a company collects its A/R on hand
- A similar measure is *Receivable collection period*
    - A.k.a. Number of days' sales in receivables
    - Calculated as $\frac{365}{Accounts\ receivable\ turnover}$
    - The number of days it take to collect the company's A/R

> Microsoft's 2017 A/R turnover: $\frac{89,950}{\frac{1}{2}\left(19,792 + 18,277\right)} = 4.73$

> Microsoft's 2017 A/R period: $\frac{365}{4.73}=77.2\ days$

## Payable turnover {data-background="../Backgrounds/default.jpg"}

$$
\frac{COGS}{\frac{1}{2}\left(A/P_T + A/P_{T-1}\right)}
$$

- How many times per year a company pays its A/P it owes
- A similar measure is *Payable outstanding period*
    - Calculated as $\frac{365}{Payable\ turnover}$
    - The number of days it take to pay the company's A/P

> Microsoft's 2017 A/P turnover: $\frac{34,261}{\frac{1}{2}\left(7,390 + 6,898\right)} = 4.80$

> Microsoft's 2017 A/P period: $\frac{365}{4.80}=76.1\ days$

## Cash conversion cycle {data-background="../Backgrounds/default.jpg"}

$$
\begin{equation*}
\frac{365}{Inventory\ turnover} + \frac{365}{A/R\ turnover} - \frac{365}{A/P\ turnover}\\
\Updownarrow\\
\scriptstyle Receivable\ collection\ period + Inventory\ resident\ period - Payable\ outstanding\ period
\end{equation*}
$$

- Measures how long it takes from paying payables to receiving cash for a sale
- Can calculate from turnover ratios or periods

![](../Diagrams/CCC.png){width=1000px}

> Microsoft's 2017 cash conversion cycle: $23.6 + 77.2 - 76.1 = 24.7\ days$

## Asset turnover {data-background="../Backgrounds/default.jpg"}

$$
\text{Asset turnover} = \frac{Net~revenue}{\frac{1}{2}\left(Assets_T + Assets_{T-1}\right)}
$$

- Measures sales volume in relation to asset base

> Microsoft's 2017 asset turnover: $\frac{89,950}{\frac{1}{2}\left(241,086+193,468)\right)}=41.4\%$

## Current ratio {data-background="../Backgrounds/default.jpg"}

$$
\frac{Current\ assets}{Current\ liabilities}
$$

- Measures a company's ability to pay current liabilities
- This should usually be $>2$
<br><br>

> Microsoft's 2017 current ratio: $\frac{159,851}{64,527}=2.48$


## Quick ratio {data-background="../Backgrounds/default.jpg"}

$$
\frac{Cash + Short\ term\ investments + A/R}{Current\ liabilities}
$$

- A.k.a. acid-test ratio 
- Measures a company's ability to pay current liabilities
    - Only factors in liquid current assets
- This should be $>1$
<br><br>

> Microsoft's 2017 quick ratio: $\frac{7,663 + 125,318 + 19,792}{64,527}=2.37$

## Debt to equity ratio {data-background="../Backgrounds/default.jpg"}

$$
\frac{Total\ liabilities}{Total\ equity}
$$

- Measures a company's leverage
    - Leverage = how much the company is financed by debt
- Higher = more leverage = more debt financing

> Microsoft's 2017 debt to equity ratio: $\frac{168,692}{72394}=2.33$


## Times-interest-earned ratio {data-background="../Backgrounds/default.jpg"}

$$
\frac{Income\ from\ operations}{Interest\ expense}
$$

- Measures a company's ability to cover interest payments
- Higher is better, $<1$ should cause some worry

> Microsoft's 2017 times-interest-earned ratio: $\frac{22,326}{2,222}=10.05$

## Practice {data-background="../Backgrounds/question1.jpg"}

> Calculate the following ratios for Microsoft in 2016

- Payable outstanding period
- Quick ratio
- Debt to equity ratio
- Times-interest-earned ratio

> Extra info: Microsoft's A/P in 2015 was \$6,591M

<p style="margin-bottom:8em"></p>

## Solution {data-background="../Backgrounds/answer.jpg" class="default exclude" visibility="hidden"}

- Payable payment period ($76.1\ days$ in 2017)
    - $365 / \frac{32,780}{\frac{1}{2}\left(6,898 + 6,591\right)}=75.1\ days$
- Quick ratio ($2.37$ in 2017)
    - $\frac{6,510 + 106,730 + 18,277}{59,357}=2.22$
- Debt to equity ratio ($2.33$ in 2017)
    - $\frac{121,471}{71997}=1.69$
- Times-interest-earned ratio ($10.05$ in 2017)
    - $\frac{20,182}{1,243}=16.24$

# Income statement ratios {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## Profit Margin {data-background="../Backgrounds/default.jpg"}

$$
\frac{Profit}{Revenue}
$$

- Gross profit margin tells you about the company's selling margins
- Net profit margin tells you about its overall profitability

> Microsoft's 2017 gross profit margin: $\frac{55,689}{89,950}=61.9\%$

> Microsoft's 2017 net profit margin: $\frac{21,204}{89,950}=23.6\%$

## Return on assets (ROA) {data-background="../Backgrounds/default.jpg"}

$$
\frac{Net\ income}{\frac{1}{2}\left(Assets_T + Assets_{T-1}\right)}
$$

- Measures overall profitability based on the company's size
- Very common measure in practice
- Higher is better
<br><br>

> Microsoft's 2017 ROA: $\frac{21,204}{\frac{1}{2}\left(241,086+193,468\right)}=9.76\%$

## Return on equity (ROE) {data-background="../Backgrounds/default.jpg"}

$$
\frac{Net\ income}{\frac{1}{2}\left(Equity_T + Equity_{T-1}\right)}
$$

- Measures overall profitability based on the company's size
    - Stockholder focussed
- Very common measure in practice
- Higher is better
<br><br>

> Microsoft's 2017 ROE: $\frac{21,204}{\frac{1}{2}\left(72,394+71,997)\right)}=29.4\%$

## Practice {data-background="../Backgrounds/question1.jpg"}

> Calculate the following ratios for Microsoft in 2016

- Net profit margin
- Return on assets (ROA)
- Return on equity (ROE)

> Extra info: Microsoft's 2015 total assets was \$176,223M and Microsoft's 2015 total equity was \$80,083M

<p style="margin-bottom:8em"></p>

## Solution {data-background="../Backgrounds/answer.jpg" class="default exclude" visibility="hidden"}

- Net profit margin ($23.6\%$ in 2017)
    - $\frac{16,798}{85,320}=19.7\%$
- ROA ($9.76\%$ in 2017)
    - $\frac{16,798}{\frac{1}{2}\left(193,468+176,223\right)}=9.09\%$
- ROE ($29.4\%$ in 2017)
    - $\frac{16,798}{\frac{1}{2}\left(71,997+80,083\right)}=22.1\%$


# Equity ratios {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## Earnings per share (EPS) {data-background="../Backgrounds/default.jpg"}

$$
\frac{Net\ income - Dividends\ on\ pref.\ shares}{\frac{1}{2}\left(\#Shares_T+\#Shares_{T-1}\right)}
$$

- Measures the amount of profit tied to each share of stock
- Very common measure in practice
- Assume shares in year $T$ and $T-1$ are the same if not stated
- <span style="color:red">Very easily manipulated</span>
<br><br>

> Microsoft's 2017 EPS: $\frac{21,204 - 0}{\frac{1}{2}\left(7,708+7,808)\right)}=\$2.73/share$

## Price/earnings ratio (P/E ratio) {data-background="../Backgrounds/default.jpg"}

$$
\frac{Stock\ price}{EPS}
$$

- A measure of if a stock is overpriced
- 6 to 8 is common, 20+ is common for tech firms
    - Higher = overpriced
    - Lower = underpriced
- Very common measure in practice
- <span style="color:red">Very easily manipulated</span>, since EPS is easily manipulated
<br><br>

> Microsoft's 2017 P/E ratio: $\frac{68.93}{2.73}=25.2$

## Practice {data-background="../Backgrounds/question1.jpg"}

> Calculate the following ratios for Microsoft in 2016

- EPS
- P/E Ratio

> Extra info: Microsoft's 2015 outstanding shares was 8,027M

<p style="margin-bottom:10em"></p>

## Solution {data-background="../Backgrounds/answer.jpg" class="default exclude" visibility="hidden"}

- EPS ($\$2.73/share$ in 2017)
    - $\frac{16,798 - 0}{\frac{1}{2}\left(7,808+8,027\right)} = \$2.12/share$
- P/E Ratio ($25.2$ in 2017)
    - $\frac{51.17}{2.12} = 24.1$

# Equations {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## Balance sheet ratio equations {data-background="../Backgrounds/reference.jpg"}

$$
{\scriptsize
\begin{align*}
\text{Inventory turnover} &= \frac{COGS}{\frac{1}{2}\left(Inventory_T + Inventory_{T-1}\right)} \\
\text{A/R turnover} &= \frac{Revenue}{\frac{1}{2}\left(A/R_T + A/R_{T-1}\right)} \\
\text{A/P turnover} &= \frac{COGS}{\frac{1}{2}\left(A/P_T + A/P_{T-1}\right)}\\
\text{Cash conversion cyle} &= \frac{365}{Inv.\ turnover} + \frac{365}{A/R\ turnover} - \frac{365}{A/P\ turnover}\\
\text{Asset turnover} &= \frac{Net~revenue}{\frac{1}{2}\left(Assets_T + Assets_{T-1}\right)} \\
\text{Current ratio} &= Current\ assets / Current\ liabilities \\
\text{Quick ratio} &= \frac{Cash + Short\ term\ investments + A/R}{Current\ liabilities} \\
\text{Debt to equity ratio} &= Total\ liabilities / Total\ equity \\
\text{Times-interest-earned} &= \frac{Income\ from\ operations}{Interest\ expense}
\end{align*}}
$$

## Income statement ratio equations {data-background="../Backgrounds/reference.jpg"}

$$
\scriptsize
\begin{align*}
\text{Gross (profit) margin} &= \frac{Gross\ profit}{Revenue} \\
\text{Net profit margin} &= \frac{Net\ income}{Revenue} \\
\text{Return on assets (ROA)} &= \frac{Net\ income}{\frac{1}{2}\left(Assets_T + Assets_{T-1}\right)} \\
\text{Return on equity (ROE)} &= \frac{Net\ income}{\frac{1}{2}\left(Equity_T + Equity_{T-1}\right)}
\end{align*} \\
$$

## Equity ratio equations {data-background="../Backgrounds/reference.jpg"}

$$
\scriptsize
\begin{align*}
\text{Earnings per share (EPS)} &= \frac{Net\ income - Dividends\ on\ pref.\ shares}{\frac{1}{2}\left(\#Shares_T+\#Shares_{T-1}\right)} \\
\text{Price/earnings ratio (P/E)} &= \frac{Stock\ price}{EPS}
\end{align*}
$$

# Analytics {data-background="../Backgrounds/math_idea.jpg" class="header"}

## Background {data-background-image="../Backgrounds/default.jpg"}

> This is a quick preview of a module called "Forecasting and Forensic Analytics," part of the Analytics and Forensics majors in SOA

- You don't need to know this for this class, but the techniques covered here are becoming more and more important

## Revenue prediction {data-background-image="../Backgrounds/case_digital.jpg"}

> Predicting ROA for tech companies using prior year data

```{r, echo=FALSE}
load("../Data/fit.rda")
```
```{r, echo=TRUE}
summary(fit)
```

## Code: Revenue prediction {data-background="../Backgrounds/code.jpg"}

```{r, eval=FALSE}
library(readr)
library(dplyr)
df <-read.csv("/media/Data/Data/Compustat/Compustat_199301-201703.csv")
df <- data.frame(df)
df_tech <- subset(df,gsector == 45 & at > 10000,
                  select=c("gvkey","datadate","at","ni","lt","revt","gind"))
df_tech <- arrange(df_tech, gvkey, datadate)
df_tech$ROA <- df_tech$ni / df_tech$at
df_tech$Revenue <- df_tech$revt / df_tech$at
df_tech$Debt <- df_tech$lt / df_tech$at
x <- c("ROA", "Revenue","Debt") # Columns to lag
df_tech <- df_tech %>%
  group_by(gvkey) %>%
  mutate_at(.cols=x, .funs=funs(lag = dplyr::lag(., n=1, default=NA)))
is.na(df_tech) <- sapply(df_tech, is.infinite)
fit <- lm(ROA ~ ROA_lag + Revenue_lag + Debt_lag + factor(gind), data=df_tech)
save(fit, file = "../Data/fit.rda")
summary(fit)
```

## Revenue prediction for Microsoft {data-background-image="../Backgrounds/case_digital.jpg"}

> Predict out Microsoft's 2018 ROA

```{r, echo=FALSE, warning = FALSE, plotly=TRUE}
load("../Data/df_ms.rda")
suppressPackageStartupMessages(library(plotly))
m <- list(l = 60,  r = 50,  b = 60,  t = 100,  pad = 4)
plot_ly(df_ms,x=~year,y=~ROA, name='Actual ROA', type='scatter', mode='lines', width = 800, height = 500) %>%
  add_trace(y=~ROA_predicted, name='Predicted ROA') %>%
  layout(autosize = F, margin = m,
         xaxis=list(title="Year"), yaxis=list(title="ROA"))
```

## Code: Revenue prediction for Microsoft {data-background-image="../Backgrounds/code.jpg"}

```{r, eval=FALSE}
df_ms <- subset(df,gvkey==12141, select=c("gvkey","datadate","at", "ni","lt",
                                          "revt","gind"))
df_ms2 <- data.frame(gvkey=12141, datadate=20170630, at=241086,ni=21204,
                     lt=168692,revt=89950,gind=451030)
df_ms3 <- data.frame(gvkey=12141, datadate=20180630, at=NA,ni=NA,
                     lt=NA,revt=NA,gind=451030)
df_ms <- rbind(df_ms, df_ms2, df_ms3)
df_ms$ROA <- df_ms$ni / df_ms$at
df_ms$Revenue <- df_ms$revt / df_ms$at
df_ms$Debt <- df_ms$lt / df_ms$at
x <- c("ROA", "Revenue","Debt") # Columns to lag
df_ms <- df_ms %>%
  group_by(gvkey) %>%
  mutate_at(.cols=x, .funs=funs(lag = dplyr::lag(., n=1, default=NA)))
df_ms$ROA_predicted <- predict(fit, df_ms)
df_ms$year = floor(df_ms$datadate/10000)
save(df_ms, file="Data/df_ms.rda")
```
```{r, eval=FALSE}
suppressPackageStartupMessages(library(plotly))
m <- list(l = 60,  r = 50,  b = 60,  t = 100,  pad = 4)
plot_ly(df_ms,x=~year,y=~ROA, name='Actual ROA', type='scatter',
        mode='lines', width = 800, height = 500) %>%
  add_trace(y=~ROA_predicted, name='Predicted ROA') %>%
  layout(autosize = F, margin = m,
         xaxis=list(title="Year"), yaxis=list(title="ROA"))
```

## Fraud detection {data-background-image="../Backgrounds/case_digital.jpg"}

:::: {.columns}

::: {.column width="50%"}

- Using 3 components:
    1. **T**opic -- what companies say in annual reports
    2. **S**tyle -- writing style used in annual reports
    3. **F**inancials -- financial ratios
    
<img src="../Figures/AAER_classification.png" width="400px">

- Brown, Crowley, & Elliott (2020, Journal of Accounting Research)

:::

::: {.column width="50%"}

![](../Figures/Detection_by_year.png){width="440px" fig-align="center"}

:::

::::


## Text classification of what we cover in ACCT 101 {data-background-image="../Backgrounds/neural-net.jpg"}

- This shows the output of a simple machine learning model trained on individual slides from our course
- Its goal is to summarize the content by grouping related words together

```{r, echo=FALSE, eval=FALSE}
library(readr)
library(tm)
bd <- read_csv("../Data/text.txt", col_names=F)
bd <- as.character(unlist(bd[, 1]))
stop_words <- stopwords("SMART")
# quarto keywords to drop
additional_drops <- c('width', 'column', 'figaligncenter', 'columns', 'px', 'stylemargin','lightbox', 'diagramsje', 'pngwidthpx', 'stylemarginpx', 'diagramsacct', 'diagramsinv', 'periodicpngwidthpx', 'attribution', 'stylefontsizeematarted', 'cogsspan', 'span', 'div', 'kabbleextra', 'knitr')
# pre-processing:
bd <- gsub("'", "", bd)  # remove apostrophes
bd <- gsub("[[:punct:]]", " ", bd)  # replace punctuation with space
bd <- gsub("[[:cntrl:]]", " ", bd)  # replace control characters with space
bd <- gsub('[[:digit:]]+', '', bd) # remove numbers
bd <- gsub("[[:space:]]", " ", bd) # replace whitespace with spaces
bd <- gsub("^[[:space:]]+", "", bd) # remove whitespace at beginning of documents
bd <- gsub("[[:space:]]+$", "", bd) # remove whitespace at end of documents


bd <- tolower(bd)  # force to lowercase

# tokenize on space and output as a list:
doc.list <- strsplit(bd, "[[:space:]]+")

# compute the table of terms:
term.table <- table(unlist(doc.list))
term.table <- sort(term.table, decreasing = TRUE)

# remove terms that are stop words or occur fewer than 2 times:
del <- names(term.table) %in% stop_words | names(term.table) %in% additional_drops | term.table < 3
term.table <- term.table[!del]
vocab <- names(term.table)

# now put the documents into the format required by the lda package:
get.terms <- function(x) {
  index <- match(x, vocab)
  index <- index[!is.na(index)]
  rbind(as.integer(index - 1), as.integer(rep(1, length(index))))
}
documents <- lapply(doc.list, get.terms)

D <- length(documents)  # number of documents (516)
W <- length(vocab)  # number of terms in the vocab (2675)
doc.length <- sapply(documents, function(x) sum(x[2, ]))  # number of tokens per document
N <- sum(doc.length)  # total number of tokens in the data (17266)
term.frequency <- as.integer(term.table)

K <- 10
G <- 5000
alpha <- 0.01
eta <- 0.02

library(lda)
set.seed(357)
t1 <- Sys.time()
fit <- lda.collapsed.gibbs.sampler(documents = documents, K = K, vocab = vocab, 
                                   num.iterations = G, alpha = alpha, 
                                   eta = eta, initial = NULL, burnin = 0,
                                   compute.log.likelihood = TRUE)
t2 <- Sys.time()
t2 - t1  # about 14.9s on desktop

theta <- t(apply(fit$document_sums + alpha, 2, function(x) x/sum(x)))
phi <- t(apply(t(fit$topics) + eta, 2, function(x) x/sum(x)))

All <- list(phi = phi,
            theta = theta,
            doc.length = doc.length,
            vocab = vocab,
            term.frequency = term.frequency)

library(LDAvis)

json <- createJSON(phi = All$phi, 
                   theta = All$theta, 
                   doc.length = All$doc.length, 
                   vocab = All$vocab, 
                   term.frequency = All$term.frequency)
library(servr)
serVis(json, out.dir = 'vis2', open.browser = TRUE)
```
[![](../Figures/LDA_2024.png){width="800px" fig-align="center"}](https://rmc.link/Slides/acct101v7/Session_9/vis2/)


# End matter {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## Wrap up {data-background="../Backgrounds/ending.jpg"}

- For next week
    1) Do Quiz 2 on eLearn!
    2) Recap the reading for this week
    3) Read the pages for next week
        - Cash flows (Chapter 11)
    4) Practice on eLearn
        - Practice on Financial ratios
        - Automatic feedback provided
- Survey on the class session at [rmc.link/101survey9](https://docs.google.com/forms/d/e/1FAIpQLSdioR6O9bjNSNugtTrKBLH7mhkhWhFZnu9G89ScBp8Z3hQ2FA/viewform?usp=sf_link)

## Packages used for these slides {data-background="../Backgrounds/code.jpg"}

- `{kableExtra}`
- `{knitr}`
- `{revealjs}`




## References {data-background="../Backgrounds/books.jpg" .stack .never .small visibility="hidden"}

## Custom code {data-background="../Backgrounds/code.jpg" .stack .never .small visibility="hidden"}


## NOTES {class="never" visibility="hidden"}


- Print out the ratios in advance -- too difficult to wing
