7

I have a time-series data set that contains an outcome variable which is continuous and two factor predictors (one with 6 levels and one with 2 levels).

I would like to model the non-linear interaction of the two factor variables on the continuous variable.

This is the model I have so far:

library(mgcv)

model <- bam(
    outcome ~
        factor_1 + factor_2 +
        s(time, k = 9) +
        s(time, by = factor_1, k = 9) +
        s(time, by = factor_2, k = 9),
    data = df
)

summary(model)
Family: gaussian 
Link function: identity 

Formula:
outcome ~ factor_1 + factor_2 + s(time, k = 9) + s(time, by = factor_1, 
    k = 9) + s(time, by = factor_2, k = 9)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  2612.72      23.03 113.465   <2e-16 ***
factor_1b      33.19      27.00   1.229     0.22    
factor_2z    -488.52      27.00 -18.093   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Approximate significance of smooth terms:
                    edf Ref.df      F  p-value    
s(time)           2.564  3.184  6.408 0.000274 ***
s(time):factor_1b 1.000  1.001  0.295 0.587839    
s(time):factor_2z 2.246  2.792 34.281  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

R-sq.(adj) =  0.679   Deviance explained = 69.1%
fREML = 1359.6  Scale est. = 37580     n = 207

Now I would like to add a non-linear interaction of factor_1 and factor_2 with time for the effect on outcome, so that the smoothers in every combination could differ (for example: factor_2 has a stronger non-linear effect for some levels of factor_1). Something like s(time, factor_1, factor_2) or s(time, factor_1, by = factor_2) does not work.

Stefano
  • 1,405
  • 11
  • 21
  • how did you check for existence of non-linear interaction? and how do you incorporate it in your model. –  Dec 10 '17 at 17:37
  • please add your sample data in your question. –  Dec 10 '17 at 17:39
  • Checking for the existence of a non-linear interaction will be part of model comparison for hypothesis testing. We want to know if adding an interaction between the factors improves the model or not. – Stefano Dec 10 '17 at 23:26
  • Good . But I do not see a propsed model incoporating non-linear interaction . Alternatively, does the Gam model takes care of nonlinear interaction . I need your assistance. –  Dec 11 '17 at 02:26
  • 1
    Yes, the non-linearity is dealt with by the smooths `s()`. The `by` arguments within the smooths are a way of specify interactions, but I am not sure how to achieve a multiple term interaction. – Stefano Dec 11 '17 at 07:23
  • How does mcgv contribute to your model. –  Dec 11 '17 at 11:35
  • I am not sure I understand the question, but see last edit to my question. – Stefano Dec 11 '17 at 12:55
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/70049/discussion-between-subhash-c-davar-and-stefano). –  Dec 11 '17 at 15:23

1 Answers1

8

Including an interaction of two factors using interaction() seems to do the job.

library(mgcv)

# The following assumes factors are ordered with treatment contrast.    
model <- bam(
    outcome ~
        interaction(factor_1, factor_2) +
        s(time, k = 9) +
        s(time, by = interaction(factor_1, factor_2), k = 9),
    data = df
)
Stefano
  • 1,405
  • 11
  • 21