R Statistics Skill
Sub-skills
| Sub-skill |
Description |
| r-stats-bayesian |
Stan, brms, MCMC, posterior inference |
| r-stats-optimization |
ROI, lpSolve, nloptr |
| r-stats-finance |
quantmod, PerformanceAnalytics, tidyquant |
Bayesian analysis, optimization, and finance in R.
Bayesian Analysis
| Package |
Description |
| rstan ★ |
Stan MCMC interface |
| brms ★ |
High-level Bayesian regression |
| rstanarm |
Bayesian applied regression |
| coda |
MCMC output analysis |
| mcmc |
Markov Chain Monte Carlo |
| MCMCpack |
MCMC package |
| rjags |
JAGS interface |
| R2WinBUGS |
WinBUGS/OpenBUGS interface |
| BRugs |
OpenBUGS interface |
Optimization
| Package |
Description |
| ROI ★ |
R Optimization Infrastructure |
| lpSolve |
Linear/Integer programming |
| Rglpk |
GNU Linear Programming Kit |
| ompr |
Algebraic MIP modeling |
| nloptr |
Nonlinear optimization |
| minqa |
Derivative-free optimization |
Finance
| Package |
Description |
| quantmod ★ |
Quantitative financial modeling |
| tidyquant |
Tidy quantitative analysis |
| PerformanceAnalytics |
Performance and risk analysis |
| TTR |
Technical trading rules |
| zoo ★ |
Regular/irregular time series |
| xts |
Extensible time series |
| tseries |
Time series analysis |
| fAssets |
Financial assets analysis |
| scorecard |
Credit risk scorecard |
| pedquant |
Public economic data |
Quick Examples
# Bayesian regression with brms
library(brms)
model <- brm(
y ~ x1 + x2 + (1|group),
data = df,
family = gaussian(),
prior = c(
prior(normal(0, 10), class = "b"),
prior(cauchy(0, 2), class = "sd")
),
chains = 4, iter = 2000
)
summary(model)
plot(model)
# Stan model
library(rstan)
stan_code <- "
data {
int<lower=0> N;
vector[N] x;
vector[N] y;
}
parameters {
real alpha;
real beta;
real<lower=0> sigma;
}
model {
y ~ normal(alpha + beta * x, sigma);
}
"
fit <- stan(model_code = stan_code, data = list(N = 100, x = x, y = y))
# Linear programming
library(lpSolve)
obj <- c(1, 2, 3)
con <- matrix(c(1, 1, 1, 2, 1, 3), nrow = 2, byrow = TRUE)
dir <- c("<=", "<=")
rhs <- c(4, 6)
lp("max", obj, con, dir, rhs)
# Quantitative finance
library(quantmod)
getSymbols("AAPL", from = "2020-01-01")
chartSeries(AAPL)
addBBands()
addMACD()
# Portfolio analysis
library(PerformanceAnalytics)
returns <- Return.calculate(prices)
table.AnnualizedReturns(returns)
chart.RollingPerformance(returns)
SharpeRatio(returns)
# Time series
library(zoo)
ts <- zoo(data, order.by = dates)
rollmean(ts, k = 7)
na.locf(ts) # Last observation carried forward
Bayesian Workflow
library(brms)
# 1. Specify model
model <- brm(
y ~ x + (1|group),
data = df,
family = gaussian()
)
# 2. Check convergence
plot(model)
mcmc_trace(as.array(model))
# 3. Posterior predictive check
pp_check(model)
# 4. Model comparison
loo(model1, model2)
# 5. Predictions
predict(model, newdata = new_df)
Resources
1---2name: r-stats3description: R statistical packages for Bayesian analysis, optimization, and finance. Use for MCMC (Stan, JAGS), linear programming, portfolio analysis, and quantitative finance.4metadata:5 source: https://skillmd.com/skills/leolin990405/r-stats6---78# R Statistics Skill910## Sub-skills1112| Sub-skill | Description |13|-----------|-------------|14| [r-stats-bayesian](r-stats-bayesian/SKILL.md) | Stan, brms, MCMC, posterior inference |15| [r-stats-optimization](r-stats-optimization/SKILL.md) | ROI, lpSolve, nloptr |16| [r-stats-finance](r-stats-finance/SKILL.md) | quantmod, PerformanceAnalytics, tidyquant |1718Bayesian analysis, optimization, and finance in R.1920## Bayesian Analysis2122| Package | Description |23|---------|-------------|24| **rstan** ★ | Stan MCMC interface |25| **brms** ★ | High-level Bayesian regression |26| **rstanarm** | Bayesian applied regression |27| **coda** | MCMC output analysis |28| **mcmc** | Markov Chain Monte Carlo |29| **MCMCpack** | MCMC package |30| **rjags** | JAGS interface |31| **R2WinBUGS** | WinBUGS/OpenBUGS interface |32| **BRugs** | OpenBUGS interface |3334## Optimization3536| Package | Description |37|---------|-------------|38| **ROI** ★ | R Optimization Infrastructure |39| **lpSolve** | Linear/Integer programming |40| **Rglpk** | GNU Linear Programming Kit |41| **ompr** | Algebraic MIP modeling |42| **nloptr** | Nonlinear optimization |43| **minqa** | Derivative-free optimization |4445## Finance4647| Package | Description |48|---------|-------------|49| **quantmod** ★ | Quantitative financial modeling |50| **tidyquant** | Tidy quantitative analysis |51| **PerformanceAnalytics** | Performance and risk analysis |52| **TTR** | Technical trading rules |53| **zoo** ★ | Regular/irregular time series |54| **xts** | Extensible time series |55| **tseries** | Time series analysis |56| **fAssets** | Financial assets analysis |57| **scorecard** | Credit risk scorecard |58| **pedquant** | Public economic data |5960## Quick Examples6162```r63# Bayesian regression with brms64library(brms)65model <- brm(66 y ~ x1 + x2 + (1|group),67 data = df,68 family = gaussian(),69 prior = c(70 prior(normal(0, 10), class = "b"),71 prior(cauchy(0, 2), class = "sd")72 ),73 chains = 4, iter = 200074)75summary(model)76plot(model)7778# Stan model79library(rstan)80stan_code <- "81data {82 int<lower=0> N;83 vector[N] x;84 vector[N] y;85}86parameters {87 real alpha;88 real beta;89 real<lower=0> sigma;90}91model {92 y ~ normal(alpha + beta * x, sigma);93}94"95fit <- stan(model_code = stan_code, data = list(N = 100, x = x, y = y))9697# Linear programming98library(lpSolve)99obj <- c(1, 2, 3)100con <- matrix(c(1, 1, 1, 2, 1, 3), nrow = 2, byrow = TRUE)101dir <- c("<=", "<=")102rhs <- c(4, 6)103lp("max", obj, con, dir, rhs)104105# Quantitative finance106library(quantmod)107getSymbols("AAPL", from = "2020-01-01")108chartSeries(AAPL)109addBBands()110addMACD()111112# Portfolio analysis113library(PerformanceAnalytics)114returns <- Return.calculate(prices)115table.AnnualizedReturns(returns)116chart.RollingPerformance(returns)117SharpeRatio(returns)118119# Time series120library(zoo)121ts <- zoo(data, order.by = dates)122rollmean(ts, k = 7)123na.locf(ts) # Last observation carried forward124```125126## Bayesian Workflow127128```r129library(brms)130131# 1. Specify model132model <- brm(133 y ~ x + (1|group),134 data = df,135 family = gaussian()136)137138# 2. Check convergence139plot(model)140mcmc_trace(as.array(model))141142# 3. Posterior predictive check143pp_check(model)144145# 4. Model comparison146loo(model1, model2)147148# 5. Predictions149predict(model, newdata = new_df)150```151152## Resources153154- Stan: https://mc-stan.org/155- brms: https://paul-buerkner.github.io/brms/156- quantmod: http://www.quantmod.com/157- PerformanceAnalytics: https://github.com/braverock/PerformanceAnalytics