0

I am fitting a model using gam from the mgcv package. I am storing the result in model and so far I have been looking at the smooth components using plot(model). I have recently started using lattice and like its output. So I am wondering, is it possible to plot these graphs using lattice?

This my data set: https://gist.github.com/plxsas/fcef4a228c18c772b4f3

m2<- gam(TotalInd ~ s(dayinyear, by=as.numeric(Site=="1"), bs="cr")
  +s(dayinyear, by=as.numeric(Site=="2"), bs="cr") + s(dayinyear, 
   by=as.numeric(Site=="3"), bs="cr"), random=list(Replicate=~ 1), data=data)

How can I do plot this model in lattice package with three panels representing my three sites smoother,please?

You also might noticed that I have used the dayinyear instead of proper month format(the first column in the data). This is because Generalized additive models do not handle categorical variables. However, I would like to represent the time in my graph with the names of months (like in first column), Does any one know the way forward for that in a lattice plot?

John Paul
  • 12,196
  • 6
  • 55
  • 75
user48386
  • 95
  • 1
  • 6
  • 1
    Please edit your post, removing your data set and providing it on a separate link (using e.g. a gist on Github https://gist.github.com/) to improve readability. – aronisstav May 13 '14 at 12:33

1 Answers1

1

Here is a general way to do it using some fake data. You will need to tweak this to make sure the names are as you like,

library(reshape)
library(mgcv)
library(lattice)

X1<-rnorm(100)   # Make some fake data
X2<-rnorm(100)
X3<-rnorm(100)
Y<-rnorm(100)

Mod<-gam(Y~s(X1,bs="cr")+s(X2,bs="cr")+s(X3, bs="cr")) # make a model

Z<- predict(Mod,type="terms", se.fit=T)  #Z is the predicted value 
                               #for each smooth term, se.fit give you SE

Z2<-melt(Z$fit)                     #Z was in wide form, Z2 is long form
Z2$XX<-c(X1,X2,X3)            #add the original values for he predictors 
Z2$SE<-melt(Z$se.fit)$value  #add SE
Z2$UP<-Z2$value+2*Z2$SE      #+2 SE
Z2$Low<-Z2$value-2*Z2$SE     # - 2 SE
Z2<-Z2[order(Z2$XX),]

xyplot(value~XX|X2,data=Z2,type="l",col="black",as.table=T,
     prepanel=function (x,y,...)list(ylim=c(min(Z2$Low),max(Z2$UP))),
     panel=function(x,y,groups,subscripts,...){
       panel.xyplot(x,y,...)
       panel.lines(Z2$UP[subscripts]~Z2$XX[subscripts],lty=2, col="red")
       panel.lines(Z2$Low[subscripts]~Z2$XX[subscripts],lty=2, col="red")
     }
 ) 

value is the predicted value for each predictor and X2is where the grouping variable is (indicates which data belongs to each predictor). If you are working we these a lot you should rename things to be clearer. The order part just avoids spaghetti plots

You can control the way the x-axis is labeled using the at and labels arguments for the x-axis in the scales argument. For details see ?xyplot

Update - Here is a version that works with this data

m2<- gam(TotalInd ~ s(dayinyear, by=as.numeric(Site=="1"), bs="cr")
     +s(dayinyear, by=as.numeric(Site=="2"), bs="cr") 
     + s(dayinyear, by=as.numeric(Site=="3"), bs="cr"), 
     random=list(Replicate=~ 1), data=Data)


Z<- predict(m2,type="terms",se.fit=T) #Z is the predicted value and SE
Z2<-melt(Z$fit)                     #Z was in wide form, Z2 is long form

Z2$dayinyear<-Data$dayinyear        #add the original values for he predictors 
Z2$SE<-melt(Z$se.fit)$value
Z2$UP<-Z2$value+2*Z2$SE
Z2$Low<-Z2$value-2*Z2$SE

Z2<-Z2[Z2$value!=0,] #gets rid of excess zeroes

Z2<-Z2[order(Z2$dayinyear),]



xyplot(value~dayinyear|X2,data=Z2,type="l",col="black",as.table=T,
     prepanel=function (x,y,...)list(ylim=c(min(Z2$Low),max(Z2$UP))),
     panel=function(x,y,groups,subscripts,...){
       panel.xyplot(x,y,...)
       panel.lines(Z2$UP[subscripts]~Z2$dayinyear[subscripts],lty=2, col="red")
       panel.lines(Z2$Low[subscripts]~Z2$dayinyear[subscripts],lty=2, col="red")
     }
) 

Note that I changed the name of the starting data.frame from data to Data

EDIT - I have added the two dashed lines that show + /- 2 SE for each plot

Community
  • 1
  • 1
John Paul
  • 12,196
  • 6
  • 55
  • 75
  • Thanks for you help but can you please make your answer specific to my example and data. You will find link to my data and may be looking at it will clarify my point. In my example I am applying one smoother variable which is the time of data collection and I want to see the seasonal trend in my three site with considering the three replicates in each site as random effect. – user48386 May 15 '14 at 13:02
  • Thank you very much for your help and I am really getting there. The graph is very similar to what I am trying to achieve but not identical (https://www.dropbox.com/s/28bwecbaksg4do9/plotting.pdf). I am following case study I have found in Mixed Effect Models and Extension in Ecology in R (Zuur et al., 2009)[chapter 18]. My data is quite similar to the data example in that chapter. They have plotted the model as in the following picture. https://www.dropbox.com/s/cmp1uuxj46n8s8t/Chapter%2018.jpg – user48386 May 18 '14 at 16:52
  • They have plotted the model with the following script https://www.dropbox.com/s/8u5ixw7fxfqfezx/chapter18.docx. Can you please help me to tweak the script to suite my data set, I found it quite difficult to do that and I will really appreciate that. – user48386 May 18 '14 at 17:12
  • Which section and figure is it in that chapter? I can't get to drop-box just now but I am familiar with the book. – John Paul May 18 '14 at 20:08
  • The section is 18.3 A Statistical Data Analysis Strategy for DIN and the figure is Fig. 18.7 Estimated long-term smoother for each area obtained by the model in Equation (18.4) – user48386 May 18 '14 at 20:24
  • Dear John Paul, Thank you very much so far for helping me. Would you be able to help in answering my last question, please? I will really appreciate you help – user48386 May 21 '14 at 08:17
  • @user48386 I have added the SE lines. I did not really follow Zuur et al's code as that was complex and for older R versions, but I think this does the same thing and makes the same figures. – John Paul May 22 '14 at 00:05
  • @user48386 Did this solve the issue? You should either indicate what is not working or accept that answer so that folks who have a similar issue in the future can use this. – John Paul May 22 '14 at 23:07
  • Thank you very much John for help me and I really appreciate the time you dedicate for that. Yes, it work beatifully – user48386 May 23 '14 at 15:57
  • @user48386 Glad to hear it. If you look at the top of my answer on the left there is a number (currently 0) up and down arrows and an outline of a check mark. When someone answers your question it is a good idea to click on the check mark. It will turn green and your question will be recorded as answered. You and the person who answered will also gain rep from this. Thanks JP – John Paul May 23 '14 at 16:23