# R programming language Homework

All the answers need to include the R code used, and not only the answer of the question.
All the questions need to be answered.
All graphs/plots/data generated by R need to be included.
The R code is given, so you just need to use/tweak it in order to answer the questions.
MSF 60600-01 Homework 3 3
Report which seem to be the significant factors. Try to find a smaller model with only significant
terms. Report the coefficient estimates and t-stats. While not normally useful, a high R2
is
also worth noting. Note which factors make sense and why — and which do not make sense.
Report the Sharpe ratio, Sortino ratio, and information ratio for the fund.
1. WFINX
3. EAERX
4. ARBFX
5. GSCI (add oil, natgas, copper, and corn returns)
6. PRNEX (add oil, natgas, copper, and corn returns)
3. (9 points) (Commodities or Posh Spice?) Regress excess returns for the Teucrium ETFs (CORN,
WEAT, SOYB) on commodity returns. See if you can find which commodity futures they perform
like. How close is the performance? Look at the expense ratios of the funds. How do those
compare to the cost of holding futures? Why might these be attractive to some investors?
R Code
library(xts)
library(Quandl)
library(quantmod)
library(PerformanceAnalytics)
# Get constant-maturity (US) Treasuries for basic yield curves
ust.tickers <- c(“FRED/DGS3MO”, “FRED/DGS2”, “FRED/DGS10”, “FRED/DGS30″)
ust <- Quandl(ust.tickers, type=”xts”)/100
ust.colnames <- c(“UST3M”, “UST2Y”, “UST10Y”, “UST30Y”)
colnames(ust) <- ust.colnames
ted <- Quandl(“FRED/TEDRATE”, type=”xts”)/100
colnames(ted) <- “TED”
index.symbols <- c(“^GSPC”, “^RUT”, “^VIX”)
index.prices <- getSymbols(index.symbols, source=”yahoo”,
for (i in 2:length(index.symbols)) {
index.tmp <- getSymbols(index.symbols[i], source=”yahoo”,
index.prices <- cbind(index.prices, index.tmp)
}
colnames(index.prices) <- c(“SPX”, “RUT”, “VIX”)
index.rets <- diff(log(index.prices))
idxfund.symbols <- c(“VFINX”, “VOO”, “SPY”, “AFDAX”, “PTMAX”, “PINVX”,
“RYSOX”, “SAMKX”, “SOAVX”)
idxfund.prices <- getSymbols(idxfund.symbols, source=”yahoo”,
for (i in 2:length(idxfund.symbols)) {
idxfund.tmp <- getSymbols(idxfund.symbols[i], source=”yahoo”,
idxfund.prices <- cbind(idxfund.prices, idxfund.tmp)
}
MSF 60600-01 Homework 3 4
colnames(idxfund.prices) <- idxfund.symbols
idxfund.rets <- diff(log(idxfund.prices))
fund.symbols <- c(“WFINX”, “NLY”, “EAERX”, “ARBFX”, “PRNEX”)
fund.prices <- getSymbols(fund.symbols, source=”yahoo”,
for (i in 2:length(fund.symbols)) {
fund.tmp <- getSymbols(fund.symbols[i], source=”yahoo”,
fund.prices <- cbind(fund.prices, fund.tmp)
}
colnames(fund.prices) <- fund.symbols
fund.rets <- diff(log(fund.prices))
commfund.symbols <- c(“CORN”, “WEAT”, “SOYB”)
commfund.prices <- getSymbols(commfund.symbols, source=”yahoo”,
for (i in 2:length(commfund.symbols)) {
commfund.tmp <- getSymbols(commfund.symbols[i], source=”yahoo”,
commfund.prices <- cbind(commfund.prices, commfund.tmp)
}
colnames(commfund.prices) <- commfund.symbols
commfund.rets <- diff(log(commfund.prices))
## Now get the commodity returns
comm.symbols <- c(“CHRIS/CME_GI1”, “CHRIS/CME_CL1”, “CHRIS/CME_NG1”, “CHRIS/CME_HG1”,
“CHRIS/CME_C1”, “CHRIS/CME_W1”, “CHRIS/CME_S1”)
settle <- “Settle” # settle field is labeled
comm.prices <- Quandl(comm.symbols, type=”xts”)[,settle]
for (i in 2:length(comm.symbols)) {
comm.tmp <- Quandl(comm.symbols[i], type=”xts”)[,settle]
comm.prices <- cbind(comm.prices, comm.tmp)
}
comm.colnames <- c(“GSCI”, “crude”, “natgas”, “copper”, “corn”, “wheat”, “beans”)
colnames(comm.prices) <- comm.colnames
comm.rets <- diff(log(comm.prices))
alldata <- cbind(index.rets, idxfund.rets, fund.rets,
commfund.rets, comm.rets)[“2010/”]
## Now create excess returns
columns2change <- colnames(alldata)
alldata.xs <- alldata[,columns2change] – rf
for (i in 2:length(columns2change)) {
alldata.xs <- cbind(alldata.xs, alldata[,columns2change[i]] – rf)
}
colnames(alldata.xs) <- columns2change
alldata.xs <- na.omit(alldata.xs)
alldata.xs\$UST3M <- ust\$UST3M[“2010/”]
alldata.xs\$UST30Y <- ust\$UST30Y[“2010/”]
alldata.xs\$TED <- ted\$TED[“2010/”]
alldata.xs\$YCSLOPE <- alldata.xs\$UST30Y – alldata.xs\$UST3M
alldata.xs\$VIX <- index.prices\$VIX[“2010/”]
## calculate the Sharpe, Sortino, and information ratio for each fund
by.column = 2
daily.risk.premium <- apply(alldata.xs, by.column, mean, na.rm=TRUE)
daily.volatility <- apply(alldata.xs, by.column, sd, na.rm=TRUE)
daily.semideviation <- apply(alldata.xs, by.column, SemiDeviation, na.rm=TRUE)
MSF 60600-01 Homework 3 5
# compute Sharpe ratio like so: (You need to compute annualized numbers) 