Load Julia packages (libraries) needed
using StatisticalRethinking, GLM
gr(size=(300, 300))Plots.GRBackend()snippet 0.4
Below dataset(...) provides access to often used R datasets.
cars = dataset("datasets", "cars")| Speed | Dist | |
|---|---|---|
| Int64⍰ | Int64⍰ | |
| 1 | 4 | 2 |
| 2 | 4 | 10 |
| 3 | 7 | 4 |
| 4 | 7 | 22 |
| 5 | 8 | 16 |
| 6 | 9 | 10 |
| 7 | 10 | 18 |
| 8 | 10 | 26 |
| 9 | 10 | 34 |
| 10 | 11 | 17 |
| 11 | 11 | 28 |
| 12 | 12 | 14 |
| 13 | 12 | 20 |
| 14 | 12 | 24 |
| 15 | 12 | 28 |
| 16 | 13 | 26 |
| 17 | 13 | 34 |
| 18 | 13 | 34 |
| 19 | 13 | 46 |
| 20 | 14 | 26 |
| 21 | 14 | 36 |
| 22 | 14 | 60 |
| 23 | 14 | 80 |
| 24 | 15 | 20 |
| ⋮ | ⋮ | ⋮ |
If this is not a common R dataset, use e.g.: howell1 = CSV.read(joinpath(ProjDir, "..", "..", "data", "Howell1.csv"), delim=';') df = convert(DataFrame, howell1)
This reads the Howell1.csv dataset in the data subdirectory of this package, StatisticalRethinking.jl. See also the chapter 4 snippets.
Fit a linear regression of distance on speed
m = lm(@formula(Dist ~ Speed), cars)┌ Warning: In the future eachcol will have names argument set to false by default
│ caller = evalcontrasts(::DataFrame, ::Dict{Any,Any}) at modelframe.jl:124
└ @ StatsModels ~/.julia/packages/StatsModels/AYB2E/src/modelframe.jl:124
StatsModels.DataFrameRegressionModel{GLM.LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}
Formula: Dist ~ 1 + Speed
Coefficients:
Estimate Std.Error t value Pr(>|t|)
(Intercept) -17.5791 6.75844 -2.60106 0.0123
Speed 3.93241 0.415513 9.46399 <1e-11estimated coefficients from the model
coef(m)2-element Array{Float64,1}:
-17.57909489051096
3.932408759124088Plot residuals against speed
scatter( cars[:Speed], residuals(m),
xlab="Speed", ylab="Model residual values", lab="Model residuals")snippet 0.5 is replaced by above using StatisticalRethinking.
This page was generated using Literate.jl.