# Pairwise comparisons of regression coefficients [duplicate]

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?

## marked as duplicate by Ben Bolker, Peter Flom♦ regression StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Mar 22 at 13:25

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

• Perfect, thank you! – Philopolis Mar 22 at 13:43