When introducing a random effect in gamm
and gamm4
, I receive different p-values for the $gam
parametric coefficients. Which seems to be due to a difference in SE calculation(?).
Differences in the smooth term values as well.
Computing a random effect structure in gam
gives a comparable result to that of gamm$gam
.
EDIT
Reproducible example with modified models:
library(tidyr)
library(dplyr)
library(mgcv)
library(gamm4)
library(performance)
library(ggplot2)
# Generate the data
leaf_rand <- data.frame(
Treatment = rep(factor(c("B", "C", "A", "C", "B", "A", "C", "A", "B")),
times = length(2015:2022)),
Year = rep(2015:2022, each = 9),
Plot = factor(rep(1:9, times = 8)),
N.P = round(c(
15.2, 13.6, 15.8, 14.5, 16.1, 17.5, 19.4, 13.8, 14.5,
16.4, 15.7, 14.1, 14.1, 16.1, 18.4, 19.3, 14.8, 16.9,
17.0, 15.1, 18.9, 17.0, 17.3, 17.0, 19.5, 17.0, 16.0,
18.2, 13.1, 19.7, 17.3, 17.3, 17.7, 22.0, 18.3, 14.9,
17.9, 14.4, 16.3, 15.7, 15.8, 20.0, 14.6, 15.4, 12.8,
17.2, 12.4, 13.9, 11.2, 16.3, 22.0, 18.5, 12.4, 12.1,
16.6, 13.6, 17.0, 10.5, 16.2, 18.3, 17.9, 14.5, 14.0,
22.0, 15.4, 20.0, 17.5, 20.8, 21.8, 21.4, 19.4, 17.1
))
)
### GAMM Models ---------------------------------------------------------------
gam_model <- gamm4(N.P ~ Treatment +
s(Year, Treatment, k = 5, bs = "fs"),
random = ~(1|Plot),
data = leaf_rand, REML = TRUE)
gam_model1 <- gamm(N.P ~ Treatment +
s(Year, Treatment, k = 5, bs = "fs"),
random = list(Plot=~1),
data = leaf_rand, REML = TRUE)
gam_model2 <- gam(N.P ~ Treatment +
s(Year, Treatment, k = 5, bs = "fs") +
s(Plot, k = 9, bs = 're'), data = leaf_rand, method="REML")
# Model summaries
summary(gam_model$gam)
summary(gam_model1$gam)
summary(gam_model2)
# Compare the models
compare <- compare_performance(gam_model$gam, gam_model1$gam, gam_model2)
print(compare)
### ggplot by treatment
ggplot(leaf_rand, aes(x = Year, y = N.P, color = Treatment)) +
geom_point() +
geom_smooth(aes(group = Treatment), method = "loess", se = FALSE) +
theme_minimal() +
theme(legend.position = "bottom") +
labs(
x = "Year",
y = "N.P",
color = "Treatment"
)
What is wrong here?
Which one should I proceed with?
Is it correct to assume that the differences between the treatments are significant according to the $gam parametric coefficient p-values?
For more context about the data in question - there are three treatments and three plots per treatment as the repetitions. The treatments have been applied annually during several years and each plot was sampled once per year.
It is a first attempt at additive models trying to catch information from an inter annual fluctuating curve, where a linear model perhaps not completely satisfy.
The overall goal is still to test for differences between the treatments. Along the entire treatment period, as well as within the years.