Book contents

STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 25

Introduction to Julia for Statistics

Julia is a modern programming language designed for high–performance numerical computing. Its syntax is similar to MATLAB and Python, and it offers the speed of compiled languages. This chapter introduces basic Julia programming concepts and packages useful for data analysis.

Getting Started

Install Julia from https://julialang.org and use the Julia REPL or an IDE such as Juno or VS Code. Basic operations are straightforward:

2 + 3
sqrt(2.0)
log(10)

Variables are assigned with = and types are inferred. Arrays are created using square brackets: [1, 2, 3]. Indexing starts at 1, unlike Python and C.

Packages for Data Analysis

Julia’s package manager Pkg installs packages. Key packages include:

For example:

using DataFrames, CSV
df = CSV.read("data.csv", DataFrame)
describe(df)

Data Manipulation and Plotting

Create a DataFrame and perform basic operations:

using DataFrames
df = DataFrame(height = [160,165,170], weight = [55,60,65])
mean_height = mean(df.height)

Plotting with Plots.jl:

using Plots
scatter(df.height, df.weight, xlabel="Height", ylabel="Weight", title="Height vs Weight")
histogram(df.height, bins=3, xlabel="Height", title="Histogram of Height")

Linear Regression in Julia

Fit a linear model using GLM.jl:

using GLM
df = DataFrame(height = [160,165,170,175,180], weight = [55,60,65,70,75])
lm_fit = lm(@formula(weight ~ height), df)
coef(lm_fit)

The coef() function returns the estimated intercept and slope.

Exercises

Practice

  1. Install the DataFrames.jl and CSV.jl packages. Read a CSV dataset and compute basic summary statistics. Filter rows and create new columns.

  2. Write a Julia function that takes a vector of numbers and returns the interquartile range. Test it on a sample dataset.

  3. Using the GLM.jl package, fit a linear model predicting one variable from another. Extract the coefficients and plot the fitted line on a scatterplot.

  4. Compare the syntax of Julia, R and Python for reading data, computing means and fitting linear models. Discuss advantages and disadvantages of Julia.

References