5
$\begingroup$

This question already has an answer here:

I would like to know how to make quickly pairwise comparisons of regressions coefficients across three or more groups in R. Here is a small example:

library(car)
data(iris)
scatterplot(Sepal.Width~Sepal.Length | Species, regLine=TRUE, 
            smooth=FALSE, boxplots=FALSE, by.groups=TRUE, data=iris)

As you can see here, two groups seem to have approximately the same regression coefficients, whereas the third one exhibits a clearly different relationship between the two variables. I can make an Ancova, for example like this:

modComplete <- lm(Sepal.Width ~ -1 + Species + Species:Sepal.Length, data=iris)
modSimple <- lm(Sepal.Width ~ Sepal.Length, data=iris)
anova(modComplete, modSimple)

However, this only tells me that at least two groups differ, and I have no information about pairwise comparisons. I would like to know if there are significantly different regression coefficients between each pair of groups. I think that it could be done using the function multcomp::glht but I am not familiar with it, and the help page is a little confusing to me.

Is there a quick way to do this?

$\endgroup$

marked as duplicate by Ben Bolker, Peter Flom regression Mar 22 at 13:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
$\begingroup$

Use the emmeans package, specifically pairs(emtrends(m, ~grp, var="var")) ... where grp is the categorical (grouping) variable, "var" is the slope variable.

library(emmeans)
m <- lm(Sepal.Width ~ Species*Sepal.Length, iris)
pairs(emtrends(m, ~Species, var="Sepal.Length"))
##  contrast               estimate     SE  df t.ratio p.value
##  setosa - versicolor      0.4788 0.1337 144 3.582   0.0013 
##  setosa - virginica       0.5666 0.1262 144 4.490   <.0001 
##  versicolor - virginica   0.0878 0.0971 144 0.905   0.6382 

## P value adjustment: tukey method for comparing a family of 3 estimates 
$\endgroup$
  • $\begingroup$ Perfect, thank you! $\endgroup$ – Philopolis Mar 22 at 13:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.