JuliaInterop / RCall.jl

Call R from Julia
Other
318 stars 59 forks source link

rput gives formula error for only some models #487

Closed oricon closed 1 year ago

oricon commented 1 year ago

I'm not sure if this is an issue with rput or MixedModels, but I'll try here first. I've run into an issue where I cannot move a model from julia to R. It doesn't happen with most models, but when it occurs with one model, any subsequent model will cause the same issue. For example, if I run the following model, I can move it to R with rput and write it out.

julia> rt_bis_m2 = fit(MixedModel, @formula(logRT ~ 1 + alt * prevRew * rewCond * rewExpectancy * genderid * bisC + (1 + alt * prevRew * rewCond | participant)), rtdata, contrasts = Dict(:alt => EffectsCoding(base = "repeat"), :prevRew => EffectsCoding(base = "hi"), :rewCond => EffectsCoding(base = "Hi"), :rewExpectancy => EffectsCoding(base = "high")))
julia> rt_bis_m2_j = (rt_bis_m2, rtdata);
julia> @rput rt_bis_m2_j;
┌ Warning: RCall.jl: Warning in optwrap(optimizer, devfun, getStart(start, rho$pp), lower = rho$lower,  :
│   convergence code 5 from nloptwrap: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached.
│ boundary (singular) fit: see help('isSingular')
└ @ RCall ~/.julia/packages/RCall/LWzAQ/src/io.jl:172

julia> R"write_rds(rt_bis_m2_j, 'Rdata/rt_bis_m2_j.Rds')";

But then when I run the following model and try to move it to R I get an error. Then if I try to repeat the above rput command I get the error for that model as well.

julia> rt_id_m5 = fit(MixedModel, @formula(logRT ~ 1 + alt * prevRew * rewCond * rewExpectancy * genderid * basC * bisC + zerocorr(1 + alt + prevRew + rewCond | participant)), rtdata, contrasts = Dict(:alt => EffectsCoding(base = "repeat"), :prevRew => EffectsCoding(base = "hi"), :rewCond => EffectsCoding(base = "Hi"), :rewExpectancy => EffectsCoding(base = "high")))
julia> rt_id_m5_j = (rt_id_m5, rtdata);
julia> @rput rt_id_m5_j;
[ Info: Swapping to afex::lmer_alt instead of lme4::g/lmer from here on out...
ERROR: REvalError: Warning: Due to missing values, reduced number of observations to 26183
Numerical variables NOT centered on 0: basC, bisC
If in interactions, interpretation of lower order (e.g., main) effects difficult.
Warning: Using formula(x) is deprecated when x is a character vector of length > 1.
  Consider formula(paste(x, collapse = " ")) instead.
Error in str2expression(x) : <text>:2:10: unexpected symbol
1: rid:basC + alt:bisC + prevRew:bisC + rewCond:bisC + rewExpectancy:bisC + genderid:bisC + basC:bisC + alt:prevRew:rewCond + alt:prevRew:rewExpectancy + alt:rewCond:rewExpectancy + +(1+re1.altsw
2: logRT    prevRew
            ^
julia> @rput rt_bis_m2_j;
ERROR: REvalError: Numerical variables NOT centered on 0: bisC
If in interactions, interpretation of lower order (e.g., main) effects difficult.
Warning: Using formula(x) is deprecated when x is a character vector of length > 1.
  Consider formula(paste(x, collapse = " ")) instead.
Error in str2expression(x) : <text>:2:10: unexpected symbol
1: w:genderid + alt:rewCond:genderid + prevRew:rewCond:genderid + +(1+re1.altswitch+re1.prevRewlo+re1.rewCondLo+re1.altswitch_by_prevRewlo+re1.altswitch_by_rewCondLo+re1.prevRewlo_by_rewCondLo+re
2: logRT    alt
palday commented 1 year ago

The key is the line

[ Info: Swapping to afex::lmer_alt instead of lme4::g/lmer from here on out...
palday commented 1 year ago

I think this is an issue with afex on the R side and potentially how that interacts with some of the logic in JellyMe4.

oricon commented 1 year ago

Thanks, I removed afex and I'm getting other errors, but I think they are originating from JellyMe4.

julia> @rput rt_id_m5_j;
ERROR: ArgumentError: zerocorr for factors with more than 2 levels not supported without using afex::lmer_alt
Stacktrace:
 [1] convert_julia_to_r(f::FormulaTerm
.
.
.
   @ JellyMe4 ~/.julia/packages/JellyMe4/wxnld/src/formula.jl:42
 [2] sexp(#unused#::Type{RCall.RClass{:lmerMod}}, x::Tuple{LinearMixedModel{Float64}, DataFrame})
   @ JellyMe4 ~/.julia/packages/JellyMe4/wxnld/src/lmerMod.jl:93
 [3] sexp
   @ ~/.julia/packages/RCall/LWzAQ/src/convert/default.jl:0 [inlined]
 [4] setindex!(e::Ptr{EnvSxp}, v::Tuple{LinearMixedModel{Float64}, DataFrame}, s::Symbol)
   @ RCall ~/.julia/packages/RCall/LWzAQ/src/methods.jl:553
 [5] setindex!(e::RObject{EnvSxp}, v::Tuple{LinearMixedModel{Float64}, DataFrame}, s::Symbol)
   @ RCall ~/.julia/packages/RCall/LWzAQ/src/methods.jl:562
 [6] top-level scope
   @ REPL[12]:1