[R] mgcv gam modeling trend variation over cases

William Shadish wshadish at ucmerced.edu
Thu Dec 5 01:48:08 CET 2013


Dear R-Helpers,

I posted two days ago on testing significance of random effects in mgcv, 
but realize I did not make my overall purpose clear. I have a series of 
N short time series, where N might range from 3-10 and short means a 
median of 20 time points. The sample data below (PCP) has N = 4 cases 
with 9, 13, 16 and 16 observations over time respectively. The data set 
contains four variables: PID = case number, SessIDX = time (x axis), DVY 
= the outcome (y axis) and TX is treatment is applied = 1 or not = 0.

My goal is to determine (a) is trend present in each case, (b) is it 
linear or nonlinear in each case, and (c) does trend vary significantly 
over cases (PID), the latter presumably to be measured with a random 
effect. I can do the first two (not shown here), but am not sure about 
the third. I am using generalized additive models, either mgcv gam or 
gamm4. For example, using mgcv gam my syntax is

M1 <- gam(DVY  ~ s(SessIDX, bs = "re") + factor(TX),
            data = PCP,
            family = quasipoisson(link="log"), method="REML")
summary(M1,all.p=TRUE)
gam.vcomp(M1)

Using gamm4, my syntax is

PCP$fPID <- factor(PCP$PID)
M2 <- gamm4(DVY ~ factor(TX) + s(SessIDX, by = factor(PID)),
            data = PCP,
            random =~ (1|fPID),
            family = poisson (link="log"))
summary(M2$gam)
summary(M2$mer)

It is not clear to me whether either of these gives me what I want. In 
generalized linear mixed models, I am accustomed to the HLM approach 
(e.g., Raudenbush & Bryk) where each case would have a trend 
coefficient, and the random effect would tell me if those four 
coefficients varied significantly. So that is what I am looking for, but 
adding the nonlinearity modeling of GAM. Is either of these formulations 
giving me what I want--a test of whether trend differs significantly 
over cases or not.

Thanks for any help you can offer. I have worked very hard to solve this 
on my own, and just can't seem to do so.

Will Shadish

 > PCP
    PID SessIDX DVY TX
1    1       1   4  0
2    1       2   5  0
3    1       3   5  0
4    1       4   8  0
5    1       5   3  1
6    1       6   0  1
7    1       7   0  1
8    1       8   0  1
9    1       9   0  1
10   2       1   2  0
11   2       2   2  0
12   2       3   4  0
13   2       4   4  0
14   2       5   4  0
15   2       6   2  0
16   2       7   3  0
17   2       8   1  1
18   2       9   2  1
19   2      10   3  1
20   2      11   1  1
21   2      12   0  1
22   2      13   0  1
23   3       1   7  0
24   3       2   3  0
25   3       3   2  0
26   3       4   5  0
27   3       5   3  0
28   3       6   4  0
29   3       7   3  0
30   3       8   0  1
31   3       9   3  1
32   3      10   2  1
33   3      11   0  1
34   3      12   0  1
35   3      13   2  1
36   3      14   0  1
37   3      15   1  1
38   3      16   1  1
39   4       1   3  0
40   4       2   1  0
41   4       3   1  0
42   4       4   0  0
43   4       5   1  0
44   4       6   2  0
45   4       7   3  0
46   4       8   0  0
47   4       9   1  0
48   4      10   0  1
49   4      11   0  1
50   4      12   0  1
51   4      13   0  1
52   4      14   0  1
53   4      15   0  1
54   4      16   0  1
 >


-- 
William R. Shadish
Distinguished Professor
Founding Faculty

Mailing Address:
William R. Shadish
University of California
School of Social Sciences, Humanities and Arts
5200 North Lake Rd
Merced CA  95343

Physical/Delivery Address:
University of California Merced
ATTN: William Shadish
School of Social Sciences, Humanities and Arts
Facilities Services Building A
5200 North Lake Rd.
Merced, CA 95343

209-228-4372 voice
209-228-4007 fax (communal fax: be sure to include cover sheet)
wshadish at ucmerced.edu
http://faculty.ucmerced.edu/wshadish/index.htm
http://psychology.ucmerced.edu



More information about the R-help mailing list