1

I am new to fitting gamm models and ran into two problems with my analysis.

  1. I ran the same model using the gam and the bam function of the package mgcv. The models give me different estimates, and I don't understand why and how to choose which function to use. Can anyone explain to me why these functions give different estimates?

  2. I am estimating a model including an interaction between age and condition (binomial factor with 2 conditions). For some reason one of the interaction terms (age:conditioncomputer or age:conditioncozmo) looks weird. It always gives a EDF and chi square of 0 and a p-value of 0.5, as if it was fixed to that. I tried using sum-to-zero and dummy contrasts, but that did not change the output. What is weird to me that there is a significant age effect, but this effect is not significant in neither condition. So I have the strong feeling that something is going wrong here.

Did anyone ever run into this before and can help me figure out if this is even a problem or normal, and how to solve it if it is a problem?

My model syntax is the following:

`bam(reciprocity ~ s(age,k=8) + condition + s(age, by = condition, k=8) + s(ID, bs="re") + s(class, bs="re") + s(school, bs="re"), data=df, family=binomial(link="logit"))`

This is the model output:

enter image description here

My df looks somewhat like this:

enter image description here

In short, I've used below code:

library(tidyverse)
library(psych)
library(mgcv)
library(ggplot2)
library(cowplot)
library(patchwork)
library(rstatix)
library(car)
library(yarrr)
library(itsadug)
df <- read.csv("/Users/lucaleisten/Desktop/Private/Master/Major_project/Data/test/test.csv", sep=",")

df$ID <- as.factor(as.character(df$ID))
df$condition <- as.factor(df$condition)
df$school <- as.factor(df$school)
df$class <- as.factor(df$class)
df$reciprocity <- as.factor(as.character(df$reciprocity))
summary(df)

model_reciprocity <- bam(reciprocity ~ s(age,k=7) +condition + s(age, by = condition, k=7) + s(ID, bs="re") + s(class, bs="re") + s(school, bs="re"), data=df, family=binomial(link="logit"))
summary(model_reciprocity)
Luca
  • 43
  • 4
  • 1
    You can't get a smooth of age that is independent of the two conditional smooths on age. If you take the first `s(age, k=8)` out, I bet you'll get "good" estimates for the two conditional smooths. – DaveArmstrong Apr 19 '22 at 14:46
  • Thanks Dave! That indeed solved it! I assumed gamms are similar to linear mixed models where you also estimate the main effect, but your explanation makes a lot of sense! – Luca Apr 19 '22 at 15:39
  • 2
    @DaveArmstrong In general I believe you can do this! I would first evaluate in some way whether you need both smooths. In these models there may be an issue with concurvity. It often helps if you penalize different derivatives for the global smooth and the "by" smooth, for example, this paper 10.7717/peerj.6876 recommends placing the penalty on the first derivative of the "by" term (your model currently has penalties on the second derivative of each) – SushiChef Apr 19 '22 at 15:42
  • 1
    @SushiChef you're right; I hadn't seen that paper. Perhaps Luca needs to use a different smoothing basis for this to work? – DaveArmstrong Apr 19 '22 at 16:11
  • 1
    Something looks wrong here. I would have assumed that the entry in the Parametric coefficients table would be listed as `conditioncozmo`, not `condition1`. I also don't know why the p value is being displyed like that, nor how you get an EDF below 1 for the GAM model being shown - even though as @SushiChef mentions you really need `m=1` on the factor `by` smooths to make this all identifiable. – Gavin Simpson Apr 20 '22 at 09:25
  • Hi all, first of all thanks for the help! I looked at the paper @DaveArmstrong recommended and I think that might shed some light here. I do have another question though and maybe you can help. My model (without the main effect of age) now gives me two marginally significant effects for age:computer and age:cozmo. I also ran the same model on a subset of just cozmo condition and just human condition (without effect of condition). I get a significant age effect in the cozmo subset. How is it possible that I get a significant effect there, but not for age:cozmo in the model with both conditions? – Luca Apr 20 '22 at 15:18
  • 1
    @Luca the subset models are essentially interacting everything with the subsetting condition. As such, all sorts of things can change. – DaveArmstrong Apr 20 '22 at 15:34
  • @Dave Ah I see! So in that case I guess it is better to trust the full model instead of trying running independent models, correct? – Luca Apr 20 '22 at 18:01
  • 1
    Not necessarily, just be mindful of the fact that by splitting the data and running separate models that it implies an a set of interactions that you may or may not want. – DaveArmstrong Apr 20 '22 at 19:07

0 Answers0