Helpers for converting model specifications in R to Julia equivalents
Source:R/jl_-model.R
jl-helpers-model.Rd
Helpers for converting model specifications in R to Julia equivalents
Usage
jl_formula(formula)
jl_contrasts(df, cols = NULL, show_code = FALSE)
jl_data(df)
jl_family(family = c("gaussian", "binomial", "poisson"))
Examples
# \donttest{
jlme_setup(restart = TRUE)
#> Starting Julia (v1.10.5) ...
#> Successfully set up Julia connection. (30s)
# Set up model data in R
x <- mtcars
x$cyl_helm <- factor(x$cyl)
contrasts(x$cyl_helm) <- contr.helmert(3)
colnames(contrasts(x$cyl_helm)) <- c("4vs6", "4&6vs8")
# Formula conversion with
julia_formula <- jl_formula(mpg ~ am * cyl_helm)
julia_formula
#> <Julia object of type FormulaTerm{Term, Tuple{Term, Term, InteractionTerm{Tuple{Term, Term}}}}>
#> FormulaTerm
#> Response:
#> mpg(unknown)
#> Predictors:
#> am(unknown)
#> cyl_helm(unknown)
#> am(unknown) & cyl_helm(unknown)
# Data frame conversion
julia_data <- jl_data(x)
julia_data
#> <Julia object of type Table{@NamedTuple{mpg::Float64, cyl::Float64, disp::Float64, hp::Float64, drat::Float64, wt::Float64, qsec::Float64, vs::Float64, am::Float64, gear::Float64, carb::Float64, cyl_helm::String}, 1, @NamedTuple{mpg::Vector{Float64}, cyl::Vector{Float64}, disp::Vector{Float64}, hp::Vector{Float64}, drat::Vector{Float64}, wt::Vector{Float64}, qsec::Vector{Float64}, vs::Vector{Float64}, am::Vector{Float64}, gear::Vector{Float64}, carb::Vector{Float64}, cyl_helm::Vector{String}}}>
#> Table with 12 columns and 32 rows:
#> mpg cyl disp hp drat wt qsec vs am gear carb ⋯
#> ┌─────────────────────────────────────────────────────────────────────
#> 1 │ 21.0 6.0 160.0 110.0 3.9 2.62 16.46 0.0 1.0 4.0 4.0 ⋯
#> 2 │ 21.0 6.0 160.0 110.0 3.9 2.875 17.02 0.0 1.0 4.0 4.0 ⋯
#> 3 │ 22.8 4.0 108.0 93.0 3.85 2.32 18.61 1.0 1.0 4.0 1.0 ⋯
#> 4 │ 21.4 6.0 258.0 110.0 3.08 3.215 19.44 1.0 0.0 3.0 1.0 ⋯
#> 5 │ 18.7 8.0 360.0 175.0 3.15 3.44 17.02 0.0 0.0 3.0 2.0 ⋯
#> 6 │ 18.1 6.0 225.0 105.0 2.76 3.46 20.22 1.0 0.0 3.0 1.0 ⋯
#> 7 │ 14.3 8.0 360.0 245.0 3.21 3.57 15.84 0.0 0.0 3.0 4.0 ⋯
#> 8 │ 24.4 4.0 146.7 62.0 3.69 3.19 20.0 1.0 0.0 4.0 2.0 ⋯
#> 9 │ 22.8 4.0 140.8 95.0 3.92 3.15 22.9 1.0 0.0 4.0 2.0 ⋯
#> 10 │ 19.2 6.0 167.6 123.0 3.92 3.44 18.3 1.0 0.0 4.0 4.0 ⋯
#> 11 │ 17.8 6.0 167.6 123.0 3.92 3.44 18.9 1.0 0.0 4.0 4.0 ⋯
#> 12 │ 16.4 8.0 275.8 180.0 3.07 4.07 17.4 0.0 0.0 3.0 3.0 ⋯
#> 13 │ 17.3 8.0 275.8 180.0 3.07 3.73 17.6 0.0 0.0 3.0 3.0 ⋯
#> 14 │ 15.2 8.0 275.8 180.0 3.07 3.78 18.0 0.0 0.0 3.0 3.0 ⋯
#> 15 │ 10.4 8.0 472.0 205.0 2.93 5.25 17.98 0.0 0.0 3.0 4.0 ⋯
#> 16 │ 10.4 8.0 460.0 215.0 3.0 5.424 17.82 0.0 0.0 3.0 4.0 ⋯
#> 17 │ 14.7 8.0 440.0 230.0 3.23 5.345 17.42 0.0 0.0 3.0 4.0 ⋯
#> ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱
# Contrasts construction (`show_code = TRUE` pretty prints the Julia code)
julia_contrasts <- jl_contrasts(x, show_code = TRUE)
#> Dict(
#> :cyl_helm => HypothesisCoding(
#> [
#> -1/2 1/2 0
#> -1/6 -1/6 1/3
#> ];
#> levels = ["4", "6", "8"],
#> labels = ["4vs6", "4&6vs8"],
#> ),
#> )
julia_contrasts
#> <Julia object of type Dict{Symbol, HypothesisCoding{Matrix{Float64}, Matrix{Float64}}}>
#> Dict{Symbol, HypothesisCoding{Matrix{Float64}, Matrix{Float64}}} with 1 entry:
#> :cyl_helm => HypothesisCoding{Matrix{Float64}, Matrix{Float64}}([-0.5 0.5 0.0…
# Family conversion
julia_family <- jl_family("binomial")
julia_family
#> <Julia object of type Bernoulli{Float64}>
#> Bernoulli{Float64}(p=0.5)
stop_julia()
# }