A replication of the ecological models per Hierarchical generalized additive models in ecology: an introduction with mgcv using stock data.
Load the required packages and the standard data set for analysis
library(romerb)
library(mgcv)
library(gratia)
data("stock_data")
<- stock_data
fundamental_raw rm(stock_data)
# Data
<- fundamental_raw[fundamental_raw$date_stamp == as.Date('2021-06-30'), ]
df $log_mkt_cap <- log(df$mkt_cap)
df$log_book <- log(-df$total_equity_cln)
df$roe <- df$roe * -1
df<- df[df$date_stamp == as.Date('2021-06-30'), c('symbol','sector','log_book','log_mkt_cap','log_pb','roe','leverage')] df
Using the factor smooth basis.
# Model
<- gam(
gam_mlm1 ~ s(roe, k = 5, m = 2) + s(roe, sector, k = 5, m = 2, bs = "fs"),
log_pb data = df,
method = "REML"
)
# Predict
<- predict(gam_mlm1, se.fit = TRUE)
gam_mlm1_pred $pred <- gam_mlm1_pred$fit
df$sepred <- gam_mlm1_pred$se.fit
df
# Visualise
ggplot(data = df, aes(x = roe, y = log_pb, group = sector)) +
facet_wrap(~reorder(sector, as.numeric(sector)), ncol = 4, scales = 'free') +
geom_ribbon(aes(ymin = pred - 2 * sepred,
ymax = pred + 2 * sepred), alpha=0.25) +
geom_line(aes(y = pred)) +
geom_point(alpha = 0.3) +
labs(x = 'Return on equity',
y = 'Log price / book ratio')
# Model
<- gam(
gam_mlm2 ~ s(roe, leverage, bs = "tp", k = 5, m = 2), #+ s(roe, sector, k = 5, m = 2, bs = "fs"),
log_pb data = df,
method = "REML"
)
# Predict
<- 30
interval <- with(df, seq(min(roe), max(roe), length = interval))
roe <- with(df, seq(min(leverage), max(leverage), length = interval))
leverage
<- expand.grid(roe = roe, leverage = leverage)
new_data
<- matrix(predict(gam_mlm2, new_data), interval, interval) #, type = "response"
gam_mlm2_pred
# Visualise
# https://bikeactuary.com/datasci/gams_scams_pt1
persp(
x = leverage,
y = roe,
zlab = 'log_pb',
theta = 225, phi = 20, col = "blue", ticktype = "detailed") gam_mlm2_pred,
Using a tensor product smooth. Modelling market capitalisation on return on equity and book value of equity.
# Model
<- gam(
gam_te1 ~ te(roe, log_book, bs=c("cc", "tp"), k=c(10, 10)),
log_mkt_cap data = df, method = "REML"
)
# Visualise
::draw(gam_te1) gratia
Same model, visualise with a 3d plot.
# Predict
<- 30
interval <- with(df, seq(min(roe), max(roe), length = interval))
roe <- with(df, seq(min(log_book), max(log_book), length = interval))
log_book
<- expand.grid(roe = roe, log_book = log_book)
new_data
<- matrix(predict(gam_te1, new_data), interval, interval)
gam_te1_pred
# Visualise
persp(
x = log_book,
y = roe,
zlab = 'log_mkt_cap',
theta = 45, phi = 20, col = "blue", ticktype = "detailed") gam_te1_pred,
Modelling price to book ratio on return on equity and leverage.
<- gam(
gam_te2 ~ te(roe, leverage, bs=c("cc", "tp"), k=c(10, 10)),
log_pb data = df, method = "REML"
)
::draw(gam_te2) gratia
Same model, visualise with a 3d plot.
# Predict
<- 30
interval <- with(df, seq(min(roe), max(roe), length = interval))
roe <- with(df, seq(min(leverage), max(leverage), length = interval))
leverage
<- expand.grid(roe = roe, leverage = leverage)
new_data
<- matrix(predict(gam_te2, new_data), interval, interval)
gam_te2_pred
# Visualise
persp(
x = leverage,
y = roe,
zlab = 'log_pb',
theta = 225, phi = 20, col = "blue", ticktype = "detailed") gam_te2_pred,
Once again using plotly
.
library(plotly)
<- plot_ly(z = gam_te2_pred, type = "surface")
p1 p1
3d plots with color
https://stackoverflow.com/questions/22652941/how-to-add-colorbar-with-perspective-plot-in-r https://stackoverflow.com/questions/31374951/add-points-and-colored-surface-to-locfit-perspective-plot https://stackoverflow.com/questions/24918604/how-to-have-only-every-other-border-in-a-persp https://stackoverflow.com/questions/3786189/r-4d-plot-x-y-z-colours