0

I think this is a basic coding error, but I can't work out what is wrong. I haven't provided a data sample, as it is a massive and complex dataset and I don't think a sample is needed for this. I have given the numeric range of the data below. I am trying to plot an interaction effect from a lme4 model using the effects package predictorEffect function.

My interacting predictor variables are continuous numeric:

  • x1 variable (tree), range: 0 - 1.5
  • x2 variable (pH), range: 3-9

I plotted the interaction initially using:

plot(predictorEffect("tree", model1)

Which gives the following display: enter image description here

The x2 (pH) variable values used are 3,5,6,7,9 - however I want the to use the values 4,5,6,7,8. So I used the xlevels argument to attempt to set the values:

plot(predictorEffect("tree", divnew, xlevels=list(x1=5, x2=c(4,5,6,7,8))))

But this doesn't do anything - I have read the documentation over and over and this seems like it should work. Am I missing something?

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
user197410
  • 31
  • 4
  • 3
    You don't have to provide your original dataset, but it would easier to answer if you used an example dataset (i.e., a dataframe built into R or something smaller and randomly generated) to make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – jrcalabrese Feb 23 '23 at 14:29
  • I would do this, but if I subset my data or create a random dataset and run an lmer on it there is no interaction effect to plot. – user197410 Feb 23 '23 at 15:02

1 Answers1

0

I'm not sure what you are trying to do. You haven't provided a reproducible dataset and it makes it pretty difficult for others to chip in and help without it. It also appears you are doing something somewhat odd, you're trying to change the predictor values and the estimated y values at the same time. The y-axis will necessarily change by virtue of changing the predictor values. What specifically is the purpose of this? To me this isn't super helpful, but perhaps you can elaborate in a comment here.

Instead of trying what you did, I have simulated some data to try to resemble yours, then fit a lme4 regression. Thereafter I plot an interaction with a different function, the plot_model function from the sjPlot package. First, the requisite packages are loaded below, one for the mixed models regression and one for the plotting function.

#### Load Libraries ####
library(lmerTest)
library(sjPlot)

Then I tried simulating your data to at least resemble what you described and created a data frame called df below.

#### Simulate Data ####
set.seed(123)
tree <- sample(seq(0,1.5,by=.01),
       100)
ph <- sample(seq(3,9,by=.01),
             100)
y <- rnorm(n=1000)
re <- factor(round(rbinom(n=1000,size=5,prob = .5)),
       labels = c("site1","site2","site3","site4","site5","site6"))
df <- data.frame(tree,ph,y,re)
df

Then I fit the model to a linear mixed model with this code.

#### Fit Model ####
fit <- lmer(y ~ tree*ph + (1|re),
            df)
summary(fit)

To plot the interactions for this, one can use the plot_model function mentioned previously. We can specify method = "int" to plot interactions, then specify mdrt.values = "meansd" to get it to plot the interactions using slices with the mean and one standard deviation above and below the mean. Of course you also select the terms with the interaction with the terms argument. Because it is a ggplot object, you can also add some extra customization of the labels, so I added an x/y axis name and title.

#### Plot ####
plot_model(fit,
           type = "int",
           mdrt.values = "meansd",
           terms = c("tree","ph"))+
  labs(x="Tree Size",
       y="Growth Rate",
       title = "Interaction Between pH Levels and Tree Size on Growth Rate")

Which should look like this:

enter image description here

While this doesn't give you the exact values you want, it at least gives you meaningful ones. You now have values that resemble what z-score changes affect the direction of the interaction.

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30