0

I have defined a generalised linear model as follows:

glm(formula = ParticleCount ~ ParticlePresent + AlgaePresent + 
ParticleTypeSize + ParticlePresent:ParticleTypeSize + AlgaePresent:ParticleTypeSize, 
family = poisson(link = "log"), data = PCB)

and I have the below significant interactions

                                 Df  Deviance  AIC  LRT Pr(>Chi)   
<none>                               666.94  1013.8                  
ParticlePresent:ParticleTypeSize  6  680.59  1015.4 13.649 0.033818 * 
AlgaePresent:ParticleTypeSize     6  687.26  1022.1 20.320 0.002428 **

I am trying to proceed with a posthoc test (Tukey) to compare the interaction of ParticleTypeSize using the lsmeans package. However, I get the following message as soon as I proceed:

library(lsmeans)
leastsquare=lsmeans(glm.particle3,~ParticleTypeSize,adjust ="tukey")

Error in `contrasts<-`(`*tmp*`, value = contrasts.arg[[nn]]) : 
  contrasts apply only to factors

I've checked whether ParticleTypeSize is a valid factor by applying:

 l<-sapply(PCB,function(x)is.factor(x))
 l
      Sample     AlgaePresent  ParticlePresent ParticleTypeSize 
        TRUE            FALSE            FALSE             TRUE 
   ParticleCount 
       FALSE 

I'm stumped and unsure as to how I can rectify this error message. Any help would be much appreciated!

1 Answers1

0

That error happens when the variable you specify is not a factor. You tested and found that it is, so that's a mystery and all I can guess is that the data changed since you fit the model. So try re-fitting the model with the present dataset.

All that said, I question what you are trying to do. First, you have ParticleTypeSize interacting with two other predictors, which means it is probably not advisable to look at marginal means (lsmeans) for that factor. The fact that there are interactions means that the pattern of those means changes depending on the values of the other variables.

Second, are AlgaePresent and ParticlePresent really numeric variables? By their names, they seem like they ought to be factors. If they are really indicators (0 and 1), that's OK, but it is still cleaner to code them as factors if you are using functions like lsmeans where factors and covariates are treated in distinctly different ways.

BTW, the lsmeans package is being deprecated, and new developments are occurring in its successor, the emmeans package.

Russ Lenth
  • 5,922
  • 2
  • 13
  • 21
  • Thank you for your comment. I managed to fit it into the model by treating AlgaePresent and ParticlePresent as factors, as follows: PCB$AlgaePresent<-as.factor(PCB$AlgaePresent) PCB$ParticlePresent<-as.factor(PCB$ParticlePresent) In response to your questions, AlgaePresent and ParticlePresent are input as presence or absence (T/F). ParticleTypeSize is a concatenated factor of particle type and size which was concatenated due to lack of overlap should they be treated separately. I will look into the emmeans package. Thanks! – tiptothetop Apr 15 '18 at 01:57
  • Hmmmm. I guess I didn't even know that one could use a logical variable as a predictor, and with that insight, I can reproduce the error you experienced (using a different example). Now that I know about it, presumably I can fix it. So in a month or so, a new update of **emmeans** will come out that can handle logical predictors. – Russ Lenth Apr 15 '18 at 02:15
  • OK, it was an extremely easy fix. Logical predictors are now supported, and work like factors. If you want it now, go to https://github.com/rvlenth/emmeans. – Russ Lenth Apr 15 '18 at 02:33
  • Thanks very much! Appreciate your swift response- – tiptothetop Apr 15 '18 at 13:55