# [S] Interactions and GAM

Charles Anderson (charles.anderson@dnr.state.mn.us)
Fri, 23 Oct 1998 16:26:39 -0500

Dear S-PLUSers:

Interaction of a continuous and a dichotomous variable in GAM--an answer that works:

B0 <- ifelse(group==0,B,NA)
B1 <- ifelse(group==1,B,NA)
gfact <- factor(C)
gfit <- gam(y~gfact +s(A) +s(B0) +s(B1), na.action=na.gam.replace)

The question was about fitting a GAM with three covariates, A, B, and C. Variables A and B are continuous variables and C is dichotomous. How does one fit a model with s(A) and an interaction between B and C that would produce separate smooths over B for each level of C. The situation is similar to figure 9.13 and section 9.5.2 in Hastie and Tibshirani's book.

I posted the question a month ago and received no answers, only requests for the answer. I recently found the same question and a wrong answer when searching the Statlib S-news site. (I searched diligently before posting my question and don't know how my previous search failed. Oh well.)

The posted answer, attributed to Hastie, was that "You can achieve this in a rather roundabout way, via NA's, with gam(). As you say, make two versions of B, one with NA's when C=1, the other with Nas when C=2. Then fit the model with both of these in:
gam(y~s(A)+s(B1)+s(B2),na.action=na.gam.replace,...)
and you will get what you want. The reason it works is that the NA's get ignored in the smoothing, and the average fit is returned, ie 0."

Hastie's answer neglected to mention the required group factor (gfact) term in the GAM expression. GAM does not create the factor automatically. It is needed because the averages of the groups are unequal in most applications.

The following models would allow a test of "same line" and "separate lines" as in analysis of covariance. y ~ s(B) vs. y ~ gfact +s(B0) +s(B1).
The posted question, wrong answer, and a follow up question (not answered) are at: http://lib.stat.cmu.edu/s-news/Burst/6012 (and 6157 and 6159).

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news