-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathREADME.Rmd
206 lines (164 loc) · 8.4 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# equityanalysis
<!-- badges: start -->
[![Travis build status](https://travis-ci.org/Ljupch0/equityanalysis.svg?branch=master)](https://travis-ci.org/Ljupch0/equityanalysis)
<!-- badges: end -->
The equityanalysis package simplifies the download and analysis of financial statement data. The data source is yahoo finance.
## Installation
You can install the released version of equityanalysis from [Github](https://github.com/Ljupch0/equityanalysis) with:
``` r
devtools::install_github("ljupch0/equityanalysis")
library(equityanalysis)
```
## Introduction
The functions **getIncome**, **getBalanceSheet** and **getCashFlow** scrape the respective financial statements for a single stock ticker. **getFinancials** combines these functions and applies them over a vector of stock tickers.
The resulting dataframes are available in 3 formats. The "raw" format presents the data as it usually would be seen as a table in a financial report, and thus is best for readability. The "clean" format keeps that readable structure, but removes empty grouping rows, and converts missing data to NA and numbers to numeric. Finally, `format = "tidy"` pivots the data so that every statement item is a variable, and every report is an observation, and also shortens the variable names for ease of use. Most importantly, "tidy" format makes the data ready to be analyzed and plotted with the tidyverse and beyond, and as such is the defaut format.
The resulting dataframes are by default assigned to the envirnoment with succint object names. However, by setting `assign = FALSE` the user is free to have only the results returned and assigned to the environment with a name of their choice.
**getFinancials** is the most general function, which can take a vector of tickers and returns a tidy dataframe containing all the income statement, balance sheet, and cash flow data from the latest 4 yearly reports.
## Examples
Libraries needed for these examples
```{r}
library(equityanalysis)
library(dplyr)
library(ggplot2)
```
To get an income statement that can be read through like a table, use **getIncome** with `format = "raw"`.
```{r fig.width=5, fig.height=5}
getIncome(ticker="AAPL", format = "raw", assign = FALSE)
```
However to get a report that is easier to work with in R, we can use the `format = "tidy"` and `assign = TRUE` defaults and just provide the company ticker. In the resulting dataframe we can filter a for a single yearly report and calculate the EBITDA margin, for instance.
```{r}
getIncome("AAPL")
aapl_income %>%
dplyr::filter(year == "2019") %>%
transmute(
ebitda_margin = ebitda / revenue
)
```
To get all financial data for a group of stocks, we can provide a vector of tickers to **getFinancials**. Note that getFinancials only returns tidy data and has no format argument. With the resulting "financials" dataframe we can calculate profitability ratios using the shortened variable names, and can plot the results.
```{r}
getFinancials(c("FB", "AMZN", "AAPL", "GOOG"))
profit_margins <- financials %>%
dplyr::filter(year=="2016" | year == "2017" | year=="2018") %>% #AAPL has its 2019 report out, others do not.
mutate(
gross_margin = gross_profit / revenue,
ebitda_margin = ebitda / revenue,
ebit_margin = ebit / revenue,
net_profit_margin = ni / revenue,
cash_flow_margin = operating_cf / revenue
)
ggplot(data = profit_margins)+
geom_bar(mapping = aes(x = year, y=ebit_margin, fill=reorder(ticker, ebit_margin)),
stat="identity",
position = "dodge")+
geom_text(mapping = aes(x = year, y=ebit_margin, fill=reorder(ticker, ebit_margin), label=round(ebit_margin,2)),
position = position_dodge(width = 0.9),
vjust = -0.3)+
scale_fill_manual("legend", values = c("FB" = "#3b5b9a", "AMZN" = "#ff9900", "GOOG" = "#4285F4", "AAPL" = "#e5e5e5"))+
labs(title = "EBIT Margins for FAAG Stocks", x ="Year")+
theme(
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect("white"),
plot.title = element_text(hjust = 0.5)
)
```
## Data
The full list of data available and variable name shortcuts.
Income Statement:
"revenue"=`Total Revenue`,
"cost_revenue" = `Cost of Revenue`,
"gross_profit" = `Gross Profit`
"rd" = `Research Development`,
"sga" = `Selling General and Administrative`,
"operating_expenses" = `Total Operating Expenses`,
"ebit" = `Operating Income or Loss`,
"interest_expense" = `Interest Expense`,
"other_income_net" = `Total Other Income/Expenses Net`,
"ebt" = `Income Before Tax`,
"tax" = `Income Tax Expense`,
"income_operations" = `Income from Continuing Operations`,
"ni" = `Net Income`,
"ni_to_shareholders" = `Net Income available to common shareholders`,
"basic_eps" = `Basic EPS`,
"diluted_eps" = `Diluted EPS`,
"basic_w_avg_shares" = `Basic W. Average Shares Outstanding`,
"diluted_w_avg_shares" = `Diluted W. Average Shares Outstanding`,
"ebitda" = `EBITDA`
Balance Sheet:
"cash_equivalents" = `Cash And Cash Equivalents`,
"st_investments" = `Short Term Investments`,
"total_cash" = `Total Cash`,
"net_receivables" = `Net Receivables`,
"inventory" = `Inventory`,
"other_ca" = `Other Current Assets`,
"total_ca" = `Total Current Assets`,
"gross_ppe" = `Gross property, plant and equipment`,
"accumulated_depreciation" = `Accumulated Depreciation`,
"net_ppe" = `Net property, plant and equipment`,
"equity_other_investments" = `Equity and other investments`,
"goodwill" = `Goodwill`,
"intangibles" = `Intangible Assets`,
"other_lta" = `Other long-term assets`,
"total_lta" = `Total non-current assets`,
"total_assets" = `Total Assets`,
"total_revenue" = `Total Revenue`,
"payables" = `Accounts Payable`,
"taxes_payable" = `Taxes payable`,
"accrued_liabilities" = `Accrued liabilities`,
"other_cl" = `Other Current Liabilities`,
"total_cl" = `Total Current Liabilities`,
"lt_debt" = `Long Term Debt`,
"deferred_taxes" = `Deferred taxes liabilities`,
"deferred_revenue_lt" = `Deferred revenues`,
"other_ltl" = `Other long-term liabilities`,
"total_ltl" = `Total non-current liabilities`,
"total_liabilities" = `Total Liabilities`,
"common_stock" = `Common Stock`,
"retained_earnings" = `Retained Earnings`,
"accumulated_other_income" = `Accumulated other comprehensive income`,
"total_equity" = `Total stockholders' equity`,
"total_liabilities_equity" = `Total liabilities and stockholders' equity`
Cash Flow Statement:
"ni" = `Net Income`,
"da" = `Depreciation & amortization`,
"deferred_taxes_change"=`Deferred income taxes`,
"stock_comp" = `Stock based compensation`,
"working_capital_change" = `Change in working capital`,
"receivables_change" = `Accounts receivable`,
"inventory_change" = `Inventory`,
"payables_change" = `Accounts Payable`,
"other_working_capital" = `Other working capital`,
"other_non_cash" = `Other non-cash items`,
"operating_cf" = `Net cash provided by operating activites`,
"ppe_investment" = `Investments in property, plant and equipment`,
"acquisitions" = `Acquisitions, net`,
"purchases_investments" = `Purchases of investments`,
"sales_investments" = `Sales/Maturities of investments`,
"other_investing" = `Other investing activites`,
"investing_cf" = `Net cash used for investing activites`,
"debt_repayment" = `Debt repayment`,
"stock_issued" = `Common stock issued`,
"stock_repurchased" = `Common stock repurchased`,
"dividends_paid" = `Dividends Paid`,
"other_financing" = `Other financing activites`,
"financing_cf" = `Net cash used privided by (used for) financing activities`,
"change_cash" = `Net change in cash`,
"cash_start_period" = `Cash at beginning of period`,
"cash_end_period" = `Cash at end of period`,
"capex" = `Capital Expenditure`,
"fcf" = `Free Cash Flow`