mjskay / ARTool

R Package for Aligned Rank Transform for Nonparametric Factorial ANOVAs
58 stars 9 forks source link

art.con: error when building model with within-subject style Error term #37

Open thomaskoehn11 opened 1 year ago

thomaskoehn11 commented 1 year ago

When I do

art_model <- art(values ~ X*Y + Error(participant / (X*Y)), data = my_data)

and run

art.con(art_model, "X:Y")

I receive

Error in eval(predvars, data, env) : object 'X' not found

Mind that my_data is a data.frame and X, Y and participant are factors. values is a num.

Mind that the following commands do work, and produce sensible results:

summary(art_model)
anova(art_model)
art.con(art_model, "X")
art.con(art_model, "Y")

And likewise, if I create the model instead with a shorter Error term like this:

art_model <- art(values ~ X*Y + Error(participant), data = my_data)

The original problematic line works and produces sensible results:

art.con(art_model, "X:Y")

Additional alternations, in the hope to provide more help – mind the adjusted Error terms and error message:

art_model <- art(values ~ X*Y + Error(participant / (X)), data = my_data)
art.con(art_model, "X:Y")
=> Error in eval(predvars, data, env) : object 'X' not found
art_model <- art(values ~ X*Y + Error(participant / (Y)), data = my_data)
art.con(art_model, "X:Y")
=> Error in eval(predvars, data, env) : object 'Y' not found

My environment:

Thanks for the help @mjskay , and thanks for ARTool! :)

thomaskoehn11 commented 1 year ago

I can also confirm that the issue is not dataset-specific, as I just ran into the same error with a completely different dataset (only similarly is that both use the same amount of participants & conditions, but measured completely different data).

I can also confirm that both datasets were either fully or primarily non-normally distributed, and summary(art_model) showed all the expected items as 0.

thomaskoehn11 commented 1 year ago

@mjskay Hi Matthew. Apologies for pinging. Any chance you can look into this? Also happy to hear if I got something wrong and the above is actually expected behavior. Thanks!

Frauke13 commented 1 year ago

I experience the same issue on two datasets. The art-function provides reasonable results, and so does the art.con function if I try to calculate the post-hoc tests for the main effects only. As soon as I try to calculate the interaction, I get the exact same error message as described above. The interaction contrasts (comparing differences of differences; interaction = TRUE) is working.

biosmanager commented 1 year ago

@mjskay This is really an issue. Is there any fix planned?

mjskay commented 1 year ago

Can you give a test dataset (on synthetic data will do) giving the error with a reprex? Thanks!

biosmanager commented 1 year ago

@mjskay Sure thing:

library(ARTool)
data(Higgins1990Table5, package = "ARTool")

m <- art(DryMatter ~ Moisture*Fertilizer + Error(Tray/(Moisture*Fertilizer)), data=Higgins1990Table5)
print(art.con(m, ~ Moisture*Fertilizer))
#> Error in eval(predvars, data, env): object 'Moisture' not found

Created on 2023-08-28 with reprex v2.0.2

A slightly different error term gives this error:

library(ARTool)
data(Higgins1990Table5, package = "ARTool")

m <- art(DryMatter ~ Moisture*Fertilizer + Error(Tray), data=Higgins1990Table5)
print(art.con(m, ~ Moisture*Fertilizer))
#> Error in X[ii, ii, drop = FALSE] %*% y[ii]: non-conformable arguments

Created on 2023-08-28 with reprex v2.0.2

mjskay commented 1 year ago

Ah okay, I see the issue now. Fixing it for the repeated measures ANOVA is a bit of a pain, but fixing it for mixed effects models was straightforward. I put a fix on the dev branch for mixed effects models, for which you should be able to create an equivalent specification. A model like this:

m = art(values ~ X*Y + Error(participant / (X*Y)), data = my_data)

As random slopes in a mixed effects model would be:

m = art(values ~ X*Y + (X*Y | participant), data = my_data)

Which should now work with something like:

art.con(m, ~ X*Y)

To use this, you have to install the dev branch first via:

devtools::install_github("mjskay/ARTool@dev")

Let me know if that helps.

biosmanager commented 1 year ago

Thanks for the effort! @mjskay I am not familiar with random slopes - is this really equivalent to a repeated measures model in all cases? Do you think that a fix for RM can also be implemented soon? We are currently conducting statistical analysis for a paper deadline ;)

mjskay commented 1 year ago

Ah, personally I would prefer the random slopes model, but you can get the precisely equivalent model in lmer using the same nesting syntax; something like:

m = art(values ~ X*Y + (1 | participant / (X*Y)), data = my_data)

See the Remark at the end of section 6.2.2 here.

You can verify by using aov() and lmer() directly on the data that these models are equivalent.

mjskay commented 1 year ago

(as I am under a paper deadline myself and the random effects syntax should yield the same model, I am unlikely to fix the error term syntax at the moment - it will require more hairy rewriting of internal code)