Retrieving Specific Financial Statements in Quantmod - quantmod

I am currently working in the quantmod package and wish to retrieve some financial statements. I am having an issue specifying what type of financial statement I want. By default it retrieves the Annual BS.
tickers <-new.env()
s <-c(list of tickers...)
lapply(s, getFinancials, env=tickers)
FS <-data.frame(lapply(tickers, viewFinancials)
When I do attempt to specify the FS, it gives me the error message of either saying 'x' must be type of financials or since I"m using lapply it won't recognize it as a function. I do like using lapply for this because it puts the financial statements in a data frame and in the exact format that I like, I just want to do it for the Annual IS.
Thank you!

This code works and not only includes the BS, but the IS and CF as well:
Input:
tickers <-new.env()
t <-c("AAL", "AAME", "AAOI")
lapply(t, getFinancials, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
Output:
> tickers <-new.env()
> t <-c("AAL", "AAME", "AAOI")
> lapply(t, getFinancials, env=tickers)
[[1]]
[1] "AAL.f"
[[2]]
[1] "AAME.f"
[[3]]
[1] "AAOI.f"
> BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
Annual Balance Sheet for AAME
Annual Balance Sheet for AAL
Annual Balance Sheet for AAOI
> IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
Annual Income Statement for AAME
Annual Income Statement for AAL
Annual Income Statement for AAOI
> CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
Annual Cash Flow Statement for AAME
Annual Cash Flow Statement for AAL
Annual Cash Flow Statement for AAOI
?viewFin or ?viewFinancials would have shown you the possible options/arguments for viewFinancials.

Related

Trying to forecast the next 24hrs temperature using UCI repository datasets in R programming

Hi I accessed the datasets from the UCI repository http://archive.ics.uci.edu/ml/datasets/Air+Quality
I am trying to predict the next 24hrs temperature.Below is the code which I have written
filling the missing values by NA
library(plyr)
AirQualityUCI[AirQualityUCI==-200.0]<-NA
Replacing the NA by mean of each columns
for(i in 1:ncol(AirQualityUCI)){
AirQualityUCI[is.na(AirQualityUCI[,i]),i] <- mean(AirQualityUCI[,i], na.rm = TRUE)
}
plot time series
plot(AirQualityUCI$T, type = "l")
How do I set the frequency in hours and predict the temperature of next 24hrs ?
Tempts <- ts(AirQualityUCI)
Temprforecasts <- HoltWinters(Tempts, beta=FALSE, gamma=FALSE)
library(forecast)
accuracy(Temprforecasts,24)
Getting the below error
Error in attr(x, "tsp") <- value :
invalid time series parameters specified
library(readxl)
AirQualityUCI <- read_excel("AirQualityUCI.xlsx")
library(plyr)
AirQualityUCI[AirQualityUCI==-200.0]<-NA
#First, limit to the one column you are interested in (make sure data is sorted by time variable before doing this)
library(data.table)
temp <- setDT(AirQualityUCI)[,c("T")]
#Replace NA with mean
temp$T <- ifelse(is.na(temp$T), mean(temp$T, na.rm=TRUE), temp$T)
#Create time series object...in this case freq = 365 * 24 (hours in year)
Tempts <- ts(temp, frequency = 365*24)
#Model
Temprforecasts <- HoltWinters(Tempts, beta = FALSE, gamma = FALSE)
#Generate next 24 hours forecast
library(forecast)
output.forecast <- forecast.HoltWinters(Temprforecasts, h = 24)

R: Arguments imply differing number of rows using financial data

I am intending to build a code to check the cointegration of two time series of financial price data in order to make better forecasting of one of them.
For this purpose, I chose this two time series: BBVA historical price and IBEX35 and build this little code:
ibex <- new.env()
bbva <- new.env()
library(quantmod)
getSymbols("^IBEX", env = ibex, src = "yahoo", from = as.Date("2010-01-04"), to = as.Date("2015-11-30"))
getSymbols("bbva", env = bbva, src = "yahoo", from = as.Date("2010-01-04"), to = as.Date("2015-11-30"))
ibex <- ibex$IBEX
ibex <- ibex$IBEX.Adjusted
bbva <- bbva$BBVA
bbva <- bbva$BBVA.Adjusted
ldbbva <- diff(log(bbva))
ldibex <- diff(log(ibex))
mean <- mean(ldbbva, na.rm = TRUE)
ldbbva[is.na(ldbbva)] <- mean
mean <- mean(ldibex, na.rm = TRUE)
ldbbva[is.na(ldibex)] <- mean
library(urca)
jotest=ca.jo(data.frame(ldbbva,ldibex), type="trace", K=2, ecdet="none", spec="longrun")
At that point when I try to make a data frame from my time series, I face this error arguments imply differing number of rows: 1488, 1514.
What can I do?
You need to join the time series to have them properly aligned.
dat <- merge(ibex, bbva)
dat <- diff(log(dat))
#mean imputation
dat <- na.aggregate(dat)
library(urca)
jotest=ca.jo(dat, type="trace", K=2, ecdet="none", spec="longrun")
######################################################
## Johansen-Procedure Unit Root / Cointegration Test #
######################################################
#
#The value of the test statistic is: 619.1603 1473.644

R calculating a stock's beta (using PerformanceAnalytics CAPM.beta() function or lm() function producing unexpected results)

I am trying to quantify a stock's beta (bench marked vs. SPY) in R using the PerformanceAnalytics CAPM.beta() function and the results aren't even close to the values I am seeing online at Yahoo/Google Finance. The code:
require(PerformanceAnalytics)
start_date <- "2013-08-24"
acad <- getSymbols("ACAD", from = start_date, auto.assign = F)
spy <- getSymbols("SPY", from = start_date, auto.assign = F)
CAPM.beta(acad[,6], spy[,6])
For the above example, Yahoo/Finviz/Google all list ACAD's beta at 2.6 to more than 3.0. While I am not sure what the lookback period is for each site, changing the value in the above code produces a beta value of less than 1 for 1,2,3 yr lookbacks.
Similarly, by trying to calculate the beta using lm(), I am getting ie 0.39 beta for ACAD ~ SPY 2 year lookback:
m <- lm(acad[,6] ~ spy[,6] + 0)
beta <- coef(m)[1]
beta
what am I missing?
Beta is calculated in terms of returns, often montly. You do want the intercept term (alpha) in the model fit when using lm.
start_date <- "2012-07-01"
acad <- getSymbols("ACAD", from = start_date, auto.assign = F)
spy <- getSymbols("SPY", from = start_date, auto.assign = F)
r<-function(x) {m<-to.monthly(x[,6])[,4];diff(m)/lag(m)}
coef(lm(r(acad)[2:37]~r(spy)[2:37]))
#> (Intercept) r(spy)[2:37]
#> 0.08601629 2.62485092
Beta calculated for 36 months of adjusted month-end close is about 2.6 in this case.

How can I preserve xts format for fitted data after regression?

I did a regression on a time series data in xts format
> inputfit <- lm(y_t ~ y_tminus1
+ cpi_t + cpi_tminus1 +
m1_t + m1_tminus1 +
ip_t + ip_tminus1, data = RegData)
> class(RegData)
[1] "xts" "zoo"
class(fitted(inputfit))
[1] "numeric"
lm() preserves "ts" format for fitted data but doesn't do the same for "xts" format. Why is that? How can I overcome this problem? Is there already a package available?
The other option is that I coerce the fitted data to xts but I don't know how to do that either. My fitted data looks like this
> head(fitted(inputfit))
Aug 2001 Sep 2001 Oct 2001 Nov 2001 Dec 2001 Jan 2002
3.534743 3.285140 2.598543 2.271459 1.902562 1.712419
How can I coerce this data to an xts format?
Edit 1:
Here is the code to make it reproducible:
require("Quandl")
library("zoo")
fromDate = "2001-01-01"
toDate = "2013-12-31"
## Macroeconomic Data
CPI_Data = Quandl("FRED/CPIAUCSL", start_date = fromDate, end_date = toDate, type = "xts")
IP_Data = Quandl("FRED/INDPRO", start_date = fromDate, end_date = toDate, type = "xts")
M1_Data = Quandl("FRED/M1SL", start_date = fromDate, end_date = toDate, type = "xts")
## Yield Curve Data
# 1 month
GS1M_Data = Quandl("FRED/GS1M", start_date = fromDate, end_date = toDate, type = "xts")
# Taking the lag difference of input data
inputminus1 <- lag(GS1M_Data,1)
# Taking the log Difference of CPI_Data
LOGCPI <- (diff(log(CPI_Data), 1))
#Taking the Lag difference of log of CPI_Data
CPIminus1 <- lag(LOGCPI,1)
#Taking the log Difference of M1_Data
LOGM1 <- (diff(log(M1_Data), 1))
#Taking the Lag difference of log of M1_Data
M1minus1 <- lag(LOGM1,1)
#Taking the log Difference of IP_Data
LOGIP <- (diff(log(IP_Data), 1))
#Taking the Lag difference of log differenced IP_Data
IPminus1 <- lag(LOGIP,1)
#Merging all the above values along with the original input Data
RegData = merge(GS1M_Data,inputminus1,LOGCPI,CPIminus1,LOGM1,M1minus1,LOGIP,IPminus1)
#Removing NAs
RegData = RegData[complete.cases(RegData),]
colnames(RegData) <- c("y_t", "y_tminus1",
"cpi_t", "cpi_tminus1",
"m1_t", "m1_tminus1",
"ip_t", "ip_tminus1")
# Regression
inputfit <- lm(y_t ~ y_tminus1
+ cpi_t + cpi_tminus1 +
m1_t + m1_tminus1 +
ip_t + ip_tminus1, data = RegData)
In my tests, fitted(inputfit) returned the same thing (a named numeric vector) whether I ran lm on a ts object or an xts object. So I'm skeptical that a ts object is really being returned.
Regardless, you can convert the output of fitted(inputfit) back to xts using as.xts:
> head(as.xts(fitted(inputfit), dateFormat="yearmon"))
[,1]
Aug 2001 3.534743
Sep 2001 3.285140
Oct 2001 2.598543
Nov 2001 2.271459
Dec 2001 1.902562
Jan 2002 1.712419
And here's an example people can run if they don't want to sign up for Quandl.
require(quantmod)
fromDate = "2001-01-01"
toDate = "2013-12-31"
## Macroeconomic Data
options(getSymbols.auto.assign=FALSE)
CPI_Data <- getSymbols("CPIAUCSL", from=fromDate, to=toDate, src="FRED")
IP_Data <- getSymbols("INDPRO", from=fromDate, to=toDate, src="FRED")
M1_Data <- getSymbols("M1SL", from=fromDate, to=toDate, src="FRED")
## Yield Curve Data
GS1M_Data <- getSymbols("GS1M", from=fromDate, to=toDate, src="FRED")
## Regression data
RegData <- merge(GS1M_Data, lag(GS1M_Data),
diff(log(CPI_Data)), lag(diff(log(CPI_Data))),
diff(log(M1_Data)), lag(diff(log(M1_Data))),
diff(log(IP_Data)), lag(diff(log(IP_Data))))
colnames(RegData) <- c("y_t", "y_tminus1", "cpi_t", "cpi_tminus1",
"m1_t", "m1_tminus1", "ip_t", "ip_tminus1")
RegData <- RegData[complete.cases(RegData),]
# Regression
inputfit <- lm(y_t ~ y_tminus1 + cpi_t + cpi_tminus1 +
m1_t + m1_tminus1 + ip_t + ip_tminus1, data = RegData)
The dyn package will work with zoo objects (not xts). Just preface lm with dyn$ as shown:
library(dyn)
# create test data
set.seed(123)
z <- zoo(rnorm(10))
class(fitted(dyn$lm(z ~ lag(z))))
## [1] "zoo"

Error when forecasting with midasr (reproducible example included)

The code is self contained, except the datasets which is linked below.
.csv files used in the code, download this first please: https://drive.google.com/?authuser=0#folders/0B1ciW4R5hjUCRFpjQlJKZGFqcVU
library(midasr)
library(zoo)
yvellaregdata <- read.table("~/Desktop/attempt1/ymonthlyjackson.csv", quote="\"")
yvellareg <- ts(yvellaregdata, start=c(2008,7), frequency=12)
xvellareginit <- read.table("~/Desktop/attempt1/xdailyjackson.csv", quote="\"")
xvellaregzoo <- zoo(xvellareg)
xvellareg <- as.numeric(xvellaregzoo) #i had to convert to numeric for it to work
#yvellareg is the monthly y variable
#xvellareg is the daily x variable
betareg <- midas_r(yvellareg ~ mls(yvellareg, 1, 1) + mls(xvellareg, 3:25, 30), start=NULL)
summary(betareg)
#Defining data for forecasting
xdailyfulldataread <- read.table("~/Desktop/attempt1/xdailyfulldatajackson.csv", quote="\"")
xdailyfulldata <- zoo(xdailyfulldataread)
xdailyfulldata <- as.numeric(xdailyfulldata)
ymonthlyfulldataread <- read.table("~/Desktop/attempt1/ymonthlyfulldatajackson.csv", quote="\"")
ymonthlyfulldata <- ts(ymonthlyfulldataread,start=c(2008,7), frequency=12)
fulldata <- list(xx=xdailyfulldata,
yy=ymonthlyfulldata)
insample <- 1:length(yvellareg)
outsample <- (1:length(fulldata$yy))[-insample]
#errorhere
avgf<-average_forecast(list(betareg),
data=fulldata,
insample=insample,
outsample=outsample)
sqrt(avgf$accuracy$individual$MSE.out.of.sample)
Since you already prepared the data with in-sample and full-sample outside of R, there is no need to convert it to time series objects.
Here is the cleaned-up version of your code, which assumes that data files are in R working directory:
library(midasr)
yvellareg <- scan("ymonthlyjackson.csv")
xvellareg <- scan("xdailyjackson.csv")
#yvellareg is the monthly y variable
#xvellareg is the daily x variable
betareg <- midas_r(yvellareg ~ mls(yvellareg, 1, 1) + mls(xvellareg, 3:25, 30), start=NULL)
summary(betareg)
#Defining data for forecasting
xdailyfulldata <- scan("xdailyfulldatajackson.csv")
ymonthlyfulldata <- scan("ymonthlyfulldatajackson.csv")
fulldata <- list(xvellareg=xdailyfulldata,
yvellareg=ymonthlyfulldata)
insample <- 1:length(yvellareg)
outsample <- (1:length(fulldata$yvellareg))[-insample]
#errorhere
avgf<-average_forecast(list(betareg),
data=fulldata,
insample=insample,
outsample=outsample)
sqrt(avgf$accuracy$individual$MSE.out.of.sample)
But this still throws an error, since your data is not conformable. Package midasr expects that each low frequency period has the same number of high frequency periods. In your case this is 30. But we have
> length(xdailyfulldata)
[1] 1230
> length(ymonthlyfulldata)
[1] 42
> 1230/42
[1] 29.28571
Since 42*30=1260 it seems you have more monthly than daily observations. Dropping one monthly observation makes the code run without the errors:
fulldata <- list(xvellareg=xdailyfulldata,
yvellareg=ymonthlyfulldata[-42])
insample <- 1:length(yvellareg)
outsample <- (1:length(fulldata$yvellareg))[-insample]
#errorhere
avgf<-average_forecast(list(betareg),
data=fulldata,
insample=insample,
outsample=outsample)
sqrt(avgf$accuracy$individual$MSE.out.of.sample)
[1] 1.118709

Resources