I'm hoping I can find some advice on this question. I have repeated measures of an outcome variable yvar
where the trend over xvar
changes with a factor SF
. At lower values of SF, yvar is more linear. At higher values of SF, yvar is more curvilinear.
I've modelled yvar with a quadratic polynomial.
lmm <- lm(yvar ~ SF * poly(xvar, 2), data = df)
# visualise
ggplot(df, aes(x = xvar, y = yvar, colour = as.factor(SF))) +
geom_point() +
geom_smooth(formula = y ~ poly(x, 2), method = "lm", se = FALSE)
My main question is: at what value of SF does yvar become significantly better described by a quadratic function than linear?
I think I can answer this by comparing the quadratic coefficient to zero for each level of SF? Does this sound appropriate?
I'm not sure I'm interpreting the results of emmeans::emtrends
correctly, and would appreciate any advice.
library(lme4)
library(lmerTest)
library(emmeans)
library(tidyverse)
# df provided below
lmm <- lm(yvar ~ SF * poly(xvar, 2), data = df)
emt <-
emtrends(
lmm,
specs = ~ SF * degree,
var = "xvar",
max.degree = 2,
at = list(SF = seq(4, 16, 4)), # simplify levels of SF
adjust = "sidak",
infer = TRUE
)
# Remove the linear coefs to focus on quadratic coefs
emt_quadratic <- emt[-c(1:4)]
emt_quadratic
> emt_quadratic
SF degree xvar.trend SE df lower.CL upper.CL t.ratio p.value
4 quadratic -0.00164 0.00223 160 -0.00725 0.00396 -0.739 0.9158
8 quadratic -0.00613 0.00153 160 -0.00998 -0.00228 -4.013 0.0004
12 quadratic -0.01062 0.00178 160 -0.01510 -0.00614 -5.976 <.0001
16 quadratic -0.01511 0.00273 160 -0.02197 -0.00824 -5.544 <.0001
Confidence level used: 0.95
Conf-level adjustment: sidak method for 4 estimates
P value adjustment: sidak method for 4 tests
df <-
structure(list(
ID = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5,
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 12,
12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14,
14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 16, 16,
16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
18, 18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 21, 21, 21, 21, 21, 21, 21),
xvar = c(29, 42, 57,
72, 86, 100, 25, 37, 50, 62, 75, 88, 100, 19, 29, 38, 48, 57,
66, 76, 86, 95, 100, 21, 31, 42, 52, 62, 73, 83, 94, 99, 20,
30, 40, 50, 60, 70, 80, 90, 100, 22, 33, 44, 56, 66, 78, 89,
100, 21, 32, 42, 52, 64, 74, 84, 94, 100, 20, 29, 38, 48, 58,
68, 78, 87, 96, 100, 23, 33, 44, 56, 67, 78, 89, 100, 24, 36,
47, 59, 71, 84, 95, 100, 27, 40, 54, 67, 80, 94, 100, 25, 37,
50, 62, 75, 87, 100, 23, 35, 46, 58, 69, 80, 92, 100, 20, 30,
40, 50, 60, 70, 80, 90, 100, 27, 40, 53, 67, 80, 94, 100, 25,
38, 50, 62, 75, 87, 100, 26, 39, 52, 64, 77, 90, 99, 30, 44,
58, 74, 88, 100, 25, 37, 50, 62, 75, 88, 100, 18, 27, 36, 45,
54, 64, 72, 82, 92, 100, 25, 37, 50, 62, 75, 87, 100),
yvar = c(42,
36, 31, 26, 20, 14, 45, 44, 39, 34, 28, 24, 18, 64, 68, 70, 66,
56, 46, 37, 27, 22, 16, 65, 72, 74, 70, 64, 56, 48, 41, 33, 60,
70, 68, 66, 60, 44, 38, 34, 28, 50, 50, 46, 42, 30, 21, 16, 10,
44, 47, 43, 37, 33, 24, 18, 8, 5, 64, 62, 57, 50, 44, 36, 26,
23, 20, 17, 56, 69, 66, 60, 56, 48, 36, 23, 52, 61, 62, 58, 52,
42, 32, 26, 86, 84, 83, 74, 58, 46, 44, 50, 48, 43, 34, 18, 10,
8, 56, 66, 68, 68, 62, 55, 44, 34, 62, 58, 54, 50, 44, 38, 30,
20, 11, 46, 42, 40, 34, 28, 22, 10, 82, 81, 80, 76, 64, 47, 26,
74, 81, 84, 82, 72, 58, 41, 82, 87, 86, 82, 76, 66, 52, 50, 42,
38, 30, 25, 16, 50, 47, 44, 41, 34, 29, 20, 14, 12, 10, 30, 34,
50, 69, 72, 54, 31),
SF = c(12, 12, 12, 12, 12, 12, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 16, 16, 16, 16, 16,
16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8, 8, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
12, 12, 12, 12, 12, 12, 12, 12, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 5, 5, 5, 5, 5, 5, 5, 12, 12, 12,
12, 12, 12, 12, 12, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 15, 15, 15, 15, 15, 15, 15, 17, 17, 17, 17, 17, 17, 17,
13, 13, 13, 13, 13, 13, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 15, 15, 15, 15, 15, 15, 15)),
row.names = c(NA, -166L), class = c("tbl_df", "tbl", "data.frame"))
I think this answers my question, that the quadratic coefficient for SF = 4 is not different from 0? I would go look closer between SF %in% 4:8 to see where it becomes significant, and interpret the meaningfulness across SF levels.
When I compare contrasts between levels of SF, why do they come back with the same p.value? e.g. I would expect SF4 to be more different from SF16 than SF8. This is probably something basic that I don't understand?
> contrast(emt_quadratic, "revpairwise")
contrast estimate SE df t.ratio p.value
SF8 quadratic - SF4 quadratic -0.00449 0.00131 160 -3.419 0.0044
SF12 quadratic - SF4 quadratic -0.00897 0.00263 160 -3.419 0.0044
SF12 quadratic - SF8 quadratic -0.00449 0.00131 160 -3.419 0.0044
SF16 quadratic - SF4 quadratic -0.01346 0.00394 160 -3.419 0.0044
SF16 quadratic - SF8 quadratic -0.00897 0.00263 160 -3.419 0.0044
SF16 quadratic - SF12 quadratic -0.00449 0.00131 160 -3.419 0.0044
P value adjustment: tukey method for comparing a family of 4 estimates
How then could I test which individual levels of SF were different from each other? Say I want to know what delta change in SF will cause significant differences in the resulting profile of yvar?
I hope this question makes sense. Thanks!
+ facet_grid(~SF)
is more informative. A couple of things I notice right away: the variances vary across SF and something seems to be off with SF = 13. (Is each group a composition of multiple replicates?) Anyway, because of the variance disparities I suggest that a simple approach is to analyze each SF groups separately: the mean params are already SF-specific & if you make the sigma SF-specific also, the dataset "splits". And then you can compare a linear model with a quadratic model with for example likelihood ratio test, one SF group at a time. $\endgroup$SF
in your model as a linear term interacting with your polynomial; i.e., that the polynomial effects change linearly withSF
, meaning that any comparison of those effects is the same for a given increment ofSF
. $\endgroup$SF
alone would predictyvar
. I'll have to re-think how to approach this question from ground up. In general I want to ask how the 'curviness' of the response is related toSF
. $\endgroup$