1
$\begingroup$

--- Edit ---

Let's take a very simple model, with Y and X numerical variables and Fact a categorical variable.

mod = lm(Y~X*Fact)

I want to:

  1. Check whether there are differences of Y between the Fact categories; i.e. to make pairwise comparisons of means of Y for Fact categories :

This can be easily done with the glht package :

 summary(glht(mod, mcp(Fact ="Tukey")))
  1. And whether the slopes of y=f(x) are different for each value of Fact; again with pairwise comparisons. This is what I cannot find.

I found many discussions about glht or lsmeans here and elsewhere (1)(2)(3)(4) but they are all about interactions between two factors, not numerical variable*factor, so I wonder if I might have missed something obvious there?

$\endgroup$
1
  • $\begingroup$ Your rephrased question (1) is still somewhat misleading, in that this comparison is ambiguous because it depends on X. I think glht uses the mean of X but am not sure. At any rate, it is extremely important to state which X is being used; otherwise, we're not giving the complete story. The code in my answer shows how to do this for 3 different X values. $\endgroup$
    – Russ Lenth
    Dec 7 '16 at 23:08
3
$\begingroup$

I'm not sure you understand the implications of the model you fitted. Since X is a quantitative predictor and Fact is a factor, your model has fitted 3 straight lines having different slopes and intercepts. It's almost the same as separately fitting the 3 lines with the data split according to Fact, except that they share common variance estimates.

Given this, what do you mean by "differences of Y between Fact categories"? The factor levels compare differently at each X value, for which there are potentially infinitely many. If you want to do these comparisons at particular X values, it is easy enough:

library(lsmeans)
lsmeans(mod, pairwise ~ Fact | X, at = list(X = c(-2, 0, 2))

(but substitute the particular X values of interest)

I completely don't understand the second question about whether the interactions are significantly different. Perhaps you want to compare the slopes of the three fitted lines? That can be done using

lstrends(mod, pairwise ~ Fact, var = "X")
$\endgroup$
1
  • $\begingroup$ I am sorry I was not clear. I will edit my question to make it clearer. lstrends() was what I was looking for. $\endgroup$
    – Nausi
    Dec 7 '16 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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