Multi-Language Code Examples

statistics
programming
tutorial
Examples of using Stata, Julia, and pseudocode in blog posts
Author

Zad Rafi

Published

January 6, 2026

Introduction

This post demonstrates how to include Stata, Julia, and pseudocode in your statistical posts on Less Likely.

Stata Examples

Stata code can be included for syntax highlighting:

* ============================================
* Example: Linear Regression Analysis
* ============================================

* Load example dataset
sysuse auto, clear

* Descriptive statistics
summarize price mpg weight foreign

* Create a scatter plot
scatter price mpg, title("Price vs. MPG")

* Run linear regression
regress price mpg weight foreign

* Store estimates
estimates store model1

* Display results with confidence intervals
regress price mpg weight foreign, level(95)

* Test joint significance
test mpg weight

* Predict fitted values
predict price_hat

* Calculate residuals
predict residuals, residuals

* Plot residuals
scatter residuals price_hat, ///
  yline(0) ///
  title("Residual Plot")

Advanced Stata: Panel Data Analysis

* ============================================
* Panel Data Example
* ============================================

* Load panel dataset
webuse nlswork, clear

* Declare panel structure
xtset idcode year

* Summary statistics by panel
xtsum ln_wage age

* Fixed effects regression
xtreg ln_wage age tenure, fe

* Random effects regression
xtreg ln_wage age tenure, re

* Hausman test
hausman fe re

Julia Examples

Julia code for statistical computing:

# ============================================
# Example: Linear Regression in Julia
# ============================================

using DataFrames, Statistics, GLM, Plots

# Create sample data
n = 100
x = randn(n)
y = 2.0 .+ 3.0 .* x .+ randn(n) .* 0.5

# Create DataFrame
df = DataFrame(x = x, y = y)

# Fit linear model
model = lm(@formula(y ~ x), df)

# Display results
println(model)

# Plot data and fitted line
scatter(x, y, label="Data", alpha=0.6)
plot!(x, predict(model), label="Fitted Line", linewidth=2)
xlabel!("X")
ylabel!("Y")
title!("Linear Regression")

Advanced Julia: Bootstrap Confidence Intervals

# ============================================
# Bootstrap Example
# ============================================

using Distributions, StatsBase

# Define a function to bootstrap
function bootstrap_mean(data, n_bootstrap=1000)
    n = length(data)
    bootstrap_means = zeros(n_bootstrap)

    for i in 1:n_bootstrap
        # Resample with replacement
        sample = rand(data, n)
        bootstrap_means[i] = mean(sample)
    end

    return bootstrap_means
end

# Generate sample data
data = randn(100) .+ 5.0

# Perform bootstrap
bootstrap_samples = bootstrap_mean(data)

# Calculate confidence interval
ci_lower = quantile(bootstrap_samples, 0.025)
ci_upper = quantile(bootstrap_samples, 0.975)

println("95% Bootstrap CI: [", ci_lower, ", ", ci_upper, "]")

# Plot bootstrap distribution
histogram(bootstrap_samples,
         bins=30,
         label="Bootstrap Distribution",
         xlabel="Sample Mean",
         ylabel="Frequency")
vline!([ci_lower, ci_upper],
       label="95% CI",
       linewidth=2,
       color=:red)

Pseudocode Examples

Example 1: Algorithm Block

Algorithm 1 Bootstrap Confidence Interval

Input: Data X={x1,x2,...,xn}, statistic θ(X), confidence level α

Output: Confidence interval [θL,θU]

  1. for b=1 to B do
    • Draw bootstrap sample Xb by sampling n observations from X with replacement
    • Calculate θb=θ(Xb)
  2. end for
  3. Sort {θ1,θ2,...,θB} in ascending order
  4. Set θL=quantile(θ,α/2)
  5. Set θU=quantile(θ,1α/2)
  6. return [θL,θU]

Example 2: Simple Pseudocode Block

ALGORITHM: Linear Regression via Gradient Descent
INPUT: X (n × p design matrix), y (n × 1 response vector), learning rate α
OUTPUT: β (p × 1 coefficient vector)

1. Initialize β = 0, iteration = 0, maxiter = 1000
2. REPEAT until convergence OR iteration > maxiter:
   a. Compute predictions: ŷ = Xβ
   b. Compute residuals: r = y - ŷ
   c. Compute gradient: ∇L = -2X'r/n
   d. Update parameters: β = β - α∇L
   e. iteration = iteration + 1
3. RETURN β

Example 3: Detailed Algorithm with Comments

Algorithm 2 Maximum Likelihood Estimation

Input: Data X, log-likelihood function (θ|X), initial value θ0

Output: θ^MLE

  1. Set θθ0, t0
  2. while not converged do
    • Compute score function: s(θ)=θ(θ|X)
    • Compute Hessian: H(θ)=θ2(θ|X)
    • Update: θθH(θ)1s(θ)
    • Check convergence: if s(θ)<ϵ then break
    • tt+1
    • if t>max_iterations then break
  3. end while
  4. Compute standard errors: SE(θ^)=diag(H(θ^)1)
  5. return θ^MLE

Comparing Approaches

Here’s how the same analysis looks across languages:

Task: Calculate Mean and 95% CI

R:

data <- rnorm(100, mean = 5, sd = 2)
mean_val <- mean(data)
ci <- t.test(data)$conf.int

Julia:

using Statistics, HypothesisTests
data = randn(100) .* 2 .+ 5
mean_val = mean(data)
ci = confint(OneSampleTTest(data))

Stata:

clear
set obs 100
generate data = rnormal(5, 2)
mean data

Python:

import numpy as np
from scipy import stats
data = np.random.normal(5, 2, 100)
mean_val = np.mean(data)
ci = stats.t.interval(0.95, len(data)-1,
                      loc=mean_val,
                      scale=stats.sem(data))

Best Practices

  1. Use syntax highlighting for display-only code - Most readers just need to see the code
  2. Execute code sparingly - Only execute when output is essential
  3. Document your setup - Note required packages and versions
  4. Test before publishing - Ensure code runs without errors
  5. Provide context - Explain what the code does and why

Resources

Conclusion

With support for multiple languages, you can now share statistical methods and algorithms in the language that best suits your analysis and audience.

Citation

BibTeX citation:
@online{panda2026,
  author = {Panda, Sir and Rafi, Zad},
  title = {Multi-Language {Code} {Examples}},
  date = {2026-01-06},
  url = {https://lesslikely.com/posts/statistics/code-examples.html},
  langid = {en}
}
For attribution, please cite this work as:
1. Panda S, Rafi Z. (2026). ‘Multi-Language Code Examples’. https://lesslikely.com/posts/statistics/code-examples.html.