In the following model, I have a linear model with a categorical by continuous interaction:
library(ggplot2)
library(lsmeans)
myModel = lm(Petal.Length ~ Petal.Width*Species, data = iris)
ggplot(data=iris, aes(x = Petal.Width, y = Petal.Length, color = Species)) +
geom_jitter(alpha = 0.5) +
geom_smooth(method = "lm")
I want to know the following:
- for each species, is the slope different from zero?
- do slopes differ between species?
I can get some of this information from summary(myModel)
:
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.3276 0.1309 10.139 < 2e-16 ***
Petal.Width 0.5465 0.4900 1.115 0.2666
Speciesversicolor 0.4537 0.3737 1.214 0.2267
Speciesvirginica 2.9131 0.4060 7.175 3.53e-11 ***
Petal.Width:Speciesversicolor 1.3228 0.5552 2.382 0.0185 *
Petal.Width:Speciesvirginica 0.1008 0.5248 0.192 0.8480
In my understanding, this gives:
- the slope for the species "setosa" (
Petal.Width
) - the differences in slopes of setosa vs. versicolor (
Petal.Width:Speciesversicolor
) and setosa vs. virginica (Petal.Width:Speciesvirginica
)
I tried to get the other slopes and slope differences through lsmeans::lstrends
:
# get slopes
myModelSlopes <- lstrends(myModel, "Species", var="Petal.Width")
myModelSlopes
Species Petal.Width.trend SE df lower.CL upper.CL
setosa 0.546 0.490 144 -0.422 1.52
versicolor 1.869 0.261 144 1.353 2.39
virginica 0.647 0.188 144 0.276 1.02
# get differences between slopes
pairs(myModelSlopes)
contrast estimate SE df t.ratio p.value
setosa - versicolor -1.323 0.555 144 -2.382 0.0483
setosa - virginica -0.101 0.525 144 -0.192 0.9799
versicolor - virginica 1.222 0.322 144 3.798 0.0006
From this, I have two questions:
- Is there any way get the p-values for the single slopes (via
lsmeans
, or another way)? I guess I could always rerun the model using another baseline category, but that seems akward... - While the estimates seem to be equivalent, I am somewhat confused that
pairs(myModelSlopes)
gives other p-values for the slope comparisons thansummary(myModel)
. Why is this the case?