::opts_chunk$set(echo = TRUE,fig.align = "center",
knitrmessage = FALSE,
warning = FALSE)
options(tinytex.verbose = TRUE)
library(tidymodels)
library(tidyquant)
library(tidyverse)
library(scales)
library(kableExtra)
library(lubridate)
library(ggplot2)
library(plotly)
library(timetk)
library(modeltime)
library(DataExplorer)
library(correlationfunnel)
#The real estate sector generally includes two different types of investments related to real estate. Some stocks in the sector are responsible for developing new real estate projects and then managing them by obtaining tenants for various spaces within the project property. In addition, most real estate investment trusts, which are special tax-favored business entities that operate in various areas of the real estate industry, get counted as within the real estate sector.
#tickers
<- c("WELL","WY","LPX","DRE","PEAK") %>% sort()
symbols # prices
# stock prices
<- symbols %>%
prices tq_get(get = "stock.prices", from = "1990-01-01")
# returns
<- prices %>%
returns group_by(symbol) %>%
tq_transmute(
select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
%>%
) ungroup() %>%
mutate(date = rollback(date, roll_to_first = TRUE))
returns
%>%
returns group_by(symbol) %>%
ggplot(aes(date, monthly.returns, color = symbol)) +
geom_line() + scale_y_continuous(labels = scales::percent) +
theme_tq() + facet_wrap(~symbol) +
labs(
title = "Monthly returns",
subtitle = "Real Estate Sector",
x = "",
y = "Monthly Returns"
)
#timetk
%>%
returns plot_time_series(
.date_var = date,
.value = monthly.returns,
.facet_vars = symbol,
.color_var = symbol,
.facet_ncol = 2,
.title = "Returns of Real Estate Sector Stocks",
.x_lab = "",
.y_lab = "Monthly Returns",
.smooth_size = 0.5
)
#Stock Prices Visualization
%>%
prices group_by(symbol) %>%
plot_time_series(
.date_var = date,
.value = adjusted,
.color_var = symbol,
.facet_ncol = 2,
.facet_scales = "free_y",
.smooth = FALSE,
.title = "Stock Prices Real Estate Sector",
.x_lab = "",
.y_lab = "Adjusted Prices",
.legend_show = FALSE
)
#timetk
%>%
prices plot_time_series(
.date_var = date,
.value = adjusted,
.facet_vars = symbol,
.color_var = symbol,
.facet_ncol = 2,
.title = "Stock Prices Real Estate Sector",
.x_lab = "",
.y_lab = "Adjusted Stock Prices($)",
.smooth = FALSE
)
#Portfolio Construction
# Weights Allocation
<- rep(1/5, 5)
w # Portfolio creation
<- returns %>%
port_ret_tbl tq_portfolio(
assets_col = symbol,
returns_col = monthly.returns,
weights = w
%>%
) mutate(date = rollback(date, roll_to_first = TRUE)) %>%
add_column(symbol ="Portfolio", .before = 1)
#Visualization
%>%
port_ret_tbl ggplot(aes(date, portfolio.returns)) +
geom_line() + geom_smooth(method = "loess") +
scale_y_continuous(labels = scales::percent) + theme_tq() + scale_color_tq() +
labs(
title = "Portfolio Returns",
subtitle = "Real Estate Sector",
y = "Returns",
x = ""
)
#timetk
%>%
port_ret_tbl plot_time_series(
.date_var = date,
.value = portfolio.returns,
.title = "Portfolio Returns Real Estate Sector",
.x_lab = "",
.y_lab = "Returns"
)
#S&P500
<- tq_get("^GSPC", get = "stock.prices",from = "1990-01-01")
market_prices <- market_prices %>%
market_returns tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "market_ret") %>%
mutate(date = rollback(date, roll_to_first = TRUE))
market_returns
#FAMA-FRENCH FACTORS
library(frenchdata)
<- download_french_data("Fama/French 3 Factors")
factors_ff_monthly_raw <- factors_ff_monthly_raw$subsets$data[[1]] |>
factors_ff_monthly transmute(
date = floor_date(ymd(str_c(date,"01")),"month"),
rf = as.numeric(RF) / 100,
mkt_excess = as.numeric(`Mkt-RF`) / 100,
smb = as.numeric(SMB) / 100,
hml = as.numeric(HML) / 100
%>%
) # filter the dates
filter(date >= "1990-01-01")
factors_ff_monthly
#Combining all the data
<- port_ret_tbl %>%
port_risk_market_tbl left_join(factors_ff_monthly, by = "date") %>%
left_join(market_returns, by = "date") %>%
mutate(excess_returns = portfolio.returns - rf) %>%
mutate(market_excess_returns = market_ret - rf) %>%
select(symbol, date, mkt_excess, smb, hml, excess_returns, market_excess_returns)
port_risk_market_tbl
#Beta Estimation ## Step1: CAPM Function
library(slider)
# estimate capm
<- function(data, min_obs = 1) {
estimate_capm if (nrow(data) < min_obs) {
<- as.numeric(NA)
beta else {
} <- lm(excess_returns ~ mkt_excess + smb+hml+ market_excess_returns, data = data)
fit <- as.numeric(fit$coefficients[2])
beta
}return(beta)
}# rolling capm estimation
<- function(data, months, min_obs) {
roll_capm_estimation <- bind_rows(data) |>
data arrange(date)
<- slide_period_vec(
betas .x = data,
.i = data$date,
.period = "month",
.f = ~estimate_capm(., min_obs),
.before = months - 1,
.complete = FALSE
)
tibble(
month = unique(data$date),
beta = betas
) }
##Step2: Estimation
<- port_risk_market_tbl %>%
beta_port_tbl mutate(roll_capm_estimation(cur_data(), months = 60, min_obs = 48)) %>%
drop_na() %>%
select(symbol, date, beta)
beta_port_tbl
##Step3: Beta Visualization
%>%
beta_port_tbl ggplot(aes(date, beta)) +
geom_line() + geom_smooth(method = "loess") +
scale_y_continuous() + theme_tq() + scale_color_tq() +
labs(
title = "Rolling Beta using 5 years data",
subtitle = "Real Estate Sector",
caption = "Stocks(DRE, LPX, PEAK, WELL, WY)",
x ="",
y="Beta"
)
#timetk
%>%
beta_port_tbl plot_time_series(
.date_var = date,
.value = beta,
.title = "Rolling Beta Estimates Real Estate Sector",
.x_lab = "",
.y_lab = "Beta"
)
# combining the data
<- returns %>%
returns_risk_market_tbl group_by(symbol) %>%
left_join(factors_ff_monthly, by = "date") %>%
left_join(market_returns, by = "date") %>%
mutate(excess_returns = monthly.returns -rf) %>%
mutate(market_excess_returns = market_ret - rf) %>%
ungroup() %>%
drop_na() %>%
select(symbol, date, excess_returns, market_excess_returns, mkt_excess, smb, hml)
returns_risk_market_tbl
##Beta Estimation
<- returns_risk_market_tbl %>%
beta_stocks_tbl group_by(symbol) %>%
mutate(roll_capm_estimation(cur_data(), months = 60, min_obs = 48)) %>%
ungroup() %>%
drop_na() %>%
select(symbol, date, beta)
beta_stocks_tbl
##Beta Visualization
%>%
beta_stocks_tbl ggplot(aes(date, beta, color = symbol)) +
geom_line() + scale_y_continuous() +
theme_tq() +
labs(
title = "Rolling beta with 5 years of data",
subtitle = "Real Estate Sector ",
capition = "stocks(DRE, WELL,WY, LPX, PEAK)",
y ="Beta",
x =""
)
###timetk
%>%
beta_stocks_tbl plot_time_series(
.date_var = date,
.value = beta,
.color_var = symbol,
.smooth = FALSE,
.title = "Rolling Beta for Stocks (Real Estate Sector)",
.x_lab = "",
.y_lab = "Beta"
)