# Assessing differences in slope of a loess (or any smoother) fit

I have continuous data for x (for example "time") and y with several groups. I would like to show/test that the slope of groupC changes over time (is positive and rises) while groups A and B do not. Below is a MRE. I was thinking about using a GAM, but could need some inspiration of how to formulate this in R. Please note that my real data are a lot more variable than the example in case that is relevant, so it's really about using some sort of smoother and then use this as a basic for assessing differences in slope.

library(tidyverse)

set.seed(1)
data <-
rbind(data.frame(x=seq(1, 100), y=rnorm(100, 1, 5), group="A"),
data.frame(x=seq(1, 100), y=rnorm(100, 3, 5), group="B"),
data.frame(x=seq(1, 100), y=jitter(seq(1, 10, length=100)^3, 1000)/20, group="C"))

data %>%
ggplot(aes(x=x, y=y, color=group)) +
geom_smooth() + # any smoother is fine, loess is just an example for viz
geom_point()


• The problem is that for some intervals of $x$ there are no significant differences in slope while for other intervals there are significant differences. Thus, you need to tell us in more detail what you mean by "differences in slope."
– whuber
May 16 at 16:00

Any continuous function on a compact space can be uniformly approximated as closely as desired by a polynomial. (Stone-Weierstrauss theorem) In this case fit a quadratic to each set and note that only C has significant linear and quadratic coefficients.

library(nlme)
fm <- lmList(y ~ x | group, data = data, pool = FALSE)
fm0 <- lmList(y ~ 1 | group, data = data, pool = FALSE)
Map(anova, fm, fm0)


and here is a plot of the points and fitted lines showing it fits well:

library(ggplot2)
ggplot(data, aes(x, y, col = group)) +
geom_point() +
geom_smooth(formula = y  ~ poly(x, 2, raw = TRUE), method = "lm")


(continued after graph)

We can alternately use a gam. In the case of the data in the question it does not really make any difference but if the data in the question is not representative of your actual problem it may be useful.

library(mgcv)
Map(\(g) summary(gam(y ~ x, data = data, subset = group == g)),
unique(data\$group))


## Update

Have added an ANOVA to lm model and added a gam alternative. Note that both nlme and mgcv come with R so they do not have to be separately installed.

• Polynomial approximations tend to be poor for statistical modeling: polynomials are too rigid (that is, they are determined globally by tiny amounts of local information). Consider splines instead.
– whuber
May 16 at 18:22
• Did you even read the answer??? It worked very well and is really easy to do. May 16 at 18:24
• It worked well for this toy example which was based on a polynomial! In general, the "smoothers" referred to in the question are not polynomials. Thus, you are proposing a solution that seems to work in one special textbook case but is not going to generalize well to real data. Moreover, you still haven't answered the question: how do you propose to interpret what "differences in slope" means and how does fitting a polynomial answer that?
– whuber
May 16 at 18:33
• Unfortunately, the Stone-Weierstrass Theorem is useless for statistical analysis: the polynomial basis is just too large and cumbersome to be of any practical use. There are excellent reasons why you don't commonly see, say, polynomials of the 77th degree used even for enormous datasets. Indeed, the very concept of a smoother precludes using polynomials except locally, because a smoother is supposed to flexibly describe the data within relatively small neighborhoods in a way that is immune to whatever happens beyond them. Piecewise polynomials--splines--are the natural solution.
– whuber
May 16 at 19:19
• Unfortunately, ridiculous polynomial degrees rapidly arise when using polynomials in an effort to smooth even modest datasets -- so implicitly, yes you are suggesting the use of such polynomials.
– whuber
May 16 at 23:13