I am using GAM to simply model a river section bathymetry from point field measurement of water depth. I've used the process several time and without problems (a great example of the workflow can be found here https://fromthebottomoftheheap.net/2016/03/27/soap-film-smoothers/). This time I am getting a wired behaviour of the smoother close to the boundary of my area, even if I forced the smooth to get a value=0 at the boundary. I see my question being similar to this old post that has no responses How to set boundary condition in a complex soap film GAM smoother?.
Here the procedure I followed:
- Set boundary and knots
area<-read_sf("/area.shp")
g<-st_coordinates(area)
g<-as.data.frame(g)
g$past<-paste(g$X,g$Y)
bound <- list(list(long = g[,1], lat = g[,2], f=rep(0,nrow(g))))##f= values at boundary
N <- 20#number of knots
gx <- seq(min(g[,1]), max(g[,1]), len = N)
gy <- seq(min(g[,2]), max(g[,2]), len = N)
gp <- expand.grid(gx, gy)
names(gp) <- c("long","lat")
gp_sf<-st_as_sf(gp,coords = c("long","lat"))
st_crs(gp_sf)<-4326
knots <- gp[with(gp, inSide(bound, long, lat)), ]
out<-autocruncher(bound,knots,x="long",y="lat") #download autocruncher function form https://github.com/dill/soap_checker
- Load filed measurement and filer out external points
bat<-read_sf("batymetry_2d.shp")[,c(3,9)] ##fied water depth points, depth in cm
bat$depth<- -1*as.numeric(bat$'_REMARKS')
bat<-bat[-which(is.na(bat$depth)|bat$depth< -100),]
bat$long<-unlist(st_geometry(bat))[seq(1,(nrow(bat)*2),2)]
bat$lat<-unlist(st_geometry(bat))[seq(2,(nrow(bat)*2),2)]
##filter out external point
bat$wt<-c(st_within(bat,area,sparse=F))
bat2<-bat[-which(bat$wt==F),]
Here the knots distribution (black points) and my measures location (blue points)
- Run GAM
gam1<-gam(depth~s(long,lat,bs="so",xt=list(bnd=bound)),data=bat2
,knots = knots[-c(out),] ,method = "REML")
As you can see the model give wired estimation of water depth near the edges where they are supposed to be 0. White area inside the boundary are removal of estimation where are located of emerging stones. Measure are in cm.
I have clearly checked any presence of outliers, and tried various number of knots but without and major improvement. I don't know if it is due to observation\knots location, but any suggestion will be appreciated.