Multi-Language Code Examples
Introduction
In this post, I’d like to show how we can include Stata, Julia, and pseudocode in statistical posts on Less Likely.
Stata Examples
Let’s start with Stata. Here, the code is only included for syntax highlighting and is not executed:
* ============================================
* 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
Next, here’s some Julia code for a few basic summaries and a regression:
# ============================================
# 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 , statistic , confidence level
Output: Confidence interval
- for to do
- Draw bootstrap sample by sampling observations from with replacement
- Calculate
- 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 , log-likelihood function , initial value
Output:
- Set ,
- while not converged do
- Compute score function:
- Compute Hessian:
- Update:
- Check convergence: if then break
- if then break
- end while
- Compute standard errors:
- return
Comparing Approaches
Here’s how the same (very simple) analysis looks when we write it in each language:
Task: Calculate Mean and 95% CI
R:
Julia:
using Statistics, HypothesisTests
data = randn(100) .* 2 .+ 5
mean_val = mean(data)
ci = confint(OneSampleTTest(data))Stata:
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
- Use syntax highlighting for display-only code. Most readers only need to see the code.
- Execute code sparingly. I’d only execute it when the output is essential to the post.
- Document the setup. We should note the required packages and their versions.
- Test before publishing. The code should run without errors.
- Provide context. It helps to explain what the code does and why it is there.
Resources
Conclusion
With support for several languages in place, we can share statistical methods and algorithms in whichever language suits the analysis and the audience. I’d also suggest reading the documentation linked above before executing Stata or Julia chunks.
Comments