Miscellaneous R Code

Code that might be useful to others for learning/demonstration purposes, specifically along the lines of modeling and various algorithms. **Superseded by the models-by-example repo**.

193
82
R

Lifecycle Status

Miscellaneous (mostly) R Code

NOTE: Many of the models and algorithms in this REPO have been updated and now live in a a more organized document. Most of the code that carries over (the bulk of the files in /ModelFitting) has been upgraded, so refer to the document instead of these files, even if they have been relatively recently revised. Though I started updating these initially, all subsequent work went to the document, so it should now be your point of reference.

However, only a handful of Bayesian demos are going to be included in that document, as the Stan code is pretty dated at this point, and most of the functionality demonstrated now exists in rstanarm/brms, whose functionality allows one to inspect the model code. I will still likely transfer some that may be more unique/useful to the document.

The document still has some kinks to work out and some additions incoming, but can be found here. The updated code there will be more ‘tidy’ and conform to more widely used coding standards, which was the goal of the code here anyway. More can be found at my website.


This is a place for miscellaneous R and other code I’ve put together for clients, co-workers or myself for learning and demonstration purposes. The attempt is made to put together some well-commented and/or conceptually clear code from scratch, though most functionality is readily available in any number of well-developed R packages. Typically, examples are provided using such packages for comparison of results. I would say most of these are geared toward intermediate to advanced folks that want to dig a little deeper into the models and underlying algorithms.

I also have documents of varying depth on a range of modeling and programming topics that can be found at my website.

Model Fitting

Code related to fitting of various models.

standard linear regression,
standard logistic regression,
penalized regression,
lasso regression,
ridge regression,
newton and IRLS,
nelder-mead (Python) ®,
gradient descent (stochastic),
one factor random effects ®
(Julia)
(Matlab),
two factor random effects ®
(Julia)
(Matlab),
mixed model via ML,
bivariate probit,
heckman selection,
tobit,
naive bayes,
multinomial regression,
ordinal regression,
quantile regression,
marginal structural model,
cubic spline,
hurdle poisson,
hurdle negbin,
zero-inflated poisson,
zero-inflated negbin,
Cox survival,
confirmatory factor analysis,
Markov model,
hidden Markov model ®
(Python),
EM mixture univariate,
EM mixture multivariate,
EM probit,
EM pca,
EM probabilistic pca (with missing),
EM state space model,
Gaussian Process noisy,
Gaussian Process noise-free,
reproducing kernel hilbert space regression,
extreme learning machine,
Chinese restaurant process, Indian buffet process,
One-line models (an exercise),

Bayesian (mostly with Stan/rstan)

BEST t-test,
linear regression
(Compare with BUGS version, JAGS),
mixed model,
mixed model with correlated random effects,
beta regression,
mixed model with beta response (Stan) (JAGS),
mixture model,
topic model,
multinomial models,
multilevel mediation,
variational bayes regression,
gaussian process,
stochastic volatility,
horseshoe prior,
item response theory, …

SC and TR

This part of the repository is deprecated, but used to be a section of ‘short courses’ and ‘technical reports’. See the Workshops or docs repositories instead, or go to the workshops and documents sections of the website where you can see finished products …

Other

Random shenanigans.

FizzBuzz test ® (julia) (Python),
Reverse a string recursively ® (Python),
Recursive Word Wrap ® (Python),
calculate compound interest recursively,
get US Congress roll call data,
Scrape xkcd ® (Python),
Shakespearean Insulter,
ggplot2 theme,
spurious correlation with ratios,
R matrix speedups, …