Multi-Language Code Examples
statistics
programming
tutorial
Examples of using Stata, Julia, and pseudocode in blog posts
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 reJulia 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
Output: Confidence interval
- for
to do- Draw bootstrap sample
by sampling observations from with replacement - Calculate
- Draw bootstrap sample
- end for
- Sort
in ascending order - Set
- Set
- return
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
Output:
- Set
, - while not converged do
- Compute score function:
- Compute Hessian:
- Update:
- Check convergence: if
then break - if
then break
- Compute score function:
- end while
- Compute standard errors:
- return
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.intJulia:
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 dataPython:
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
- Use syntax highlighting for display-only code - Most readers just need to see the code
- Execute code sparingly - Only execute when output is essential
- Document your setup - Note required packages and versions
- Test before publishing - Ensure code runs without errors
- 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.