2

I've got a lmer model that looks similar to the following example:

library(lme4)
df<-data.frame(var_1=c(1.1,2.2,2.1,4.4,4.4,5.6,4.4,2.6,3.3,3.3,3.9,3.8,1.1,1.3,1.4,1.1,1.8,2.1),
               var_2=c(1.1,0.9,0.5,3.4,3.9,4.1,3.5,2.1,2.7,4.4,4.7,4.9,0.8,0.9,0.6,1.1,1.3,1.7),
                  var_3=c(1,1,1,2,2,2,2,3,3,4,4,4,5,5,5,6,6,6))

mod_1<-lmer(log(var_1)~var_2+ (1|var_3), data=df)

I want to generate a plot that shows the singular trendline of this model, with standard errors, without plotting out the individual lines for the random effect. To do this, I assumed a good option would be to use the plot_model function from the sjPlot package:

library(sjPlot)
plot_1 <- plot_model(mod_1,type = 'pred', colors = 'bw', line.size = 2, terms = c("var_2"))

Resulting plot

However, when I run this I receive the following error:

Model has log-transformed response. Back-transforming predictions to original response scale. Standard errors are still on the transformed scale.

Is there a way to also back-transform the standard errors so that the plot is all on the same scale? Or is this not as big of an issue as I'm thinking? Alternatively, is there an easier way to go about doing this?

2
  • 2
    Could you edit your question to be reproducible? See here for some help on how to do so: How to write a good R question with a reproducible example Commented yesterday
  • @jpsmith thanks for the input - I've updated the question now and hopefully that improved things. Commented yesterday

1 Answer 1

3

I am, frankly, not a fan of packages like these exactly because they make it very easy to do things where you have no idea what's really happening. This is only compounded by the many different modelling implementations they try to cover, leaving you guessing whether the result is even correct (relax: it is here).

The message has nothing to do with the plot: it is just the result of an internal check for transformed response. While back-transforming point estimates is easy (just exponentiate the logged values in this case), doing so for variance estimates is far from trivial and often relies on an approximation such as the delta method.

However, your plot is showing a confidence interval, which is derived entirely in the transformed scale. Here's a direct reproduction (well, not sure the x range is exactly the same) to show what is going on:

## Range of var_2 to show predictions for
x <- seq(0.5, 5, length.out=1E2)

preds <- predict(mod_1, newdata=data.frame(var_2=x, var_3=0),
                 allow.new.levels=TRUE, se.fit=TRUE, re.form=NA)

## Asymptotic 95% CI critical value
crit <- qnorm(.975)

plotdata <- data.frame(
   x=x,
   ## Back-transform predicted response
   y=exp(preds$fit),
   ## CI in log-transformed scale -> back-transform
   ymin=exp(preds$fit - crit * preds$se),
   ymax=exp(preds$fit + crit * preds$se)
)

ggplot2::ggplot(plotdata, ggplot2::aes(x=x, y=y, ymin=ymin, ymax=ymax)) +
   ggplot2::geom_ribbon(alpha=0.2) +
   ggplot2::geom_line(linewidth=2)

reproduction of sjPlot output

So now you know: the message not an issue, because when working with transformed response you'll want to refrain from back-transforming until you've done all of your calculations (which sjPlot does correctly so).

You will not want to use a back-transformed standard error for any inference, because (i) it will only be an approximation in many cases (beyond it being estimated), and (ii) it'll often lead to issues like the domain of the response not being respected (e.g. logged values must be >0 in the original scale).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.