Using phia package to test interactions with latest version of afex
Table of Contents
- Introduction
- Understanding the Issue
- Background: The Role of phia and afex in R
- The Latest Version of afex (aov_ez)
- Fixing the Issue with phia Package
- Example Usage
Introduction
In this blog post, we will explore how to use the phia package in R to test interactions with the latest version of afex, which is now known as aov_ez. The issue at hand arises from a change in the way aov_ez returns its data. We will delve into this change and provide a solution using the phia package.
Understanding the Issue
The original question posed on Stack Overflow highlights an issue with testing interactions using the testInteractions function from the phia package when working with the latest version of afex, which is now aov_ez. The problem arises because the data used by phia in this context is no longer output as a model’s idata but rather in its Anova table.
Background: The Role of phia and afex in R
For those unfamiliar with phia or afex, let us provide a brief overview of each package. phia is a package designed to test interactions between variables within the context of generalized linear mixed models (GLMMs). It provides an easy-to-use interface for performing pairwise tests of contrasts, which can be essential in model diagnostics and hypothesis testing.
On the other hand, afex was previously known as ez.glm but has been updated to aov_ez with version 0.4. This package offers a simple way to fit GLMMs using R’s built-in linear algebra functions, which can be useful in many statistical applications.
The Latest Version of afex (aov_ez)
With the update from ez.glm to aov_ez, several changes were made in the afex package. Notably, some data that was previously output as part of a model’s idata attribute is now nested within the Anova table.
Fixing the Issue with phia Package
Given this change in how aov_ez returns its data, we can identify the solution by recognizing where phia expects to find the idata. The key insight here is that instead of using model$idata, we should utilize model$Anova$idata.
Here is a step-by-step guide on how to fix this issue:
Install and Load Required Packages
First, ensure you have both packages installed:
# Load required libraries
library(phia)
library(aov_ez)
Create a Model with aov_ez
Create an aov_ez model as you normally would. Since we’re using aov_ez, you might need to update your model formula accordingly.
# Generate example data
data("obk.long")
head(obk.long)
# Fit the aov_ez model
b <- aov_ez("id", "value", obk.long, within = c("phase", "hour"), print.formula = TRUE)
Perform Contrast Using phia
Now that we have our model and understand how phia expects its data, we can use the testInteractions function as shown in the example below.
# Use testInteractions with b$lm
testInteractions(b$lm, pairwise = "phase", idata=b$Anova$idata)
Example Usage
Here is a full example that demonstrates how to use phia to test interactions with the latest version of afex, which is now aov_ez.
# Run the old way, to illustrate method
remove.packages('afex')
install.packages("http://www2.uaem.mx/r-mirror/src/contrib/Archive/afex/afex_0.3-42.tar.gz",repos=NULL, type='source')
require(afex)
packageDescription('afex')
data("obk.long")
head(obk.long)
a <- ez.glm("id", "value", obk.long, within = c("phase", "hour"), print.formula = TRUE, return='full')
a
nice.anova(a$Anova)
# Run a contrast with phia
install.packages('phia')
require('phia')
testInteractions(a$lm, pairwise = "phase", idata=a$idata)
# Now, with the latest afex
detach("package:afex", unload=TRUE)
install.packages('aov_ez')
require(aov_ez)
packageDescription('aov_ez')
b <- aov_ez("id", "value", obk.long, within = c("phase", "hour"), print.formula = TRUE)
# This issue here is that no idata comes out of aov_ez in its latest version, so scripts no longer work...
testInteractions(b$lm, pairwise = "phase", idata=b$idata)
# The data is, however, nested in the Anova table...
testInteractions(b$lm, pairwise = "phase", idata=b$Anova$idata)
Conclusion
By understanding how phia works with the latest version of afex, which now uses aov_ez, users can easily perform model diagnostics and test interactions between variables. The key takeaway is that instead of using model$idata, we use model$Anova$idata when working with models created by aov_ez.
Last modified on 2023-11-01