Concise example of a basic linear regression in Julia, using GLM and RDatasets.

Reference:

In [1]:
using RDatasets, GLM
In [8]:
# List Datasets in the RDatasets julia library, in the "ISLR" package (there's lots more)
RDatasets.datasets("ISLR")
Out[8]:

11 rows × 5 columns

PackageDatasetTitleRowsColumns
String15String31StringInt64Int64
1ISLRAutoAuto Data Set3929
2ISLRCaravanThe Insurance Company (TIC) Benchmark582286
3ISLRCarseatsSales of Child Car Seats40011
4ISLRCollegeU.S. News and World Report's College Data77719
5ISLRDefaultCredit Card Default Data100004
6ISLRHittersBaseball Data32220
7ISLROJOrange Juice Data107018
8ISLRPortfolioPortfolio Data1002
9ISLRSmarketS&P Stock Market Data12509
10ISLRWageMid-Atlantic Wage Data300012
11ISLRWeeklyWeekly S&P Stock Market Data10899
In [11]:
# RDatasets has a dataset() function to reads in a dataset from the catalog
auto = dataset("ISLR", "Auto");
first(auto, 5)
Out[11]:

5 rows × 9 columns (omitted printing of 1 columns)

MPGCylindersDisplacementHorsepowerWeightAccelerationYearOrigin
Float64Float64Float64Float64Float64Float64Float64Float64
118.08.0307.0130.03504.012.070.01.0
215.08.0350.0165.03693.011.570.01.0
318.08.0318.0150.03436.011.070.01.0
416.08.0304.0150.03433.012.070.01.0
517.08.0302.0140.03449.010.570.01.0
In [22]:
# Call GLM, note you just use the column names without any Str of Symbol stuff
ols = lm(@formula(MPG ~ Cylinders + Displacement), auto)
Out[22]:
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Vector{Float64}}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}}}}, Matrix{Float64}}

MPG ~ 1 + Cylinders + Displacement

Coefficients:
─────────────────────────────────────────────────────────────────────────────
                   Coef.  Std. Error      t  Pr(>|t|)   Lower 95%   Upper 95%
─────────────────────────────────────────────────────────────────────────────
(Intercept)   36.5377     1.19661     30.53    <1e-99  34.1851     38.8903
Cylinders     -0.576348   0.443276    -1.30    0.1943  -1.44786     0.295169
Displacement  -0.0511185  0.00722576  -7.07    <1e-11  -0.0653249  -0.0369121
─────────────────────────────────────────────────────────────────────────────
In [ ]: