Alan Schwartz
Alan Schwartz

Reputation: 41

Passing formulas from R to Julia via XRJulia

I'm trying to use Julia from R via XRJulia to fit some regression models. I'd like to pass the R data and formula objects to Julia. Here's where I'm at so far:

library(XRJulia)
findJulia(test=T) # Works fine
juliaEval("using MixedModels")

jlmerj<-juliaEval("
 function(f,d)
   m=fit(LinearMixedModel,f,d)
   return(m)
 end
 ")
jlmer=JuliaFunction(jlmerj)

jdata<-juliaSend(data[,c('IRI_EC','learnid')])
jfrm<-juliaEval("@formula(IRI_EC ~  1 + (1|learnid))")

At this point, jdata and jfrm appear to be Julia proxy objects:

> jdata
Julia proxy object
Server Class: Dict{String,Any}; size: 7
> jfrm
Julia proxy object
Server Class: StatsModels.Formula; size: NA

But this call fails (vaguely):

res<-jlmer(jfrm,jdata)
Error: Evaluating Julia expression: R_1_7(R_1_9, R_1_29)

(R_1_7, etc. are the names of the functions as returned by juliaName(jlmerj), juliaName(jfrm), juliaName(jdata)). The same model syntax works fine in JuliaCall (I'm trying to convert to XRJulia because JuliaCall only seems to print out the results of the fit without returning anything I can usefully convert back to an R object).

Other juliaEvals of functions and the like work, but I can't get anything with formula objects to work.

Has anyone done this? How?

Upvotes: 0

Views: 377

Answers (1)

Consistency
Consistency

Reputation: 2922

Could you please give an example data set to run your code?

After checking this again, I suspect the problem is not with the formula but with the conversion of R data.frame in XRJulia, since in your message you can see that jdata is a proxy of a Dict (dictionary) instead of a DataFrame or similar things in julia. And I also (briefly) check in MixedModels and it seems that the package can't deal with a Dict as a dataset.

The usage of JuliaCall is somewhat similar to XRJulia but it will convert R data.frame to julia DataFrame. And the use of XRJulia in your examples seems okay with me. So the fact that this works with JuliaCall but not XRJulia also implies that the problem is with the conversion of data.frame in XRJulia.

It may be that XRJulia currently lacks the ability to convert data.frames in R. If this is the case, I can think two (theoretically) possible ways to deal with the problem, the first is to read the dataset into julia directly as a DataFrame, the other is to convert the Dict to DataFrame in your jlmerj function. And if an example dataset is given, I can try the solutions on the dataset.

BTW, authors of MixedModels has spoken with me about possibilities to build an R package based on MixedModels using JuliaCall, if that happens, I think it can be a lot of easier to try MixedModels from R.

Update 1: an example on converting dataframe in XRJulia for the specific minidataset

library(XRJulia)
findJulia(test = T) # Works fine
juliaEval("using MixedModels")

juliaEval("using DataFrames")

jlmerj <- juliaEval("
                  function(f,d)
                  m=fit(LinearMixedModel,f,d)
                  return(m)
                  end
                  ")
jlmer <- JuliaFunction(jlmerj)

mindata <- data.frame(IRI_EC = c(15, 14, 27, 0, 22, 16, 23, 17, 20, 26), learnid = factor(1:10))

jfrm <- juliaEval("@formula(IRI_EC ~  1 + (1|learnid))")

jIRI_EC <- juliaSend(mindata$IRI_EC) ## send columns one by one
## first convert factors to integers and convert back in julia
jlearnid <- juliaCall("CategoricalArrays.CategoricalArray", juliaSend(as.integer(mindata$learnid))

## combine columns to create dataframe in julia
jdata <- juliaEval(paste0('DataFrame(Dict([(:IRI_EC,', juliaName(jIRI_EC),
                          '), (:learnid, ', juliaName(jlearnid), ')]))'))

res <- jlmer(jfrm,jdata)

res

Upvotes: 1

Related Questions