65  Appendix A — Complete Environment Setup

66 Appendix A — Complete Environment Setup

This appendix provides comprehensive step-by-step instructions to set up a complete development environment for reproducing all analyses, code examples, and case studies in “AI-Powered Business Analytics.” The setup covers Windows, macOS, and Linux.

66.1 System Requirements

  • Operating System: Windows 10+, macOS 10.15+, or Linux (Ubuntu 20.04+)
  • Disk Space: 10 GB free
  • RAM: 8 GB minimum (16 GB recommended for deep learning chapters)
  • Internet: Required for package downloads

66.2 Part 1: R Installation

66.2.1 Windows

  1. Download R from cran.r-project.org — select “Download R for Windows”

  2. Click “base” and download the latest version (≥ 4.3.0)

  3. Run the installer (e.g., R-4.3.2-win.exe)

  4. Choose default installation directory (usually C:\Program Files\R\R-4.3.2)

  5. Complete the installation wizard

  6. Verify installation by opening Command Prompt and typing:

    R --version

66.2.2 macOS

  1. Download R from cran.r-project.org — select “Download R for macOS”

  2. Choose the appropriate version (Intel or Apple Silicon/M1/M2)

  3. Download the .pkg file (version ≥ 4.3.0)

  4. Double-click the installer and follow the wizard

  5. Verify installation in Terminal:

    R --version

66.2.3 Linux (Ubuntu/Debian)

  1. Open Terminal and add the CRAN repository:

    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
    sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
  2. Update and install:

    sudo apt update
    sudo apt install r-base r-base-dev (version ≥ 4.3.0)
  3. Verify:

    R --version

66.3 Part 2: RStudio Desktop Installation

RStudio is the recommended IDE for R development.

66.3.1 All Platforms

  1. Download from rstudio.com/products/rstudio/download
  2. Select RStudio Desktop (Free) — version ≥ 2024.04
  3. Choose your operating system and download the installer
  4. Run the installer and follow the wizard
  5. Launch RStudio and verify it detects R correctly

66.4 Part 3: Positron Installation (Optional Advanced IDE)

Positron is a modern polyglot IDE that supports R, Python, and Quarto seamlessly.

  1. Download from posit.co/download/positron
  2. Follow platform-specific instructions
  3. Launch Positron and configure default interpreter (R and Python paths)

66.5 Part 4: Quarto CLI Installation

Quarto is essential for rendering .qmd documents to HTML and PDF.

66.5.2 Windows (Alternative: Chocolatey)

choco install quarto

66.5.3 macOS (Alternative: Homebrew)

brew install quarto

66.5.4 Linux (Alternative: Direct download)

wget https://github.com/quarto-dev/quarto-cli/releases/download/v1.4.x/quarto-1.4.x-linux-amd64.tar.gz
tar -xzf quarto-1.4.x-linux-amd64.tar.gz
sudo mv quarto-1.4.x /opt/quarto
sudo ln -s /opt/quarto/bin/quarto /usr/local/bin/quarto

66.6 Part 5: Python Installation

66.6.3 Option C: Direct Installation

  1. Download from python.org — version 3.11+

  2. Run installer

  3. Windows: Check “Add Python to PATH”

  4. Verify:

    python --version

66.7 Part 6: Setting Up a Python Virtual Environment

66.7.1 Using venv (Built-in)

# Navigate to project directory
cd ~/AI-Business-Analytics

# Create virtual environment
python -m venv venv

# Activate
# macOS/Linux:
source venv/bin/activate

# Windows:
venv\Scripts\activate

66.7.2 Using Conda

conda create -n analytics python=3.11
conda activate analytics

66.8 Part 7: Installing R Packages

Open R or RStudio and install packages by chapter range. Use pak::pak() for faster parallel installation.

66.8.1 Installation Setup

# Install pak for faster installation
install.packages("pak")

66.8.2 Part I: Foundations (Chapters 1-11)

pak::pak(c(
  # Data manipulation and tidyverse
  "tidyverse",
  "data.table",
  "dtplyr",

  # Statistical foundations
  "stats",
  "car",
  "MASS",
  "nortest",

  # Hypothesis testing
  "coin",
  "exactRankTests",

  # Machine learning basics
  "caret",
  "mlbench",
  "rpart",
  "rpart.plot",

  # Visualisation
  "ggplot2",
  "plotly",
  "igraph",
  "ggraph",
  "sf",
  "leaflet",

  # Time series
  "forecast",
  "tseries",
  "xts",
  "zoo"
))

66.8.3 Part II: Advanced Methods (Chapters 12-25)

pak::pak(c(
  # Text and NLP
  "tm",
  "tidytext",
  "textdata",
  "quanteda",
  "topicmodels",
  "sentiment",
  "syuzhet",

  # Image processing
  "imager",
  "magick",

  # Recommendation engines
  "recommenderlab",
  "recosystem",

  # Network analysis
  "igraph",
  "tidygraph",
  "ggraph",
  "statnet",

  # Optimisation
  "lpSolve",
  "nloptr",
  "optimx"
))

66.8.4 Part III: Predictive Analytics (Chapters 26-36)

pak::pak(c(
  # Advanced classification and regression
  "randomForest",
  "xgboost",
  "lightgbm",
  "glmnet",
  "elasticnet",
  "e1071",
  "kernlab",

  # Model evaluation
  "pROC",
  "ROCR",
  "DescTools",

  # Survival analysis
  "survival",
  "survminer",
  "coxme",

  # Clustering
  "cluster",
  "dbscan",
  "mclust",

  # SHAP and interpretability
  "shapviz",
  "iml"
))

66.8.5 Part IV: Optimisation & Applications (Chapters 37-50)

pak::pak(c(
  # Deep learning
  "keras",
  "tensorflow",

  # Time series advanced
  "prophet",
  "anomalize",

  # Supply chain and operations
  "simmer",
  "lpSolve",

  # Financial and risk
  "quantmod",
  "PerformanceAnalytics",
  "copula",

  # General utilities
  "here",
  "glue",
  "magrittr",
  "patchwork"
))

66.9 Part 8: Installing Python Packages

Create a requirements.txt file in your project root:

# Data manipulation
pandas==2.0.3
numpy==1.24.3
polars==0.18.13

# Visualisation
matplotlib==3.7.2
seaborn==0.12.2
plotly==5.15.0

# Scikit-learn and classical ML
scikit-learn==1.3.0
xgboost==2.0.0
lightgbm==4.0.0
catboost==1.2.2

# Deep learning
tensorflow==2.13.0
torch==2.0.1
torchvision==0.15.2

# NLP and text
nltk==3.8.1
spacy==3.6.1
gensim==4.3.1
transformers==4.32.1

# Time series
statsmodels==0.14.0
prophet==1.1.5

# Network analysis
networkx==3.1

# Optimisation
scipy==1.11.2
pyomo==6.6.1
pulp==2.7.0

# Geospatial
geopandas==0.13.2
shapely==2.0.1

# Utilities
tqdm==4.66.1
joblib==1.3.1
pyyaml==6.0

# Jupyter notebooks (optional)
jupyter==1.0.0
jupyterlab==4.0.4

Install with:

pip install -r requirements.txt

66.9.1 Spacy Language Models (NLP)

After installing spacy, download language models:

python -m spacy download en_core_web_sm
python -m spacy download en_core_web_lg

66.10 Part 9: Configuring reticulate in R

To enable R to call Python, configure reticulate:

# In R or RStudio
install.packages("reticulate")

library(reticulate)

# Option A: Point to conda environment
use_condaenv("analytics")

# Option B: Point to virtual environment
use_virtualenv("~/AI-Business-Analytics/venv")

# Verify configuration
py_config()

Add to your project’s .Rprofile (in project root):

library(reticulate)
use_virtualenv("./venv")

66.11 Part 10: Setting Up a Quarto Book Project

Create a directory structure for a Quarto book:

# Create project directory
mkdir ai-business-analytics-book
cd ai-business-analytics-book

# Create subdirectories
mkdir chapters appendices data scripts

# Create Quarto configuration
cat > _quarto.yml << 'EOF'
project:
  type: book
  output-dir: _book

book:
  title: "AI-Powered Business Analytics"
  author: "Bongo Adi"
  date: 2024-01-01
  chapters:
    - index.qmd
    - chapters/01-introduction.qmd
    # ... add remaining chapters
  appendices:
    - appendices/A-setup.qmd
    # ... add remaining appendices

format:
  html:
    theme: cosmo
    toc: true
    toc-depth: 2
  pdf:
    documentclass: book
    engine: lualatex
    keep-tex: false

execute:
  freeze: auto
EOF

66.12 Part 11: LaTeX Installation (Required for PDF Rendering)

66.12.1 Windows

  1. Download MiKTeX from miktex.org

  2. Run the installer

  3. Choose “Install missing packages on-the-fly”

  4. Verify:

    pdflatex --version

66.12.2 macOS

# Using Homebrew
brew install --cask mactex

# Or minimal version
brew install --cask mactex-no-gui

66.12.3 Linux (Ubuntu/Debian)

sudo apt-get install texlive-latex-base texlive-latex-extra texlive-luatex texlive-fonts-recommended

66.13 Part 12: Rendering Quarto Documents

66.13.1 Rendering to HTML

quarto render document.qmd --to html

66.13.2 Rendering to PDF (LuaLaTeX)

In _quarto.yml:

format:
  pdf:
    engine: lualatex
    keep-tex: false

Then:

quarto render document.qmd --to pdf

66.13.3 Rendering Full Book

quarto render

66.14 Part 13: Troubleshooting

66.14.1 Windows Issues

Problem: R command not found Solution: Add R to PATH. Go to Control Panel → System → Advanced → Environment Variables. Add C:\Program Files\R\R-x.x.x\bin to PATH.

Problem: LaTeX missing for PDF Solution: Install MiKTeX and ensure pdflatex is in PATH.

Problem: Python not found in reticulate Solution: In R, run reticulate::py_discover_config() to check Python locations, then explicitly set with use_virtualenv() or use_condaenv().

66.14.2 macOS Issues

Problem: Permission denied for Homebrew packages Solution:

sudo chown -R $(whoami) /usr/local/Cellar

Problem: M1/M2 compatibility Solution: Use native R build for macOS. Download from CRAN “for macOS 11+ (Apple Silicon)”.

66.14.3 Linux Issues

Problem: Build tools missing Solution: Install development headers:

sudo apt-get install build-essential libcurl4-openssl-dev libssl-dev

Problem: Cannot load Python modules Solution: Ensure Python version matches reticulate expectations:

python -c "import sys; print(sys.version)"

66.15 Part 14: Verification Script — “Hello, Data!”

66.15.1 R Verification Script

Save as verify_setup.R:

#!/usr/bin/env Rscript

cat("=== R Setup Verification ===\n\n")

# Check R version
cat("R Version:", as.character(getRVersion()), "\n")
if (getRVersion() < "4.3.0") {
  cat("WARNING: R version should be >= 4.3.0\n")
}

# Check key packages
packages <- c(
  "tidyverse", "data.table", "caret", "ggplot2",
  "forecast", "xgboost", "keras", "reticulate"
)

cat("\nPackage Check:\n")
for (pkg in packages) {
  if (requireNamespace(pkg, quietly = TRUE)) {
    cat("  ✓", pkg, "\n")
  } else {
    cat("  ✗", pkg, "— MISSING\n")
  }
}

# Check reticulate and Python
cat("\nPython Configuration:\n")
library(reticulate)
cat("Python:", py_config()$python, "\n")
cat("Version:", paste(py_config()$version, collapse = "."), "\n")

# Simple data manipulation test
cat("\nData Manipulation Test:\n")
library(tidyverse)
test_data <- tibble(
  x = 1:10,
  y = x^2
)
cat("Created test tibble with", nrow(test_data), "rows\n")

cat("\n✓ R setup verification complete!\n")

Run with:

Rscript verify_setup.R

66.15.2 Python Verification Script

Save as verify_setup.py:

#!/usr/bin/env python

print("=== Python Setup Verification ===\n")

import sys
print(f"Python Version: {sys.version}")

required_packages = [
    "pandas", "numpy", "matplotlib", "scikit-learn",
    "tensorflow", "torch", "nltk", "statsmodels", "networkx"
]

print("\nPackage Check:")
for pkg in required_packages:
    try:
        __import__(pkg)
        print(f"  ✓ {pkg}")
    except ImportError:
        print(f"  ✗ {pkg} — MISSING")

# Simple data test
print("\nData Manipulation Test:")
import pandas as pd
df = pd.DataFrame({
    'x': range(1, 11),
    'y': [i**2 for i in range(1, 11)]
})
print(f"Created DataFrame with {len(df)} rows")

print("\n✓ Python setup verification complete!")

Run with:

python verify_setup.py

66.16 Part 15: Quick Start Checklist

Once all items are checked, you are ready to reproduce all analyses in the book!


For issues or updates, visit the book’s GitHub repository.