Skip to contents

This vignette showcases ways of interacting with pointers to jlmerclusterperm Julia objects from R using JuliaConnectoR.

See more tutorials and vignettes on the Articles page.

Setup

Interacting with Julia objects

All jlmerclusterperm functions collect Julia objects as R objects, except jlmer() and to_jlmer() which return GLM.jl or MixedModels.jl fitted model objects.

jmod <- to_jlmer(Reaction ~ Days + (Days | Subject), lme4::sleepstudy)
jmod
#> <Julia object of type LinearMixedModel{Float64}>
#> Variance components:
#>             Column    Variance Std.Dev.   Corr.
#> Subject  (Intercept)  565.51067 23.78047
#>          Days          32.68212  5.71683 +0.08
#> Residual              654.94145 25.59182
#> ──────────────────────────────────────────────────
#>                 Coef.  Std. Error      z  Pr(>|z|)
#> ──────────────────────────────────────────────────
#> (Intercept)  251.405      6.63226  37.91    <1e-99
#> Days          10.4673     1.50224   6.97    <1e-11
#> ──────────────────────────────────────────────────

You can use functions from JuliaConnectoR to interact with these (pointers to) Julia objects:

library(JuliaConnectoR)
juliaLet("x.rePCA", x = jmod)
#> <Julia object of type NamedTuple{(:Subject,), Tuple{Vector{Float64}}}>
#> (Subject = [0.5406660682947617, 1.0],)
juliaCall("issingular", jmod)
#> [1] FALSE

You can also call functions from other Julia packages that you already have installed. For example, Effects.jl for its marginaleffects/emmeans-like features:

# juliaEval('using Pkg; Pkg.activate(); Pkg.add("Effects")')
juliaEval("using Effects")
juliaLet("effects(x.namedelements, y)", x = list(Days = 2:5), y = jmod)
#> <Julia object of type DataFrame>
#> 4×5 DataFrame
#>  Row │ Days   Reaction  err      lower    upper
#>      │ Int64  Float64   Float64  Float64  Float64
#> ─────┼────────────────────────────────────────────
#>    1 │     2   272.34   7.09427  265.245  279.434
#>    2 │     3   282.807  7.70544  275.102  290.512
#>    3 │     4   293.274  8.55557  284.719  301.83
#>    4 │     5   303.742  9.58128  294.16   313.323

Note that jlmer() and to_lmer() are just conveniences for sanity checking the steps of a cluster-based permutation test, and thus should not be used as a general interface to fitting regression models in Julia for more serious analyses.

Julia global options

All functions involved in the cluster-based permutation analysis print progress bars that update in real time in the console. The function julia_progress() controls whether to show the progress bar and the width of the printed progress bar.

# Set Julia progress options and save old state
old_progress_opts <- julia_progress(show = FALSE, width = 30)
old_progress_opts
#> $show
#> [1] TRUE
#>
#> $width
#> [1] 50
julia_progress()
#> $show
#> [1] FALSE
#>
#> $width
#> [1] 30

# Restored old state
julia_progress(old_progress_opts)
identical(old_progress_opts, julia_progress())
#> [1] TRUE

# The syntax to reset progress options
julia_progress(show = TRUE, width = "auto")
julia_progress()

The Julia environment

The following packages are loaded into the Julia environment via jlmerclusterperm_setup():

cat(readLines(system.file("julia/Project.toml", package = "jlmerclusterperm")), sep = "\n")
#> [deps]
#> DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
#> Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
#> GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
#> JlmerClusterPerm = "2a59065a-9564-4903-82dd-0e42ce19d0e1"
#> MixedModels = "ff71e718-51f3-5ec2-a782-8ffcbfa3c316"
#> ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
#> Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
#> Random123 = "74087812-796a-5b5d-8853-05524746bad3"
#> StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
#> StatsModels = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
#> Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
#> 
#> # add DataFrames@1.5, GLM@1.8, MixedModels@4, ProgressMeter@1.7,
#> #     Random123@1.6, StatsBase@0.33, StatsModels@0.7, Suppressor@0.2
#> # add Distributions, Random
#> # dev JlmerClusterPerm
#> 
#> [compat]
#> DataFrames = "~1.3.0"
#> GLM = "~1.8"
#> JlmerClusterPerm = "=0.1.1"
#> MixedModels = "4"
#> ProgressMeter = "~1.7"
#> Random123 = "~1.6"
#> StatsBase = "~0.33"
#> StatsModels = "~0.7"
#> Suppressor = "~0.2"
#> julia = "1.8"

Optional configurations

For speed-ups in model fitting, install MKL.jl (Intel core) or AppleAccelerate.jl (Mac) and load it on start by modifying the startup.jl file.

For example, on my Windows machine the startup file is located at:

JuliaConnectoR::juliaCall("Base._local_julia_startup_file")
#> "C:/Users/jchoe/.julia/config/startup.jl"

And there I load MKL.jl:

if contains(first(Sys.cpu_info()).model, "Intel")
  using MKL
end