clip_04_04

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")

50 rows × 2 columns

SpeedDist
Int64⍰Int64⍰
142
2410
374
4722
5816
6910
71018
81026
91034
101117
111128
121214
131220
141224
151228
161326
171334
181334
191346
201426
211436
221460
231480
241520

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-11

estimated coefficients from the model

coef(m)
2-element Array{Float64,1}:
 -17.57909489051096
   3.932408759124088

Plot 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.