You have some problems with your data; you can't have a random effect of Plant
as a factor and get the plot you showed in the link in comment to @Aaron's Answer. Are you sure that Plant
is a factor with more than 1 level? If not, you need to code your data correctly to get a random intercept per plant. Also, can you include both RIL
and Plant
level effects? Once you've accounted for the separate Plant
effects (intercepts), won't that also logically account for the genetic line effects also?
Second, if you are using bs = 'fs'
, you need to pass in a continuous variable and a factor; so far you only pass in DateNum
. At the moment you have
s(DateNum, k = 9, bs = "fs")
and I think you wanted
s(DateNum, Trt, k = 9, bs = "fs")
This model is similar to the one proposed by @Aaron, but is somewhat different in detail. The full model might be
gam(Total ~ Trt * RIL + s(DateNum, Trt, k = 9, bs = "fs") + s(Plant, bs = "re"),
data=ce1230)
but I suspect even that is wrong? (For fs
smooths you don't need the parametric Trt
.) So I think,
gam(Total ~ RIL + Trt:RIL + s(DateNum, Trt, k = 9, bs = "fs") +
s(Plant, bs = "re"), data=ce1230)
Where the main effect of Trt
is actually contained in the fs
smooth, so we don't specify it parametrically.
The main difference between this model and @Aaron's in that here, there is a single smoothness parameter for the smooths of DateNum
by Trt
, whereas in @Aaron's answer each of the smooths gets it's own smoothness parameter. This boils down to a choice between whether you expect similar wiggliness (the shapes of the smooths can be different) for each smooth or whether you expect some of the four smooths to be a lot wigglier than others?
What you seem to want is interactions between RIL
and Trt
plus separate smooths for each Trt
. But do you want separate smooths for each RIL
and Trt
combination? That would require a separate variable formed by the combinations of RIL
and Trt
available in your data:
ce1230 <- transform(ce1230,
RILTrt = interaction(RIL, Trt, drop = TRUE))
And then you could fit
gam(Total ~ s(DateNum, RILTrt, k = 9, bs = "fs") +
s(Plant, bs = "re"), data=ce1230)
or
gam(Total ~ s(DateNum, k = 9, by = RILTrt) +
s(Plant, bs = "re"), data=ce1230)
depending on whether you wanted similar wigliness (use bs = 'fs'
) or different wigglinesses (use by
) for each estimated smooth of DateNum
.
The model also needs to respect the non-negative nature of the response; you have counts and you can't have negative counts of anything. Using family = poisson
or family = nb
would be reasonable starting points.
Trt
and theDateNum
smooth, which is what I suspect you want, I believe you have to addby=Trt
to the interior of thes(DateNum,...
smooth term and you will also need to haveTrt
as a factor term independent ofRIL
, i.e.,Total ~ Trt + Trt*RIL + s(DateNum, by=Trt, k=9, bs="fs") + ...
. $\endgroup$RIL
and make separate smooths, see the help on "Specifying generalized additive models";?gam.models
, particularly the section onby
and the corresponding example. $\endgroup$bs="re"
makes it a random effect. $\endgroup$