Search code examples
rgam

Why won't my code allow me to specify what I want a variable evaluated 'by'?


I'm trying to create a gamm of zooplankton populations (as a function of temporal variables) in which the sample_site (categorical variable) has a random effect on the populations (retrocurva is a species of zooplankton).

jr1 <- gamm(retrocurva ~ s(DOY, by = "Sample_site") + s(Year, by = "Sample_site") + s(Sample_site, bs = "re"), data = zooskeleton2)

but I keep getting this error:

Error in terms.formula(formula, data = data) : invalid model formula in ExtractVars

The code works if I make these modifications:

jr1 <- gamm(retrocurva ~ s(DOY) + s(Year), data = zooskeleton2)

but the moment I add Sample_Site back in there (even without specifying it within s())...

jr1 <- gamm(retrocurva ~ s(DOY) + s(Year) + s(Sample_site), data = zooskeleton2)

I get this error:

Error in names(dat) <- object$term : 'names' attribute [1] must be the same length as the vector [0]

Any ideas on what I'm doing wrong? Here is a subset of the 'zooskeleton2' data frame I'm operating from for reference.

retrocurva  Sample_site DOY Year
<dbl>           <chr>  <dbl><dbl>
5.054528e-01    3-996   137 1995    
7.192713e-01    3-996   137 1995    
1.827290e-01    8-994   137 1995    
7.925904e-01    8-994   137 1995    
5.506497e-01    27-918  142 1995    
2.001039e-01    29-905  142 1995    
5.606469e-01    36-873  143 1995    
4.903655e-01    37-890  143 1995    
6.504399e+01    14-972  152 1995    
5.058930e+01    16-970  152 1995    

Solution

  • There are two problems:

    1. you need to code Sample_site as a factor, and
    2. you need to pass this variable to the by argument unquoted

    This should fix things:

    zooskeleton2 <- transform(zooskeleton2, Sample_site = factor(Sample_site))
    jr1 <- gamm(retrocurva ~ s(DOY, by = Sample_site) +
                  s(Year, by = Sample_site) +
                  s(Sample_site, bs = "re"),
                data = zooskeleton2)