bergant / finstr

:chart_with_upwards_trend: Financial statements in R
251 stars 69 forks source link

How to get all the elements contained in the original XBRL? #4

Open ashgreat opened 7 years ago

ashgreat commented 7 years ago

Is there any way we can get more detailed items that are there in the financial statements but not output from your functions? For example, I want to extract information on warranty accruals and warranty claims, which are output in the XBRL list. However, there is no way I can get them from your functions because they cover only high level financial statements.

bergant commented 7 years ago

Yes, for now it doesn't cover other disclosures. But, it is possible to get the data from parsed XBRL directly. In the result returned by xbrlDoAll there are tables:

We have to find concepts related to warrants and join them to context and value:

library(XBRL)
report_url <- "https://www.sec.gov/Archives/edgar/data/320193/000119312514383437/aapl-20140927.xml"

old_o <- options(stringsAsFactors = FALSE)
xbrl_data <- xbrlDoAll(report_url)
options(old_o)

library(dplyr)
library(tidyr)

xbrl_data$element %>% 
  filter(grepl("Warrant",elementId) & # all "Warrant" elements
           type == "xbrli:monetaryItemType") %>% 
  inner_join(xbrl_data$fact, by = "elementId") %>% 
  inner_join(xbrl_data$context, by = "contextId") %>% 
  dplyr::mutate(amount = as.numeric(fact)*10^(as.numeric(decimals))) %>% 
  select(elementId, amount, startDate, endDate, periodType) %>% 
  split(.$periodType)

# Result:

# $duration
#                                        elementId amount  startDate    endDate periodType
# 1         StandardProductWarrantyAccrualPayments   3703 2012-09-30 2013-09-28   duration
# 2         StandardProductWarrantyAccrualPayments   3760 2013-09-29 2014-09-27   duration
# 3         StandardProductWarrantyAccrualPayments   1786 2011-09-25 2012-09-29   duration
# 4 StandardProductWarrantyAccrualWarrantiesIssued   5032 2012-09-30 2013-09-28   duration
# 5 StandardProductWarrantyAccrualWarrantiesIssued   4952 2013-09-29 2014-09-27   duration
# 6 StandardProductWarrantyAccrualWarrantiesIssued   2184 2011-09-25 2012-09-29   duration
# 
# $instant
#                         elementId amount startDate    endDate periodType
# 7  StandardProductWarrantyAccrual   1240      <NA> 2011-09-24    instant
# 8  StandardProductWarrantyAccrual   1638      <NA> 2012-09-29    instant
# 9  StandardProductWarrantyAccrual   2967      <NA> 2013-09-28    instant
# 10 StandardProductWarrantyAccrual   4159      <NA> 2014-09-27    instant