Skip to content

Commit

Permalink
Bugfixes & documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-metherell committed May 22, 2024
1 parent 05dc7cb commit a0f490b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 49 deletions.
47 changes: 2 additions & 45 deletions docs/src/rcall.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,8 @@
# Integration with R

If you don't want to use Julia for your data analysis, but you'd like to try out `Mice.jl`, you can use it from R! The R package [`JuliaCall`](https://non-contradiction.github.io/JuliaCall/index.html) [li_juliacall_2019](@cite) and the Julia package [`RCall.jl`](https://juliainterop.github.io/RCall.jl/stable/) allow you to call Julia functions from R and vice-versa.
If you don't want to use Julia for your data analysis, but you'd like to try out `Mice.jl`, good news - you can call R functions from Julia! The Julia package [`RCall.jl`](https://juliainterop.github.io/RCall.jl/stable/) allows you to do this.

!!! note
Using `Mice.jl` from R requires Julia v1.9 or higher to be installed on your computer. If you don't have it, you can install it from [here](https://julialang.org/downloads/). You also need to install the Julia package `RCall.jl` in addition to `Mice.jl` by entering in Julia:
```
] add Mice, RCall
```

Using the combined power of these two packages, you can use `Mice.jl` without leaving R. Here's how:

```r
> library(JuliaCall)

> julia_setup()
# Julia version 1.9.4 at location /home/~USERNAME~/julia-1.9.4/bin will be used.
# Loading setup script for JuliaCall...
# Finish loading setup script for JuliaCall.

> julia_library("Mice")

> julia_library("RCall")

> julia_library("GLM")

> julia_library("Random")

> data <- read.csv("test/data/cirrhosis.csv")

> data$Stage <- as.factor(data$Stage)

> julia_assign("data", data)

> julia_command("myPredictorMatrix = makePredictorMatrix(data);")

> julia_command('myPredictorMatrix[:, ["ID", "N_Days"]] .= 0;')

> julia_command("Random.seed!(1234);") # Set random seed for reproducibility

> julia_command("imputedData = mice(data, predictorMatrix = myPredictorMatrix);")

> julia_command("analyses = with(imputedData, data -> lm(@formula(N_Days ~ Drug + Age + Stage + Bilirubin), data));")

> julia_command("results = pool(analyses);")
```

You can also start in Julia, perform your data wrangling in R, use `Mice.jl` and then send your Mids object back to R and continue analysing it there. For example:
You can start in Julia, perform your data wrangling in R, use `Mice.jl` and then send your Mids object back to R and continue analysing it there. For example:

```r
julia> using Mice, Random, RCall
Expand Down
4 changes: 1 addition & 3 deletions src/makeFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ function initialiseWorkingData(

# Convert to non-missing type
if workingData[var][1] isa CategoricalArray
for j in eachindex(workingData[var])
workingData[var][j] = convert(CategoricalArray{nonmissingtype(eltype(workingData[var][1]))}, workingData[var][j])
end
workingData[var] = [CategoricalArray{nonmissingtype(eltype(workingData[var][1]))}(workingData[var][j]) for j in 1:m]
else
workingData[var] = convert(Vector{Vector{nonmissingtype(eltype(workingData[var][1]))}}, workingData[var])
end
Expand Down
2 changes: 1 addition & 1 deletion src/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
function sampler!(
workingData::AxisVector{Vector},
workingDataPacified::AxisVector,
workingDataLevels::AxisVector{Vector},
workingDataLevels::AxisVector,
meanTraces::Vector{Matrix{Float64}},
varTraces::Vector{Matrix{Float64}},
imputeWhere::AxisVector{Vector{Bool}},
Expand Down

0 comments on commit a0f490b

Please sign in to comment.