R syntax highlighting

So WordPress has added support for syntax highlighting in several languages including Python, R and SQL. In the future I may post about interesting snippets of codes on certain topics, such as factor investing. I want to explore how it works, so this is how this blog post came about.

Code is law.

Lawrence Lessig

The quotation above is unrelated, but somehow I felt like putting it there anyway.

These code snippets are taken from An Introduction to glmnet, a machine learning package fits generalized linear and similar models via penalized maximum likelihood (e.g. ridge regression, LASSO and elastic net).

# test syntax highlighting with glmnet
# first, install the package
install.packages("glmnet", repos = "https://cran.us.r-project.org")

The intro page walks through the basic idea behind the models and shows us the data structure (i labels with j features) required for the model. The model itself is time-agnostic so the data can be cross sectional, time series or panel.

# now, let's try using the package
library(glmnet)
data(QuickStartExample)
fit <- glmnet(x, y)
plot(fit)

There are several ways of plotting the output, but idea is to find a set of features that best predicts the labels. We can visualize coefficients versus lambda, mean-squared error versus lambda or coefficients versus dev, fraction deviance (variations) explained.

I’ll be applying glmnet to factor investing to add machine learning to the rule-based algorithm in the future.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s