Confidence Band in GAMM I would like to know why, when adjusting the following model, the confidence band is wider in one of the factors than in the other, if the variability of the raw data seems to be the same in both factors.


Maybe it's because there is no significance for the smooth effect?
To graph I am using the visreg library.
I appreciate any idea.
 A: The problem I think is a lack of identifiability on the Year smooths when you have a global smooth plus factor-by smooths of the same variable. Adding is the random factor-smooth probably isn't helping.
Often one needs to change the penalty on the by smooths so that they are more easily identifiable from the data. One way to do that is to place the penalty on the integrated squared first derivative of the by smooths. This has the effect of penalising deviations from a flat line. Now the smooth effect of Year in your groups is given by
$$ \gamma_{j[i]} + f(Year_{i}) + f_j(Year_{i}) $$
where $\gamma_{j[i]}$ is the $j$th group mean for the $i$th observation, and where the $f_j$ are penalised for deviating from a flat function. When coupled with the global smoother deviation from a flat function means deviation from the global function.
To do this for your model, you would need:
gam(pH ~ Impacted + s(Year) + s(Year, by = Impacted, m = 1) +
         s(Year, Stnumber, bs = 'fs'),
    method = 'REML', data = X)

The issue doesn't crop up as often with the "fs" basis as those bases are fully penalised and include a random intercept, so the penalties help I believe to make these terms identifiable.
You may also just not have enough data to uniquely identify three different kinds of smooth effect of Year. Do you need both the global and the Impacted-level smooths? Could you just work with the Impacted level smooths as in
gam(pH ~ Impacted + s(Year, by = Impacted) + s(Year, Stnumber, bs = 'fs'),
    method = 'REML', data = X)

where we no longer need the m = 1 bit for the first derivative-based penalty?
Nothing is really lost here as you could use methods to compare the by smooths to see how similar they are.
