Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created September 20, 2012 20:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/3758114 to your computer and use it in GitHub Desktop.
Save timelyportfolio/3758114 to your computer and use it in GitHub Desktop.
obviousness of reits
#obviousness of reits?
#get NAREIT data
#I like NAREIT since I get back to 1971
#much easier though to get Wilshire REIT from FRED
#also it is daily instead of monthly
#getSymbols("WILLREITIND",src="FRED") will do this
require(gdata)
reitURL <- "http://returns.reit.com/returns/MonthlyHistoricalReturns.xls"
reitExcel <- read.xls(reitURL,sheet="Index Data",pattern="All REITs",stringsAsFactors=FALSE)
#clean up dates so we can use xts functionality later
datetoformat <- reitExcel[,1]
datetoformat <- paste(substr(datetoformat,1,3),"-01-",substr(datetoformat,5,6),sep="")
datetoformat <- as.Date(datetoformat,format="%b-%d-%y")
reitExcel[,1] <- datetoformat
#######now get the returns and clean up slightly############
require(quantmod)
require(xtsExtra)
#shift colnames over 1
colnames(reitExcel) <- colnames(reitExcel)[c(1,1:(NCOL(reitExcel)-1))]
#get return columns
reitData <- reitExcel[,c(3,24)]
#name columns
colnames(reitData) <- c("AllREITs","EquityREITs")
reitData <- reitData[3:NROW(reitData),]
#erase commas
col2cvt <- 1:NCOL(reitData)
reitData[,col2cvt] <- lapply(reitData[,col2cvt],function(x){as.numeric(gsub(",", "", x))})
#create xts
reitData <- as.xts(reitData,order.by=reitExcel[3:NROW(reitExcel),1])
#add the sp500
getSymbols("SP500",src="FRED")
SP500 <- to.monthly(SP500)[,4]
#get 1st of month to align when we merge
index(SP500) <- as.Date(index(SP500))
#merge REIT and S&p
reitSp500 <- na.omit(merge(reitData,SP500))
reitSp500 <- ROC(reitSp500,n=1,type="discrete")
#make first return 0 instead of NA
reitSp500[1,] <- 0
##################make some charts#########################
require(PerformanceAnalytics)
layout(matrix(c(1,2),nrow=1))
chart.RiskReturnScatter(reitSp500["2000::",],
col=c("steelblue4","steelblue2","gray50"),
main="REITS and the S&P 500 Since 2000",
xlim = c(0,0.35))
chart.RiskReturnScatter(reitSp500["1984::1999",],
col=c("steelblue4","steelblue2","gray50"),
main="REITS and the S&P 500 1984-1999",
xlim = c(0,0.25))
charts.PerformanceSummary(reitSp500["2000::",],
colorset =c("steelblue4","steelblue2","gray50"),
main="REITS and the S&P 500 Since 2000")
charts.PerformanceSummary(reitSp500["1984::1999",],
colorset =c("steelblue4","steelblue2","gray50"),
main="REITS and the S&P 500 1984-1999")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment