visreg visualization of mgcv results (GAM) I fitted a GAM with random effects using mgcv, and I've noticed that the visualization of the smooths using visreg does not appear to match the output of mgcv's plots:
library(ggplot2)
library(visreg)
library(mgcv)

fit <- gam(log(Response) ~ s(Tag, by=Wundinfektionsstatus) + s(PatientenID, bs = "re") + s(PatientenID, Tag, bs = "re"), data=zeitreihen, method="REML")

intercept <- fit$coefficients[["(Intercept)"]]

plot(fit, trans = exp, shift = intercept, shade=T, residuals=T, select=1)
plot(fit, trans = exp, shift = intercept, shade=T, residuals=T, select=2)

visreg(fit, "Tag", "Wundinfektionsstatus", gg=T, type="conditional", overlay=T, partial=F, rug=F, ylab="Response", trans=exp)




The first two plots match the data pretty well, specifically the constant decrease in the second plot. Note that in the visreg plot, that curve appears almost constant with a sharp uptick at the end, and the height of the first maximum does not match.
Any ideas how this disconnect comes about?
 A: The difference arises because you are ignoring the intercept (& the coef for the non-reference levels of the factor; see first Note) when you go via the mgcv:::plot.gam() method. The visreg output is showing the smooth effect of each variable conditional upon the other terms in the model. The blue line shows the estimated smooth effect of Tag, including the model intercept.
Note your model may be wrong; you probably should include Wundinfektionsstatus as the by-factor smooths are centered about zero and hence you need a parametric fixed effect (in this case) to raise adjust the mean response of each level up/down. Without it, the model has to build this into the smooth functions.
You can achieve the same thing in mgcv by predicting from the model over a grid of values for Tag (in this case of the blue line) repeated for each level of your factor and you'll need to include a dummy patient ID, which we'll exclude:
## Assuming data in df
pred_df <- with(df, expand.grid(Tag = seq(min(Tag), max(Tag), length = 100),
                                Wundinfektionsstatus = levels(Wundinfektionsstatus),
                                PatientenID = sample(PatientenID, 1)))

pred <- predict(fit, newdata = pred_df, type = 'link', se.fit = TRUE,
                exclude = c("s(PatientenID)", "s(PatientenID,Tag)"))
pred_df <- cbind(pred_df, as.data.frame(pred))

## create confidence interval and back transform
pred_df <- transform(pred_df,
                     fitted_response = exp(fit),
                     fitted_upper    = exp(fit + (2 * se.fit)),
                     fitted_lower    = exp(fit - (2 * se.fit)))

pred_df should be in a format to plot with ggplot2 as you see fit:
library('ggplot2')

ggplot(pred_df, aes(x = Tag, y = fitted_response)) +
  geom_ribbon(aes(fill = Wundinfektionsstatus, ymin = fitted_lower,
                  ymax = fitted_upper)) +
  geom_line(aes(colour = Wundinfektionsstatus))

Note: thread the above as pseudo-code as I wrote this from memory and it is untested as there wasn't a reproducible example.
