A simple example

DL Miller edited this page Apr 19, 2014 · 16 revisions
Clone this wiki locally

Just taken from the manual at the moment, more to come...

First load some data from the package, this is artificial data from a simulated coastline.

data(wt2)

It looks like this:

https://github.com/dill/msg/wiki/wt2-domain.png!

Then we can create a sample and add some noise

samp.ind<-sample(1:length(wt2$data$x),250)
wt2.samp<- list(x=wt2$data$x[samp.ind],
               y=wt2$data$y[samp.ind],
               z=wt2$data$z[samp.ind]+rnorm(250)*0.9)

Using the usual mgcv syntax we can fit a model with a 5-dimensional projection in MDS space.

b<-gam(z~s(x,y,bs="msg",k=200,xt=list(bnd=wt2$bnd,mds.dim=5)),data=wt2.samp)

Then we can create the prediction grid and predict:

pred.grid<-data.frame(expand.grid(x=seq(min(wt2$data$x),max(wt2$data$x),len=50),       
                                 y=seq(min(wt2$data$y),max(wt2$data$y),len=50)))
ind<-inSide(wt2$bnd,pred.grid$x,pred.grid$y)
pred.grid<-pred.grid[ind,]
pred.mat<-matrix(NA,50,50)
pred.mat[ind]<-predict(b,pred.grid)

We can then plot the predictions:

image(pred.mat,main="Fitted model",xlab="x",ylab="y",col=heat.colors(1000))
contour(pred.mat,add=T,labcex=0.3,lwd=0.5)

which look like this:

https://github.com/dill/msg/wiki/wt2-prediction.png

and look at what the domain looks like in the MDS domain as compared to its original configuration:

par(mfrow=c(1,2))

plot(b$smooth[[1]]$msg$grid,pch=19,cex=0.3,
    main="Starting grid",xlab="x",ylab="y")

plot(b$smooth[[1]]$msg$mds.obj$points,pch=19,cex=0.3,
    main="Starting grid\n(MDS space)",xlab="x",ylab="y")

Which looks like this:

https://github.com/dill/msg/wiki/wt2-mds-comp.png