Fundamental Financial Ratios in R

Computing a series of financial ratios colleted from Yahoo Finance and analysing the results

library(knitr)
library(kableExtra)
library(tidyquant)
library(directlabels)
library(tibble)
library(tidyr)
library(quantmod)

I developed a model to collect financial information from Yahoo Finance, I wanted to use this model to understand a little better how to analyse comanies, what ratios are important and to see how companies performances differ from each other.

# A function to collect fundamental data from Yahoo finance (Income Statement, Balance Sheet and Cash Flow statements, just skip past this part.)

getFin <- function(stock){
  if ("rvest" %in% installed.packages()) {
    library(rvest)
    }else{
      install.packages("rvest")
      library(rvest)
      }
  for (i in 1:length(stock)) {
    tryCatch(
      {
        # Collect the Income Statement data
        link <- "https://finance.yahoo.com/quote/"
        link <- paste0(link, stock[i], "/financials?p=", stock[i])
        wahis.session <- html_session(link)
        p <- wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        IncomeStatement <- p[[1]]
        colnames(IncomeStatement) <- paste(IncomeStatement[1,])
        IncomeStatement <- IncomeStatement[-c(1,5,12,20,25),]
        names_row <- paste(IncomeStatement[,1])
        IncomeStatement <- IncomeStatement[,-1]
        IncomeStatement <- apply(IncomeStatement, 2, function(x){gsub(",","",x)})
        IncomeStatement <- as.data.frame(apply(IncomeStatement, 2, as.numeric))
        rownames(IncomeStatement) <- paste(names_row)
        temp1 <- IncomeStatement
        
        # Collect the Balance Sheet data
        link <- "https://finance.yahoo.com/quote/"
        link <- paste0(link, stock[i],"/balance-sheet?p=", stock[i])
        wahis.session <- html_session(link)
        p <- wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        BalanceSheet <- p[[1]]
        colnames(BalanceSheet) <- BalanceSheet[1,]
        BalanceSheet <- BalanceSheet[-c(1,2,17,28),]
        names_row <- BalanceSheet[,1]
        BalanceSheet <- BalanceSheet[,-1]
        BalanceSheet <- apply(BalanceSheet, 2, function(x){gsub(",","",x)})
        BalanceSheet <- as.data.frame(apply(BalanceSheet, 2, as.numeric))
        rownames(BalanceSheet) <- paste(names_row)
        temp2 <- BalanceSheet
        
        # Collect the Cash Flow data
        link <- "https://finance.yahoo.com/quote/"
        link <- paste0(link, stock[i], "/cash-flow?p=", stock[i])
        wahis.session <- html_session(link)
        p <- wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        CashFlow <- p[[1]]
        colnames(CashFlow) <- CashFlow[1,]
        CashFlow <- CashFlow[-c(1,3,11,16),]
        names_row <- CashFlow[,1]
        CashFlow <- CashFlow[,-1]
        CashFlow <- apply(CashFlow, 2, function(x){gsub(",","",x)})
        CashFlow <- as.data.frame(apply(CashFlow, 2, as.numeric))
        rownames(CashFlow) <- paste(names_row)
        temp3 <- CashFlow
        
        assign(paste0(stock[i],'.f'),value = list(IncomeStatement = temp1, BalanceSheet = temp2, CashFlow = temp3), envir = parent.frame())
        },
      error = function(cond){
        message(stock[i], "Give error ",cond)
        }
      )
  }
}

I firstly scrape the Yahoo Finance website and collect fundamental data for the Income Statement, Balance Sheet, and Cash Flows. I will analyse some well known comparable tech stocks Apple, Amazon, Google, Microsoft and Baidu and see how they all compare.

symbols <- c("AAPL", "AMZN", "GOOG", "MSFT", "BIDU")
getFin(symbols)
symbols.f <- sapply(symbols, function(x) { paste0(x, ".f") })
tickers <- list2env(mget(symbols.f))

IS <- lapply(tickers, "[[", "IncomeStatement")
BS <- lapply(tickers, "[[", "BalanceSheet")
CF <- lapply(tickers, "[[", "CashFlow")

IS <- as.data.frame(IS)
BS <- as.data.frame(BS)
CF <- as.data.frame(CF)

How the Income Statement looks:

Table 1: Income Statement
BIDU.f.12.31.2018 BIDU.f.12.31.2017 BIDU.f.12.31.2016 BIDU.f.12.31.2015 MSFT.f.6.30.2019 MSFT.f.6.30.2018 MSFT.f.6.30.2017 MSFT.f.6.30.2016 GOOG.f.12.31.2018 GOOG.f.12.31.2017 GOOG.f.12.31.2016 GOOG.f.12.31.2015 AMZN.f.12.31.2018 AMZN.f.12.31.2017 AMZN.f.12.31.2016 AMZN.f.12.31.2015 AAPL.f.9.29.2018 AAPL.f.9.30.2017 AAPL.f.9.24.2016 AAPL.f.9.26.2015
Total Revenue 103877000 86709000 70549000 66382000 125843000 110360000 96571000 91154000 136819000 110855000 90272000 74989000 232887000 177866000 135987000 107006000 265595000 229234000 215639000 233715000
Cost of Revenue 51744000 38294000 31358000 22814000 42910000 38353000 34261000 32780000 59549000 45583000 35138000 28164000 139156000 111934000 88265000 71651000 163756000 141048000 131376000 140089000
Gross Profit 52133000 48415000 39191000 43568000 82933000 72007000 62310000 58374000 77270000 65272000 55134000 46825000 93731000 65932000 47722000 35355000 101839000 88186000 84263000 93626000
Research Development 15772000 12928000 10151000 10176000 16876000 14726000 13037000 11988000 21419000 16625000 13948000 12282000 28837000 22620000 16085000 12540000 14236000 11581000 10045000 8067000
Selling General and Administrative 19231000 13128000 15071000 17076000 23098000 22223000 19942000 19198000 24459000 19765000 17470000 15183000 52177000 38992000 27284000 20411000 16705000 15261000 14194000 14329000
Non Recurring NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Others NA 4768000 3920000 4644000 NA NA NA NA NA NA NA NA 296000 214000 167000 171000 NA NA NA NA
Total Operating Expenses 86747000 69118000 60500000 54710000 82884000 75302000 67240000 63966000 105427000 81973000 66556000 55629000 220466000 173760000 131801000 104773000 194697000 167890000 155615000 162485000
Operating Income or Loss 17130000 17591000 10049000 11672000 42959000 35058000 29331000 27188000 31392000 28882000 23716000 19360000 12421000 4106000 4186000 2233000 70898000 61344000 60024000 71230000
Total Other Income/Expenses Net 10195000 3692000 4460000 26235000 729000 1416000 570000 -1549000 3521000 -1689000 434000 291000 -1151000 -304000 -390000 -687000 2005000 2745000 1348000 1285000
Earnings Before Interest and Taxes 17130000 17591000 10049000 11672000 42959000 35058000 29331000 27188000 31392000 28882000 23716000 19360000 12421000 4106000 4186000 2233000 70898000 61344000 60024000 71230000
Interest Expense -3483000 -3515000 -1158000 -1041000 -2686000 -2733000 -2222000 -1243000 -114000 -109000 -124000 -104000 -1417000 -848000 -484000 -459000 -3240000 -2323000 -1456000 -733000
Income Before Tax 27325000 21283000 14509000 37907000 43688000 36474000 29901000 25639000 34913000 27193000 24150000 19651000 11270000 3802000 3796000 1546000 72903000 64089000 61372000 72515000
Income Tax Expense 4743000 2995000 2913000 5475000 4448000 19903000 4412000 5100000 4177000 14531000 4672000 3303000 1197000 769000 1425000 950000 13372000 15738000 15685000 19121000
Minority Interest 12855000 15026000 5469000 3960053 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Net Income From Continuing Ops 22582000 18288000 11596000 32432000 39240000 16571000 25489000 20539000 30736000 12662000 19478000 16348000 10073000 3033000 2371000 596000 59531000 48351000 45687000 53394000
Discontinued Operations NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Extraordinary Items NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Effect Of Accounting Changes NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Other Items NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Net Income 27573000 18301000 11632000 33664000 39240000 16571000 25489000 20539000 30736000 12662000 19478000 16348000 10073000 3033000 2371000 596000 59531000 48351000 45687000 53394000
Preferred Stock And Other Adjustments NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Net Income Applicable To Common Shares 27443000 18318000 11075000 33335000 39240000 16571000 25489000 20539000 30736000 12662000 19478000 16348000 10073000 3033000 2371000 596000 59531000 48351000 45687000 53394000

How the Balance Sheet looks:

Table 2: Balance Sheet
BIDU.f.12.31.2018 BIDU.f.12.31.2017 BIDU.f.12.31.2016 BIDU.f.12.31.2015 MSFT.f.6.30.2019 MSFT.f.6.30.2018 MSFT.f.6.30.2017 MSFT.f.6.30.2016 GOOG.f.12.31.2018 GOOG.f.12.31.2017 GOOG.f.12.31.2016 GOOG.f.12.31.2015 AMZN.f.12.31.2018 AMZN.f.12.31.2017 AMZN.f.12.31.2016 AMZN.f.12.31.2015 AAPL.f.9.29.2018 AAPL.f.9.30.2017 AAPL.f.9.24.2016 AAPL.f.9.26.2015
Cash And Cash Equivalents 27638000 11084000 10898000 9959932 11356000 11946000 7663000 6510000 16701000 10715000 12918000 15409000 31750000 20522000 19334000 15890000 25913000 20289000 20484000 21120000
Short Term Investments 111873000 107935000 79183000 58349394 122476000 121718000 125238000 106531000 92439000 91156000 73415000 56517000 9500000 10464000 6647000 3918000 40388000 53892000 46671000 20481000
Net Receivables 9107000 28989000 6665000 6413180 29524000 26481000 22431000 18277000 21193000 18705000 15632000 13459000 16259000 11835000 8339000 5654000 48995000 35673000 29299000 30343000
Inventory NA NA NA NA 2063000 2662000 2181000 2251000 1107000 749000 268000 491000 17174000 16047000 11461000 10243000 3956000 4855000 2132000 2349000
Other Current Assets 5818000 2763000 2625000 3168819 10133000 6855000 5183000 6091000 4236000 2983000 3175000 1590000 418000 1329000 NA NA 12087000 13936000 8283000 14691000
Total Current Assets 155094000 151169000 99760000 78233663 175552000 169662000 162696000 139660000 135676000 124308000 105408000 90114000 75101000 60197000 45781000 35705000 131339000 128645000 106869000 89378000
Long Term Investments 80647000 56451000 45690000 37958591 2649000 1862000 6023000 10438000 13859000 7813000 5878000 5183000 440000 441000 223000 16000 170799000 194714000 170430000 164065000
Property, plant and equipment 17903000 12475000 11294000 10627127 43856000 36146000 30289000 18356000 59719000 42383000 34234000 29016000 61797000 48866000 29114000 21838000 41304000 33783000 27010000 22471000
Goodwill 18536000 15806000 15342000 15395573 42026000 35683000 35122000 17872000 17888000 16747000 16468000 15869000 14548000 13350000 3784000 3759000 NA NA 5414000 5116000
Intangible Assets 9181000 5467000 3872000 3334619 7750000 8053000 10106000 3733000 2220000 2692000 3307000 3847000 4110000 3371000 854000 992000 NA NA 3206000 3893000
Accumulated Amortization NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Other Assets 16205000 10360000 6039000 2303735 14723000 7442000 6076000 3409000 3430000 3352000 2202000 3432000 6652000 5085000 3646000 2437000 22283000 18177000 8757000 5422000
Deferred Long Term Asset Charges 2324000 1532000 1100000 1008174 7536000 1369000 248000 219000 737000 680000 383000 251000 NA NA NA NA NA NA NA NA
Total Assets 297566000 251728000 181997000 147853308 286556000 258848000 250312000 193468000 232792000 197295000 167497000 147461000 162648000 131310000 83402000 64747000 365725000 375319000 321686000 290345000
Accounts Payable 2966000 2300000 2184000 1054936 9382000 8617000 7390000 6898000 4378000 3137000 2041000 1931000 38192000 34616000 25309000 20397000 55888000 44242000 37294000 35490000
Short/Current Long Term Debt 6955000 6510000 8671000 974820 5516000 3998000 1049000 NA NA NA NA 1000000 1371000 100000 1056000 238000 8784000 6496000 3500000 2513000
Other Current Liabilities 28841000 60413000 24742000 15975724 45860000 38195000 30879000 33972000 16009000 10651000 5851000 4327000 9959000 8565000 7168000 5118000 40230000 38099000 8243000 10939000
Total Current Liabilities 56853000 82057000 46102000 26102802 69420000 58488000 55745000 59357000 34620000 24183000 16756000 19310000 68391000 57883000 43816000 33887000 116866000 100814000 79006000 80610000
Long Term Debt 54903000 35812000 34470000 33941792 66662000 72242000 76073000 40557000 3950000 3943000 3935000 1995000 23495000 24743000 7694000 8227000 93735000 97207000 75427000 53329000
Other Liabilities 10058000 3487000 3682000 3584563 35699000 35707000 22986000 20796000 16532000 16641000 7770000 5825000 10921000 7792000 5088000 3301000 47977000 43251000 39004000 37051000
Deferred Long Term Liability Charges 3700000 1600000 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Minority Interest 12855000 15026000 5469000 3960053 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Negative Goodwill NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Total Liabilities 121814000 121356000 84254000 63637592 184226000 176130000 162601000 121471000 55164000 44793000 28461000 27130000 119099000 103601000 64117000 51363000 258578000 241272000 193437000 170990000
Misc. Stocks Options Warrants NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Redeemable Preferred Stock NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Preferred Stock NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Common Stock NA NA NA 15 78520000 71223000 69315000 68178000 45049000 40247000 36307000 32982000 5000 5000 5000 5000 40201000 35867000 31251000 27416000
Retained Earnings 129246000 102328000 85734000 74659355 24150000 13682000 17769000 2282000 134885000 113247000 105131000 89223000 19625000 8636000 4916000 2545000 70400000 98330000 96364000 92284000
Treasury Stock 210000 930000 -1783000 -806056 -340000 -2187000 627000 1537000 -2306000 -992000 -2402000 -1874000 -2872000 -2321000 -2822000 -2560000 -3454000 -150000 634000 -345000
Capital Surplus 33441000 12088000 8323000 6402349 NA NA NA NA NA NA NA NA 26791000 21389000 17186000 13394000 NA NA NA NA
Other Stockholder Equity 210000 930000 -1783000 -806056 -340000 -2187000 627000 1537000 -2306000 -992000 -2402000 -1874000 -1035000 -484000 -985000 -723000 -3454000 -150000 634000 -345000
Total stockholders’ equity 162897000 115346000 92274000 80255663 102330000 82718000 87711000 71997000 177628000 152502000 139036000 120331000 43549000 27709000 19285000 13384000 107147000 134047000 128249000 119355000
Net Tangible Assets 135180000 94073000 73060000 61525471 52554000 38982000 42483000 50392000 157520000 133063000 119261000 100615000 24891000 10988000 14647000 8633000 107147000 134047000 119629000 110346000

How the Cash Flow Statement looks:

Table 3: Cash Flow Statements
BIDU.f.12.31.2018 BIDU.f.12.31.2017 BIDU.f.12.31.2016 BIDU.f.12.31.2015 MSFT.f.6.30.2019 MSFT.f.6.30.2018 MSFT.f.6.30.2017 MSFT.f.6.30.2016 GOOG.f.12.31.2018 GOOG.f.12.31.2017 GOOG.f.12.31.2016 GOOG.f.12.31.2015 AMZN.f.12.31.2018 AMZN.f.12.31.2017 AMZN.f.12.31.2016 AMZN.f.12.31.2015 AAPL.f.9.29.2018 AAPL.f.9.30.2017 AAPL.f.9.24.2016 AAPL.f.9.26.2015
Net Income 27573000 18301000 11632000 33664000 39240000 16571000 25489000 20539000 30736000 12662000 19478000 16348000 10073000 3033000 2371000 596000 59531000 48351000 45687000 53394000
Depreciation 16187000 11748000 8327000 5848000 11600000 9900000 7800000 5878000 9029000 6899000 6100000 5024000 15341000 11478000 8116000 5646000 10903000 10157000 10505000 11257000
Adjustments To Net Income -11074000 -3744000 -3721000 -24690000 -2521000 -3054000 1342000 6229000 3298000 8284000 7158000 5609000 6352000 4096000 2869000 2605000 -27694000 10640000 9634000 5353000
Changes In Accounts Receivables -1611000 -721000 -238000 -869000 -2812000 -3862000 -1216000 562000 -2169000 -3768000 -2578000 -2094000 -4615000 -4780000 -3436000 -1755000 -5322000 -2093000 527000 417000
Changes In Liabilities 4942000 6113000 4976000 9228000 4694000 7070000 3901000 2653000 1438000 1121000 333000 246000 4414000 7838000 6985000 5586000 9131000 8340000 563000 6043000
Changes In Inventories NA NA NA NA 597000 -465000 50000 600000 NA NA NA NA -1314000 -3583000 -1426000 -2187000 828000 -2723000 217000 -238000
Changes In Other Operating Activities -50000 1131000 1504000 -3423000 -1542000 -459000 349000 -2907000 7890000 3682000 2420000 1618000 472000 283000 1724000 913000 30057000 -8447000 -902000 5040000
Total Cash Flow From Operating Activities 35967000 32828000 22480000 19771000 52185000 43884000 39507000 33325000 47971000 37091000 36036000 26572000 30723000 18365000 17203000 12039000 77434000 64225000 66231000 81266000
Capital Expenditure -8861000 -4829000 -4215000 -5251000 -13925000 -11632000 -8129000 -8343000 -25139000 -13184000 -10212000 -9950000 -13427000 -11955000 -7804000 -5387000 -13313000 -12451000 -12734000 -11247000
Investments -21668000 -40411000 -21371000 -19665000 540000 6557000 -12511000 -14417000 -1972000 -19448000 -18229000 -13635000 1140000 -3054000 -2663000 -1066000 30845000 -33542000 -32022000 -44417000
Other Cash flows from Investing Activities 3685000 13000 5000 8000 NA -98000 -197000 203000 NA 1419000 -1978000 75000 2104000 1897000 1067000 798000 -745000 -124000 -924000 -26000
Total Cash Flows From Investing Activities -34460000 -76949000 -35911000 -31621000 -15773000 -6061000 -46781000 -23950000 -28504000 -31401000 -31165000 -23711000 -12369000 -27084000 -9516000 -6450000 16066000 -46446000 -45977000 -56274000
Dividends Paid NA NA NA NA -13811000 -12699000 -11845000 -11006000 NA NA NA NA NA NA NA NA -13712000 -12769000 -12150000 -11561000
Sale Purchase of Stock NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Net Borrowings 20727000 23579000 6850000 10352000 -4000000 -10201000 31459000 18283000 -61000 -86000 -1335000 -23000 -7686000 9928000 -3716000 -3882000 432000 29014000 22057000 29305000
Other Cash Flows from Financing Activities -3009000 22248000 7421000 3578000 -675000 -971000 -190000 -369000 -4043000 -3366000 -3304000 -2422000 NA NA NA NA NA NA NA 749000
Total Cash Flows From Financing Activities 15082000 44557000 14447000 7778000 -36887000 -33590000 8408000 -8393000 -13179000 -8298000 -8332000 -4225000 -7686000 9928000 -3716000 -3882000 -87876000 -17974000 -20890000 -17716000
Effect Of Exchange Rate Changes 1902000 -316000 144000 179000 -115000 50000 19000 -67000 -302000 405000 -170000 -434000 -351000 713000 -212000 -374000 NA NA NA NA
Change In Cash and Cash Equivalents 18491000 120000 1160000 -3893000 -590000 4283000 1153000 915000 5986000 -2203000 -3631000 -1798000 10317000 1922000 3759000 1333000 5624000 -195000 -636000 7276000

The data still looks a little messy and unreadable and in order to do the calculations we should have the data in a different shape. That is, I find it easier to program calculations column wise instead of rowwise. It’s also easier to combine the data instead of calculating the ratios across different data frames as we have currently.

Clean the data up a little bit

ISBSCF <- rbind(IS, BS, CF)

ISBSCF <- ISBSCF %>%
  t() %>%
  data.frame() %>%
  rownames_to_column('rn') %>%
  separate(rn, into = c("symbol", "year"),
           sep = -4, convert = TRUE)


ISBSCF$symbol <- gsub("(f).*", "", ISBSCF$symbol)
ISBSCF$symbol <- gsub('.$', '', ISBSCF$symbol)
ISBSCF$symbol <- gsub('^X', '', ISBSCF$symbol)

How all the fundamentals look now:

Table 4: Income Statement - Balance Sheet - Cash Flow
symbol year Total.Revenue Cost.of.Revenue Gross.Profit Research.Development Selling.General.and.Administrative Non.Recurring Others Total.Operating.Expenses Operating.Income.or.Loss Total.Other.Income.Expenses.Net Earnings.Before.Interest.and.Taxes Interest.Expense Income.Before.Tax Income.Tax.Expense Minority.Interest Net.Income.From.Continuing.Ops Discontinued.Operations Extraordinary.Items Effect.Of.Accounting.Changes Other.Items Net.Income Preferred.Stock.And.Other.Adjustments Net.Income.Applicable.To.Common.Shares Cash.And.Cash.Equivalents Short.Term.Investments Net.Receivables Inventory Other.Current.Assets Total.Current.Assets Long.Term.Investments Property..plant.and.equipment Goodwill Intangible.Assets Accumulated.Amortization Other.Assets Deferred.Long.Term.Asset.Charges Total.Assets Accounts.Payable Short.Current.Long.Term.Debt Other.Current.Liabilities Total.Current.Liabilities Long.Term.Debt Other.Liabilities Deferred.Long.Term.Liability.Charges Minority.Interest1 Negative.Goodwill Total.Liabilities Misc..Stocks.Options.Warrants Redeemable.Preferred.Stock Preferred.Stock Common.Stock Retained.Earnings Treasury.Stock Capital.Surplus Other.Stockholder.Equity Total.stockholders..equity Net.Tangible.Assets Net.Income1 Depreciation Adjustments.To.Net.Income Changes.In.Accounts.Receivables Changes.In.Liabilities Changes.In.Inventories Changes.In.Other.Operating.Activities Total.Cash.Flow.From.Operating.Activities Capital.Expenditure Investments Other.Cash.flows.from.Investing.Activities Total.Cash.Flows.From.Investing.Activities Dividends.Paid Sale.Purchase.of.Stock Net.Borrowings Other.Cash.Flows.from.Financing.Activities Total.Cash.Flows.From.Financing.Activities Effect.Of.Exchange.Rate.Changes Change.In.Cash.and.Cash.Equivalents
BIDU 2018 103877000 51744000 52133000 15772000 19231000 NA NA 86747000 17130000 10195000 17130000 -3483000 27325000 4743000 12855000 22582000 NA NA NA NA 27573000 NA 27443000 27638000 111873000 9107000 NA 5818000 155094000 80647000 17903000 18536000 9181000 NA 16205000 2324000 297566000 2966000 6955000 28841000 56853000 54903000 10058000 3700000 12855000 NA 121814000 NA NA NA NA 129246000 210000 33441000 210000 162897000 135180000 27573000 16187000 -11074000 -1611000 4942000 NA -50000 35967000 -8861000 -21668000 3685000 -34460000 NA NA 20727000 -3009000 15082000 1902000 18491000
BIDU 2017 86709000 38294000 48415000 12928000 13128000 NA 4768000 69118000 17591000 3692000 17591000 -3515000 21283000 2995000 15026000 18288000 NA NA NA NA 18301000 NA 18318000 11084000 107935000 28989000 NA 2763000 151169000 56451000 12475000 15806000 5467000 NA 10360000 1532000 251728000 2300000 6510000 60413000 82057000 35812000 3487000 1600000 15026000 NA 121356000 NA NA NA NA 102328000 930000 12088000 930000 115346000 94073000 18301000 11748000 -3744000 -721000 6113000 NA 1131000 32828000 -4829000 -40411000 13000 -76949000 NA NA 23579000 22248000 44557000 -316000 120000
BIDU 2016 70549000 31358000 39191000 10151000 15071000 NA 3920000 60500000 10049000 4460000 10049000 -1158000 14509000 2913000 5469000 11596000 NA NA NA NA 11632000 NA 11075000 10898000 79183000 6665000 NA 2625000 99760000 45690000 11294000 15342000 3872000 NA 6039000 1100000 181997000 2184000 8671000 24742000 46102000 34470000 3682000 NA 5469000 NA 84254000 NA NA NA NA 85734000 -1783000 8323000 -1783000 92274000 73060000 11632000 8327000 -3721000 -238000 4976000 NA 1504000 22480000 -4215000 -21371000 5000 -35911000 NA NA 6850000 7421000 14447000 144000 1160000
BIDU 2015 66382000 22814000 43568000 10176000 17076000 NA 4644000 54710000 11672000 26235000 11672000 -1041000 37907000 5475000 3960053 32432000 NA NA NA NA 33664000 NA 33335000 9959932 58349394 6413180 NA 3168819 78233663 37958591 10627127 15395573 3334619 NA 2303735 1008174 147853308 1054936 974820 15975724 26102802 33941792 3584563 NA 3960053 NA 63637592 NA NA NA 15 74659355 -806056 6402349 -806056 80255663 61525471 33664000 5848000 -24690000 -869000 9228000 NA -3423000 19771000 -5251000 -19665000 8000 -31621000 NA NA 10352000 3578000 7778000 179000 -3893000
MSFT 2019 125843000 42910000 82933000 16876000 23098000 NA NA 82884000 42959000 729000 42959000 -2686000 43688000 4448000 NA 39240000 NA NA NA NA 39240000 NA 39240000 11356000 122476000 29524000 2063000 10133000 175552000 2649000 43856000 42026000 7750000 NA 14723000 7536000 286556000 9382000 5516000 45860000 69420000 66662000 35699000 NA NA NA 184226000 NA NA NA 78520000 24150000 -340000 NA -340000 102330000 52554000 39240000 11600000 -2521000 -2812000 4694000 597000 -1542000 52185000 -13925000 540000 NA -15773000 -13811000 NA -4000000 -675000 -36887000 -115000 -590000
MSFT 2018 110360000 38353000 72007000 14726000 22223000 NA NA 75302000 35058000 1416000 35058000 -2733000 36474000 19903000 NA 16571000 NA NA NA NA 16571000 NA 16571000 11946000 121718000 26481000 2662000 6855000 169662000 1862000 36146000 35683000 8053000 NA 7442000 1369000 258848000 8617000 3998000 38195000 58488000 72242000 35707000 NA NA NA 176130000 NA NA NA 71223000 13682000 -2187000 NA -2187000 82718000 38982000 16571000 9900000 -3054000 -3862000 7070000 -465000 -459000 43884000 -11632000 6557000 -98000 -6061000 -12699000 NA -10201000 -971000 -33590000 50000 4283000
MSFT 2017 96571000 34261000 62310000 13037000 19942000 NA NA 67240000 29331000 570000 29331000 -2222000 29901000 4412000 NA 25489000 NA NA NA NA 25489000 NA 25489000 7663000 125238000 22431000 2181000 5183000 162696000 6023000 30289000 35122000 10106000 NA 6076000 248000 250312000 7390000 1049000 30879000 55745000 76073000 22986000 NA NA NA 162601000 NA NA NA 69315000 17769000 627000 NA 627000 87711000 42483000 25489000 7800000 1342000 -1216000 3901000 50000 349000 39507000 -8129000 -12511000 -197000 -46781000 -11845000 NA 31459000 -190000 8408000 19000 1153000
MSFT 2016 91154000 32780000 58374000 11988000 19198000 NA NA 63966000 27188000 -1549000 27188000 -1243000 25639000 5100000 NA 20539000 NA NA NA NA 20539000 NA 20539000 6510000 106531000 18277000 2251000 6091000 139660000 10438000 18356000 17872000 3733000 NA 3409000 219000 193468000 6898000 NA 33972000 59357000 40557000 20796000 NA NA NA 121471000 NA NA NA 68178000 2282000 1537000 NA 1537000 71997000 50392000 20539000 5878000 6229000 562000 2653000 600000 -2907000 33325000 -8343000 -14417000 203000 -23950000 -11006000 NA 18283000 -369000 -8393000 -67000 915000
GOOG 2018 136819000 59549000 77270000 21419000 24459000 NA NA 105427000 31392000 3521000 31392000 -114000 34913000 4177000 NA 30736000 NA NA NA NA 30736000 NA 30736000 16701000 92439000 21193000 1107000 4236000 135676000 13859000 59719000 17888000 2220000 NA 3430000 737000 232792000 4378000 NA 16009000 34620000 3950000 16532000 NA NA NA 55164000 NA NA NA 45049000 134885000 -2306000 NA -2306000 177628000 157520000 30736000 9029000 3298000 -2169000 1438000 NA 7890000 47971000 -25139000 -1972000 NA -28504000 NA NA -61000 -4043000 -13179000 -302000 5986000
GOOG 2017 110855000 45583000 65272000 16625000 19765000 NA NA 81973000 28882000 -1689000 28882000 -109000 27193000 14531000 NA 12662000 NA NA NA NA 12662000 NA 12662000 10715000 91156000 18705000 749000 2983000 124308000 7813000 42383000 16747000 2692000 NA 3352000 680000 197295000 3137000 NA 10651000 24183000 3943000 16641000 NA NA NA 44793000 NA NA NA 40247000 113247000 -992000 NA -992000 152502000 133063000 12662000 6899000 8284000 -3768000 1121000 NA 3682000 37091000 -13184000 -19448000 1419000 -31401000 NA NA -86000 -3366000 -8298000 405000 -2203000
GOOG 2016 90272000 35138000 55134000 13948000 17470000 NA NA 66556000 23716000 434000 23716000 -124000 24150000 4672000 NA 19478000 NA NA NA NA 19478000 NA 19478000 12918000 73415000 15632000 268000 3175000 105408000 5878000 34234000 16468000 3307000 NA 2202000 383000 167497000 2041000 NA 5851000 16756000 3935000 7770000 NA NA NA 28461000 NA NA NA 36307000 105131000 -2402000 NA -2402000 139036000 119261000 19478000 6100000 7158000 -2578000 333000 NA 2420000 36036000 -10212000 -18229000 -1978000 -31165000 NA NA -1335000 -3304000 -8332000 -170000 -3631000
GOOG 2015 74989000 28164000 46825000 12282000 15183000 NA NA 55629000 19360000 291000 19360000 -104000 19651000 3303000 NA 16348000 NA NA NA NA 16348000 NA 16348000 15409000 56517000 13459000 491000 1590000 90114000 5183000 29016000 15869000 3847000 NA 3432000 251000 147461000 1931000 1000000 4327000 19310000 1995000 5825000 NA NA NA 27130000 NA NA NA 32982000 89223000 -1874000 NA -1874000 120331000 100615000 16348000 5024000 5609000 -2094000 246000 NA 1618000 26572000 -9950000 -13635000 75000 -23711000 NA NA -23000 -2422000 -4225000 -434000 -1798000
AMZN 2018 232887000 139156000 93731000 28837000 52177000 NA 296000 220466000 12421000 -1151000 12421000 -1417000 11270000 1197000 NA 10073000 NA NA NA NA 10073000 NA 10073000 31750000 9500000 16259000 17174000 418000 75101000 440000 61797000 14548000 4110000 NA 6652000 NA 162648000 38192000 1371000 9959000 68391000 23495000 10921000 NA NA NA 119099000 NA NA NA 5000 19625000 -2872000 26791000 -1035000 43549000 24891000 10073000 15341000 6352000 -4615000 4414000 -1314000 472000 30723000 -13427000 1140000 2104000 -12369000 NA NA -7686000 NA -7686000 -351000 10317000
AMZN 2017 177866000 111934000 65932000 22620000 38992000 NA 214000 173760000 4106000 -304000 4106000 -848000 3802000 769000 NA 3033000 NA NA NA NA 3033000 NA 3033000 20522000 10464000 11835000 16047000 1329000 60197000 441000 48866000 13350000 3371000 NA 5085000 NA 131310000 34616000 100000 8565000 57883000 24743000 7792000 NA NA NA 103601000 NA NA NA 5000 8636000 -2321000 21389000 -484000 27709000 10988000 3033000 11478000 4096000 -4780000 7838000 -3583000 283000 18365000 -11955000 -3054000 1897000 -27084000 NA NA 9928000 NA 9928000 713000 1922000
AMZN 2016 135987000 88265000 47722000 16085000 27284000 NA 167000 131801000 4186000 -390000 4186000 -484000 3796000 1425000 NA 2371000 NA NA NA NA 2371000 NA 2371000 19334000 6647000 8339000 11461000 NA 45781000 223000 29114000 3784000 854000 NA 3646000 NA 83402000 25309000 1056000 7168000 43816000 7694000 5088000 NA NA NA 64117000 NA NA NA 5000 4916000 -2822000 17186000 -985000 19285000 14647000 2371000 8116000 2869000 -3436000 6985000 -1426000 1724000 17203000 -7804000 -2663000 1067000 -9516000 NA NA -3716000 NA -3716000 -212000 3759000
AMZN 2015 107006000 71651000 35355000 12540000 20411000 NA 171000 104773000 2233000 -687000 2233000 -459000 1546000 950000 NA 596000 NA NA NA NA 596000 NA 596000 15890000 3918000 5654000 10243000 NA 35705000 16000 21838000 3759000 992000 NA 2437000 NA 64747000 20397000 238000 5118000 33887000 8227000 3301000 NA NA NA 51363000 NA NA NA 5000 2545000 -2560000 13394000 -723000 13384000 8633000 596000 5646000 2605000 -1755000 5586000 -2187000 913000 12039000 -5387000 -1066000 798000 -6450000 NA NA -3882000 NA -3882000 -374000 1333000
AAPL 2018 265595000 163756000 101839000 14236000 16705000 NA NA 194697000 70898000 2005000 70898000 -3240000 72903000 13372000 NA 59531000 NA NA NA NA 59531000 NA 59531000 25913000 40388000 48995000 3956000 12087000 131339000 170799000 41304000 NA NA NA 22283000 NA 365725000 55888000 8784000 40230000 116866000 93735000 47977000 NA NA NA 258578000 NA NA NA 40201000 70400000 -3454000 NA -3454000 107147000 107147000 59531000 10903000 -27694000 -5322000 9131000 828000 30057000 77434000 -13313000 30845000 -745000 16066000 -13712000 NA 432000 NA -87876000 NA 5624000
AAPL 2017 229234000 141048000 88186000 11581000 15261000 NA NA 167890000 61344000 2745000 61344000 -2323000 64089000 15738000 NA 48351000 NA NA NA NA 48351000 NA 48351000 20289000 53892000 35673000 4855000 13936000 128645000 194714000 33783000 NA NA NA 18177000 NA 375319000 44242000 6496000 38099000 100814000 97207000 43251000 NA NA NA 241272000 NA NA NA 35867000 98330000 -150000 NA -150000 134047000 134047000 48351000 10157000 10640000 -2093000 8340000 -2723000 -8447000 64225000 -12451000 -33542000 -124000 -46446000 -12769000 NA 29014000 NA -17974000 NA -195000
AAPL 2016 215639000 131376000 84263000 10045000 14194000 NA NA 155615000 60024000 1348000 60024000 -1456000 61372000 15685000 NA 45687000 NA NA NA NA 45687000 NA 45687000 20484000 46671000 29299000 2132000 8283000 106869000 170430000 27010000 5414000 3206000 NA 8757000 NA 321686000 37294000 3500000 8243000 79006000 75427000 39004000 NA NA NA 193437000 NA NA NA 31251000 96364000 634000 NA 634000 128249000 119629000 45687000 10505000 9634000 527000 563000 217000 -902000 66231000 -12734000 -32022000 -924000 -45977000 -12150000 NA 22057000 NA -20890000 NA -636000
AAPL 2015 233715000 140089000 93626000 8067000 14329000 NA NA 162485000 71230000 1285000 71230000 -733000 72515000 19121000 NA 53394000 NA NA NA NA 53394000 NA 53394000 21120000 20481000 30343000 2349000 14691000 89378000 164065000 22471000 5116000 3893000 NA 5422000 NA 290345000 35490000 2513000 10939000 80610000 53329000 37051000 NA NA NA 170990000 NA NA NA 27416000 92284000 -345000 NA -345000 119355000 110346000 53394000 11257000 5353000 417000 6043000 -238000 5040000 81266000 -11247000 -44417000 -26000 -56274000 -11561000 NA 29305000 749000 -17716000 NA 7276000

This looks much cleaner. The data we currently have at the moment is just the Income Statement, Balance Sheet, and Cash Flow statements, but in order to make better comparisons across firms other information is needed such as Shares Outstanding which will be used in the Market Capitalisation calculation. We can use the quantmod package to pull some statistics from the Yahoo Finance statistics page.

############################## Get stats data ##################################

metrics <- yahooQF(c("Name", "Enterprise Value", "Enterprise Value/Revenue", "Enterprise Value/EBITDA", "Total Cash Per Share (mrq)", "EBITDA", "Volume", "P/E Ratio", "Dividend Yield", "Shares Outstanding", "Price/Book"))

stats <- getQuote(symbols, what = metrics) %>%
  rownames_to_column("symbol")

colnames(stats) <- gsub(" ", ".", colnames(stats))
colnames(stats) <- gsub("/", ".", colnames(stats))

How the company statistics look:

Table 5: Company Statistics
symbol Trade.Time Name Volume P.E.Ratio Dividend.Yield Shares.Outstanding Price.Book
AAPL 2019-09-25 16:00:01 Apple Inc.  19675563 18.767937 0.0135979 4519179776 10.383821
AMZN 2019-09-25 16:00:01 Amazon.com, Inc.  3217392 73.362510 NA 494656000 16.463211
GOOG 2019-09-25 16:00:01 Alphabet Inc.  1345323 25.164429 NA 347344992 4.501470
MSFT 2019-09-25 16:00:01 Microsoft Corporation 20835063 27.541502 0.0133935 7635409920 10.408545
BIDU 2019-09-25 16:00:01 Baidu, Inc.  3734534 8.009974 NA 348540992 1.476227

You can play around with downloading other statistics directly from the Yahoo Statistics pages, for instance, heres the statistics page for GOOG here. Already we have quite a bit of useful information, however not all of the statistics are able to be scraped from the Yahoo Finance statistics pages, therefore we will have to try and compute some ourselves. Moreover, we have some of the important statistics - Shares Outstanding and we can use the PE.Ratio, Dividend.Yield and Price.Book ratios to check the accuracy of our calculations later on.

We also need to collect the stock price data since we need the Closing Price today to compute per share statistics.

############################## Get latest stock price data ######################
stocks <- symbols %>%
  tq_get(get = "stock.prices",
         from =  "2018-01-01",
         to =  Sys.Date()) %>%
  select(symbol, date, adjusted) %>%
  group_by(symbol) %>%
  arrange(desc(date)) %>%
  slice(1)

How the stock price data looks:

Table 6: Most Recent Stock Prices
symbol date adjusted
AAPL 2019-09-25 221.03
AMZN 2019-09-25 1768.33
BIDU 2019-09-25 102.80
GOOG 2019-09-25 1246.52
MSFT 2019-09-25 139.36

We have three different data sets now ISBSCF, which is our combined Income Statement, Balance Sheet and Cash Flow statements, stats which is our Yahoo Finance company statistics data and finally stock which is todays closing price of our companies.

Now I join all the data together (we are only interested in 2018 values but we could extend it to more years).

############################## Join all 3 datasets together ######################
financials <- ISBSCF %>%
  filter(year == 2018) %>%
  inner_join(stats, by = "symbol") %>%
  inner_join(stocks, by = "symbol")

How all the data looks after joining the fundamentals, stats and latest share price data:

Table 7: All Data
symbol year Total.Revenue Cost.of.Revenue Gross.Profit Research.Development Selling.General.and.Administrative Non.Recurring Others Total.Operating.Expenses Operating.Income.or.Loss Total.Other.Income.Expenses.Net Earnings.Before.Interest.and.Taxes Interest.Expense Income.Before.Tax Income.Tax.Expense Minority.Interest Net.Income.From.Continuing.Ops Discontinued.Operations Extraordinary.Items Effect.Of.Accounting.Changes Other.Items Net.Income Preferred.Stock.And.Other.Adjustments Net.Income.Applicable.To.Common.Shares Cash.And.Cash.Equivalents Short.Term.Investments Net.Receivables Inventory Other.Current.Assets Total.Current.Assets Long.Term.Investments Property..plant.and.equipment Goodwill Intangible.Assets Accumulated.Amortization Other.Assets Deferred.Long.Term.Asset.Charges Total.Assets Accounts.Payable Short.Current.Long.Term.Debt Other.Current.Liabilities Total.Current.Liabilities Long.Term.Debt Other.Liabilities Deferred.Long.Term.Liability.Charges Minority.Interest1 Negative.Goodwill Total.Liabilities Misc..Stocks.Options.Warrants Redeemable.Preferred.Stock Preferred.Stock Common.Stock Retained.Earnings Treasury.Stock Capital.Surplus Other.Stockholder.Equity Total.stockholders..equity Net.Tangible.Assets Net.Income1 Depreciation Adjustments.To.Net.Income Changes.In.Accounts.Receivables Changes.In.Liabilities Changes.In.Inventories Changes.In.Other.Operating.Activities Total.Cash.Flow.From.Operating.Activities Capital.Expenditure Investments Other.Cash.flows.from.Investing.Activities Total.Cash.Flows.From.Investing.Activities Dividends.Paid Sale.Purchase.of.Stock Net.Borrowings Other.Cash.Flows.from.Financing.Activities Total.Cash.Flows.From.Financing.Activities Effect.Of.Exchange.Rate.Changes Change.In.Cash.and.Cash.Equivalents Trade.Time Name Volume P.E.Ratio Dividend.Yield Shares.Outstanding Price.Book date adjusted
BIDU 2018 103877000 51744000 52133000 15772000 19231000 NA NA 86747000 17130000 10195000 17130000 -3483000 27325000 4743000 12855000 22582000 NA NA NA NA 27573000 NA 27443000 27638000 111873000 9107000 NA 5818000 155094000 80647000 17903000 18536000 9181000 NA 16205000 2324000 297566000 2966000 6955000 28841000 56853000 54903000 10058000 3700000 12855000 NA 121814000 NA NA NA NA 129246000 210000 33441000 210000 162897000 135180000 27573000 16187000 -11074000 -1611000 4942000 NA -50000 35967000 -8861000 -21668000 3685000 -34460000 NA NA 20727000 -3009000 15082000 1902000 18491000 2019-09-25 16:00:01 Baidu, Inc.  3734534 8.009974 NA 348540992 1.476227 2019-09-25 102.80
MSFT 2018 110360000 38353000 72007000 14726000 22223000 NA NA 75302000 35058000 1416000 35058000 -2733000 36474000 19903000 NA 16571000 NA NA NA NA 16571000 NA 16571000 11946000 121718000 26481000 2662000 6855000 169662000 1862000 36146000 35683000 8053000 NA 7442000 1369000 258848000 8617000 3998000 38195000 58488000 72242000 35707000 NA NA NA 176130000 NA NA NA 71223000 13682000 -2187000 NA -2187000 82718000 38982000 16571000 9900000 -3054000 -3862000 7070000 -465000 -459000 43884000 -11632000 6557000 -98000 -6061000 -12699000 NA -10201000 -971000 -33590000 50000 4283000 2019-09-25 16:00:01 Microsoft Corporation 20835063 27.541502 0.0133935 7635409920 10.408545 2019-09-25 139.36
GOOG 2018 136819000 59549000 77270000 21419000 24459000 NA NA 105427000 31392000 3521000 31392000 -114000 34913000 4177000 NA 30736000 NA NA NA NA 30736000 NA 30736000 16701000 92439000 21193000 1107000 4236000 135676000 13859000 59719000 17888000 2220000 NA 3430000 737000 232792000 4378000 NA 16009000 34620000 3950000 16532000 NA NA NA 55164000 NA NA NA 45049000 134885000 -2306000 NA -2306000 177628000 157520000 30736000 9029000 3298000 -2169000 1438000 NA 7890000 47971000 -25139000 -1972000 NA -28504000 NA NA -61000 -4043000 -13179000 -302000 5986000 2019-09-25 16:00:01 Alphabet Inc.  1345323 25.164429 NA 347344992 4.501470 2019-09-25 1246.52
AMZN 2018 232887000 139156000 93731000 28837000 52177000 NA 296000 220466000 12421000 -1151000 12421000 -1417000 11270000 1197000 NA 10073000 NA NA NA NA 10073000 NA 10073000 31750000 9500000 16259000 17174000 418000 75101000 440000 61797000 14548000 4110000 NA 6652000 NA 162648000 38192000 1371000 9959000 68391000 23495000 10921000 NA NA NA 119099000 NA NA NA 5000 19625000 -2872000 26791000 -1035000 43549000 24891000 10073000 15341000 6352000 -4615000 4414000 -1314000 472000 30723000 -13427000 1140000 2104000 -12369000 NA NA -7686000 NA -7686000 -351000 10317000 2019-09-25 16:00:01 Amazon.com, Inc.  3217392 73.362510 NA 494656000 16.463211 2019-09-25 1768.33
AAPL 2018 265595000 163756000 101839000 14236000 16705000 NA NA 194697000 70898000 2005000 70898000 -3240000 72903000 13372000 NA 59531000 NA NA NA NA 59531000 NA 59531000 25913000 40388000 48995000 3956000 12087000 131339000 170799000 41304000 NA NA NA 22283000 NA 365725000 55888000 8784000 40230000 116866000 93735000 47977000 NA NA NA 258578000 NA NA NA 40201000 70400000 -3454000 NA -3454000 107147000 107147000 59531000 10903000 -27694000 -5322000 9131000 828000 30057000 77434000 -13313000 30845000 -745000 16066000 -13712000 NA 432000 NA -87876000 NA 5624000 2019-09-25 16:00:01 Apple Inc.  19675563 18.767937 0.0135979 4519179776 10.383821 2019-09-25 221.03

Now I think we have enough information to calculate some financial ratios based on company statistics and their respective financial data. The variables we have are:

What variables we have in the data:

colnames(financials) %>%
  data.frame() %>%
  setNames(c("Fundamentals")) %>%
  split(as.integer(gl(nrow(.), 14, nrow(.)))) %>%
  kable(caption = "Company Variables") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), font_size = 12)
Table 8: Company Variables
Fundamentals
symbol
year
Total.Revenue
Cost.of.Revenue
Gross.Profit
Research.Development
Selling.General.and.Administrative
Non.Recurring
Others
Total.Operating.Expenses
Operating.Income.or.Loss
Total.Other.Income.Expenses.Net
Earnings.Before.Interest.and.Taxes
Interest.Expense
Fundamentals
15 Income.Before.Tax
16 Income.Tax.Expense
17 Minority.Interest
18 Net.Income.From.Continuing.Ops
19 Discontinued.Operations
20 Extraordinary.Items
21 Effect.Of.Accounting.Changes
22 Other.Items
23 Net.Income
24 Preferred.Stock.And.Other.Adjustments
25 Net.Income.Applicable.To.Common.Shares
26 Cash.And.Cash.Equivalents
27 Short.Term.Investments
28 Net.Receivables
Fundamentals
29 Inventory
30 Other.Current.Assets
31 Total.Current.Assets
32 Long.Term.Investments
33 Property..plant.and.equipment
34 Goodwill
35 Intangible.Assets
36 Accumulated.Amortization
37 Other.Assets
38 Deferred.Long.Term.Asset.Charges
39 Total.Assets
40 Accounts.Payable
41 Short.Current.Long.Term.Debt
42 Other.Current.Liabilities
Fundamentals
43 Total.Current.Liabilities
44 Long.Term.Debt
45 Other.Liabilities
46 Deferred.Long.Term.Liability.Charges
47 Minority.Interest1
48 Negative.Goodwill
49 Total.Liabilities
50 Misc..Stocks.Options.Warrants
51 Redeemable.Preferred.Stock
52 Preferred.Stock
53 Common.Stock
54 Retained.Earnings
55 Treasury.Stock
56 Capital.Surplus
Fundamentals
57 Other.Stockholder.Equity
58 Total.stockholders..equity
59 Net.Tangible.Assets
60 Net.Income1
61 Depreciation
62 Adjustments.To.Net.Income
63 Changes.In.Accounts.Receivables
64 Changes.In.Liabilities
65 Changes.In.Inventories
66 Changes.In.Other.Operating.Activities
67 Total.Cash.Flow.From.Operating.Activities
68 Capital.Expenditure
69 Investments
70 Other.Cash.flows.from.Investing.Activities
Fundamentals
71 Total.Cash.Flows.From.Investing.Activities
72 Dividends.Paid
73 Sale.Purchase.of.Stock
74 Net.Borrowings
75 Other.Cash.Flows.from.Financing.Activities
76 Total.Cash.Flows.From.Financing.Activities
77 Effect.Of.Exchange.Rate.Changes
78 Change.In.Cash.and.Cash.Equivalents
79 Trade.Time
80 Name
81 Volume
82 P.E.Ratio
83 Dividend.Yield
84 Shares.Outstanding
Fundamentals
85 Price.Book
86 date
87 adjusted

Which is quite a lot of information. I like this method of calculating financial data since dplyr and the pipe function makes it intuitive to read and we can see what we are calculating.

Some basic financial ratios:

############################## Create some financial ratios ######################

ratios <- financials %>%
  mutate(BvE = Total.Assets - Total.Liabilities,
         BvE_per_share = BvE / (Shares.Outstanding / 1000),
         EBITDA = Earnings.Before.Interest.and.Taxes + Depreciation,
         Market_Cap = adjusted * (Shares.Outstanding / 1000),                 #¡Cuidado!
         EV = (Market_Cap + Total.Liabilities) - Cash.And.Cash.Equivalents,
         Mkt_Book = EV /(Total.Assets - Cash.And.Cash.Equivalents),
         Price_Book = Market_Cap / Total.stockholders..equity,
         Price_Sales = Market_Cap / Total.Revenue,
         PE_Ratio = Market_Cap / Net.Income,
         EV_EBITDA =  EV / EBITDA,
         NI_REV = Net.Income / Total.Revenue,
         Rev_TA = Total.Revenue / Total.Assets,
         TA_BvE = Total.Assets / BvE,
         ROE = NI_REV * Rev_TA * TA_BvE * 100) %>%
  select(symbol, date, BvE, BvE_per_share, EBITDA, Market_Cap, EV, Mkt_Book, Price_Book, Price_Sales,
         PE_Ratio, EV_EBITDA, NI_REV, Rev_TA, TA_BvE, ROE)
Table 9: Financial Ratios
symbol date BvE BvE_per_share EBITDA Market_Cap EV Mkt_Book Price_Book Price_Sales PE_Ratio EV_EBITDA NI_REV Rev_TA TA_BvE ROE
BIDU 2019-09-25 175752000 504.25059 33317000 35830015 130006015 0.4816322 0.219955 0.3449273 1.29946 3.902093 0.2654389 0.3490889 1.693102 15.68858
MSFT 2019-09-25 82718000 10.83347 44958000 1064070734 1228254734 4.9746650 12.863835 9.6418153 64.21283 27.320048 0.1501540 0.4263506 3.129283 20.03312
GOOG 2019-09-25 177628000 511.38783 40421000 432972486 471435486 2.1816526 2.437524 3.1645640 14.08682 11.663133 0.2246472 0.5877307 1.310559 17.30358
AMZN 2019-09-25 43549000 88.03896 27762000 874715023 962064023 7.3497229 20.085766 3.7559633 86.83759 34.653988 0.0432527 1.4318467 3.734827 23.13027
AAPL 2019-09-25 107147000 23.70939 81801000 998874301 1231539301 3.6241784 9.322466 3.7608927 16.77906 15.055309 0.2241420 0.7262151 3.413301 55.56012

We can check if the calculations are somewhat correct by comparing them to Yahoo Finance statistics. I select only a few ratios which we were able to get from the Yahoo statistics page.

stats %>%
  left_join(ratios, by = c("symbol")) %>%
  select(symbol, Name, P.E.Ratio, PE_Ratio, Price.Book, Price_Book) %>%
  kable(caption = "Price Earnings, Price Book ratios") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), font_size = 12)
Table 10: Price Earnings, Price Book ratios
symbol Name P.E.Ratio PE_Ratio Price.Book Price_Book
AAPL Apple Inc.  18.767937 16.77906 10.383821 9.322466
AMZN Amazon.com, Inc.  73.362510 86.83759 16.463211 20.085766
GOOG Alphabet Inc.  25.164429 14.08682 4.501470 2.437524
MSFT Microsoft Corporation 27.541502 64.21283 10.408545 12.863835
BIDU Baidu, Inc.  8.009974 1.29946 1.476227 0.219955

The ratios seem somewhat similar. However GOOG, MSFT and BIDU PE Ratio looks a little off. The same for BIDU’s Price_Book ratio. The problem here is that the adjusted stock price for BIDU is in USD however the financials are in CNY, so we have to be careful with foreign companies. I did develop a model which scrapes the currency information also and makes the conversion and we can better compare companies in different markets, which I will leave for another post.

Two things here:

  1. Yahoo uses a number of different sources for their statistics, just check out their Footnotes side bar for Google here. They use Morningstar, Thomson Reuters, EDGAR and CapitalIQ.

  2. The Shares Outstanding information will not be the same to the one we use. Yahoo published the more recent Shares Outstanding values but their PE ratios might use December 2018 - Net Income along with December 2018 - Market Cap, taking the Shares Outstanding and adjusted prices from December 2018. However we use September 2019 values! Historical Shares Outstanding information can be found here for Google

We can plot the performance of each stock to see how they did over the past year.

symbols %>%
  tq_get(get = "stock.prices",
         from =  "2018-12-31",
         to =  Sys.Date()) %>%
  select(symbol, date, adjusted) %>%
  group_by(symbol) %>%
  arrange(desc(date)) %>%
  ggplot(aes(x = date, y = adjusted))+
  geom_line() +
  facet_wrap(~ symbol, ncol = 2, scales = "free") +
  theme_tq() +
  ggtitle("Stock Prices since December 2018")

Also using fundamental data:

financials %>%
  ggplot(aes(x = symbol)) +
  geom_col(aes(y = Total.Assets, color = "blue", fill = "blue", alpha = 0.5)) +
  geom_col(aes(y = Total.Liabilities, color = "red", fill = "red", alpha = 0.5)) +
  ggtitle("Total Assets & Total Liabilities") +
  xlab("Symbols") +
  ylab("Total Assets & Total Liabilities") +
  theme_tq() +
  theme(legend.position = "none")

Any errors are my own, if you find any please let me know!

Avatar
Matthew Smith
Researcher in Dept Finance

I am a researcher with a focus on Machine Learning methods applied to economics and finance.

comments powered by Disqus