4.1 Advanced Data Handling & Preprocessing
4.1.1 Complex Data Structures
| Feature | MATLAB | R |
|---|---|---|
| Tabular Data | table: Heterogeneous columns, row names. <br> timetable: Table with row times for time-series. |
data.frame: Base structure. <br> tibble (tbl_df): Enhanced data.frame (from tibble package). |
| Categorical Data | categorical: Array of categories. Efficient for discrete data. |
factor: Categorical vector with levels. <br> forcats package for manipulation. |
| Date/Time | datetime, duration, calendarDuration: Precise time arithmetic. |
POSIXct, POSIXlt: Base date-time classes. <br> lubridate package: Simplified parsing/arithmetic. |
[!TIP] Common Pitfall: In R,
stringsAsFactors = FALSE(default in recent versions) prevents automatic conversion of strings to factors. In MATLAB,tablevariables can be accessed via dot notation (T.VarName).
4.1.2 Data Import/Export from Diverse Sources
| Source | MATLAB Functions | R Packages/Functions |
|---|---|---|
| Text/Spreadsheet | readtable, readmatrix, writetable, writematrix |
readr: read_csv, read_tsv (fast). <br> data.table: fread (very fast). |
| Databases | Database Toolbox: database, exec, fetch. |
DBI + backend (e.g., RPostgres, RSQLite): dbConnect, dbReadTable. |
| Web/API | webread, webwrite (JSON/XML). Basic scraping with htmlTree. |
jsonlite: fromJSON, toJSON. <br> xml2: read_xml. <br> rvest for web scraping. |
4.1.3 Data Cleaning & Transformation Pipelines
-
Handling Missing Data:
-
Imputation Strategies: Mean/median/mode filling, forward/backward fill (
fillmissingin MATLAB,tidyr::fillin R), model-based imputation. -
Removal:
rmmissing(MATLAB),na.omit(R).
-
-
Reshaping Data:
-
Wide to Long (Melt): MATLAB:
stack. R:tidyr::pivot_longer(). -
Long to Wide (Pivot): MATLAB:
unstack. R:tidyr::pivot_wider().
-
-
Pipeline Operations:
-
MATLAB: Use
varfun,rowfunfor group-wise operations. Chain with dot notation orfindgroups+splitapply. -
R:
dplyr+%>%pipe operator. Key verbs:filter(),select(),mutate(),group_by(),summarise().
-
[!TIP] Exam Focus: Know the difference between
pivot_longer()/pivot_wider()(tidyr, modern) vsgather()/spread()(tidyr, deprecated). In MATLAB,unstackrequires a grouping variable.
4.2 Advanced Statistical Modeling & Machine Learning
4.2.1 Supervised Learning
| Task | Model | MATLAB Function(s) | R Function(s)/Packages |
|---|---|---|---|
| Regression | Multiple Linear | fitlm |
lm() |
| Polynomial | fitlm (with polynomial terms) |
lm() (with poly() or I(x^2)) |
|
| Ridge/Lasso | fitrlinear ('Learner','ridge'/'lasso') |
glmnet: glmnet(x, y, alpha=0/1) |
|
| Classification | Logistic Regression | fitglm ('Distribution','binomial') |
glm(y ~ ., family=binomial) |
| SVM | fitcsvm |
e1071::svm() or kernlab::ksvm() |
|
| Decision Tree | fitctree |
rpart::rpart() or party::ctree() |
|
| Random Forest | TreeBagger |
randomForest::randomForest() |
|
| k-NN | fitcknn |
class::knn() |
|
| Model Evaluation | Cross-Validation | crossval, cvpartition |
caret::trainControl(method='cv') |
| ROC/AUC | perfcurve |
pROC::roc(), PRROC::roc() |
|
| Confusion Matrix | confusionmat |
caret::confusionMatrix() |
[!TIP] Key Concept: Regularization (Ridge/Lasso) adds penalty term $\lambda$ to loss function to prevent overfitting. Lasso (L1) can drive coefficients to zero (feature selection).
4.2.2 Unsupervised Learning
-
Clustering:
-
k-Means: MATLAB:
kmeans. R:stats::kmeans(). Sensitive to initial centroids & scale. -
Hierarchical: MATLAB:
linkage,cluster. R:hclust(),cutree(). -
DBSCAN: MATLAB:
dbscan(Statistics Toolbox). R:dbscan::dbscan().
-
-
Dimensionality Reduction:
-
PCA: MATLAB:
pca. R:prcomp()(recommended) orprincomp(). -
t-SNE: MATLAB:
tsne. R:Rtsne::Rtsne()oruwot::umap(for UMAP).
-
4.2.3 Time Series Analysis & Forecasting
-
Stationarity Tests: ADF test (
adftestin MATLAB,tseries::adf.test()in R). -
ARIMA/SARIMA:
-
Model Form: ARIMA$(p,d,q)$: $$\displaystyle \phi(B)(1-B)^d y_t = \theta(B)\epsilon_t $$, where $B$ is backshift operator.
-
MATLAB:
arima(p,d,q),estimate,forecast. -
R:
forecastpackage:auto.arima()(automatically selects p,d,q).fable/tsibble(tidyverts): modern framework.
-
[!TIP] Common Pitfall:
auto.arimain R uses stepwise search by default; setstepwise=FALSEfor exhaustive search. Always check residuals (checkresiduals()inforecast).
4.3 Simulation & Optimization Techniques
4.3.1 Monte Carlo Methods
-
Core Idea: Use repeated random sampling to estimate numerical results.
-
Random Number Generation:
-
MATLAB:
rand,randn,randi(control withrng). -
R:
runif,rnorm,sample(control withset.seed).
-
-
Variance Reduction: Antithetic variates, control variates, importance sampling.
-
Application Example (Option Pricing):
$$S_T = S_0 \exp\left((r - \frac{\sigma^2}{2})T + \sigma\sqrt{T}Z\right)$$
where $Z \sim N(0,1)$. Payoff = $$\displaystyle \max(S_T - K, 0) $$. Discount and average.
4.3.2 Deterministic Optimization
-
Linear Programming (LP): Minimize $$\displaystyle c^Tx $$ s.t. $Ax \leq b$, $x \geq 0$.
-
MATLAB:
linprog(f, A, b). -
R:
lpSolve::lp(direction="min", objective.in=f, const.mat=A, const.dir="<=", const.rhs=b).
-
-
Integer Programming (IP): Add integrality constraints on some $$\displaystyle x_i $$.
-
MATLAB:
intlinprog(f, intcon, A, b, Aeq, beq, lb, ub). -
R:
ompr::MIPModel()+ROIplugin (more modern) orlpSolve.
-
4.3.3 Nonlinear & Multi-objective Optimization
-
Nonlinear:
-
MATLAB:
fmincon(constrained),fminunc(unconstrained). -
R:
nloptr::nloptr().
-
-
Global/Heuristic:
- Genetic Algorithm (GA): MATLAB:
ga. R:GA::ga().
- Genetic Algorithm (GA): MATLAB:
-
Multi-objective:
- Pareto Front: MATLAB:
paretosearch. R:mco::nsga2().
- Pareto Front: MATLAB:
4.4 Advanced Visualization & Reporting
4.4.1 MATLAB Graphics Deep Dive
-
Customization:
title,xlabel,ylabel,legend,grid on. Colormaps:colormap('jet'),parula. -
Interactive Figures:
-
datacursormode(gcf, 'on'): Data cursor. -
uicontrol: Create buttons/sliders. Write callback functions that update plot data.
-
-
Geographic Plots: Requires Mapping Toolbox.
geobasemap,geoplot(lat,lon),geoscatter.
4.4.2 R Visualization Ecosystem
-
ggplot2Grammar:ggplot(data, aes(x,y)) + geom_point() + geom_line() + labs() + theme_minimal().-
Layers: Geoms (
geom_*), Stats (stat_*), Position (position_*). -
Scales:
scale_x_log10(),scale_color_manual(). -
Faceting:
facet_wrap(~var)orfacet_grid(row~col).
-
-
Interactive:
-
plotly:ggplotly(p)converts ggplot to interactive. -
shiny: Build reactive web apps. Core:ui(layout),server(logic),shinyApp(ui, server).
-
-
Dashboards:
flexdashboard(R Markdown based) orshinydashboard(Shiny UI functions).
4.5 Performance, Debugging & Code Management
4.5.1 Profiling & Acceleration
| Technique | MATLAB | R |
|---|---|---|
| Profiling | profile on/off, profile viewer. |
profvis::profvis(), Rprof(). |
| Timing | tic/toc. |
system.time(), microbenchmark::microbenchmark(). |
| Vectorization | Replace for loops with array operations. |
Use vectorized functions (+, *, apply family). |
| Preallocation | A = zeros(n,m) before loop. |
vector(mode="numeric", length=n) or numeric(n). |
| Parallel Computing | parfor (Parallel Computing Toolbox). |
parallel::mclapply(), future.apply, foreach + doParallel. |
| Compiled Code | MEX files (C/C++/Fortran). | Rcpp: Integrate C++ for speed. |
4.5.2 Debugging Strategies
-
MATLAB:
-
Set breakpoints in Editor.
-
dbstop if error: Stops on error. -
dbstack: Show call stack. -
keyboard: Pauses execution, enters debug mode.
-
-
R:
-
browser(): Insert in code to pause. -
traceback(): After error, shows call stack. -
debugonce(fun): Step through function once. -
RStudio: "Debug" pane, breakpoints, environment inspection.
-
4.5.3 Code Management & Reproducibility
-
Version Control: Git (conceptual). Use
.gitignorefor large data/compiled files. -
Packages/Libraries:
-
MATLAB: Create toolboxes. Add via
matlab.addons.install. Usematlab.addons.toolbox.installToolbox. -
R: Package development with
devtools/usethis. Document withroxygen2. Userenv::init()for dependency snapshot (likepipenv/conda).
-
-
Reproducibility: Set seeds (
rngin MATLAB,set.seedin R). Use project-oriented workflows (RStudio Projects, MATLAB Projects).
4.6 Interoperability & Specialized Applications
4.6.1 Calling R from MATLAB & Vice Versa
-
MATLAB → R: Use
systemcommand to run Rscript.system('Rscript myscript.R arg1 arg2');Exchange data via files (
.csv,.RData,.mat). -
R → MATLAB:
-
R.matlabpackage (R):readMat("file.mat"),writeMat(). -
reticulate(R → Python): Can indirectly exchange if Python has MATLAB engine. -
MATLAB Engine for Python: From R, use
reticulateto call Python which uses MATLAB Engine API.
-
4.6.2 Domain-Specific Application Patterns
| Domain | MATLAB | R |
|---|---|---|
| Engineering Systems | Simulink: Block diagram simulation. <br> Control System Toolbox: tf, pid, bode. |
Less common. control package: tf(), step(). |
| Bioinformatics/Stats | Bioinformatics Toolbox. | Bioconductor: Large repository for genomics (GenomicRanges, DESeq2). |
| Image/Signal Processing | Image Processing Toolbox (IPT): imfilter, bwareaopen, regionprops. <br> Signal Processing Toolbox: filter, fft. |
imager, EBImage (Bioconductor). signal package. |
4.7 Deployment & Sharing of Results
4.7.1 Creating Standalone Applications
-
MATLAB:
-
MATLAB Compiler (
mcc): Package apps into standalone executables or shared libraries. -
MATLAB Web App Server: Deploy apps as web apps (requires Compiler & Web App Server).
-
App Designer: Build GUI apps.
-
-
R:
-
Shiny Apps: Deploy to
shinyapps.io, RStudio Connect, or Shiny Server (open-source/pro). -
APIs:
plumberpackage. Decorate R functions with@*annotations to create REST API.
-
4.7.2 Generating Reports & Publications
-
MATLAB:
-
Live Scripts (
.mlx): Combine code, output, formatted text. -
Publish:
publish('script.m', 'pdf')or'html'. Uses markup in comments (%%sections).
-
-
R:
-
R Markdown (
.Rmd): Code chunks ({r}), inline R (r expr). Knit to HTML/PDF/Word. -
Quarto: Next-gen, supports R, Python, Julia. More publishing-focused.
-
Key Feature: Dynamic report generation—code runs, results (tables, plots) embedded automatically.
-
[!TIP] Critical Difference: R Markdown/Quarto separates code and narrative clearly in a single file. MATLAB Live Scripts are more integrated but less portable. For reproducible research, R Markdown/Quarto is industry standard.