[R] physical constraint with gam

David Winsemius dwinsemius at comcast.net
Wed May 11 09:52:35 CEST 2016


> On May 10, 2016, at 5:30 PM, Dominik Schneider <Dominik.Schneider at colorado.edu> wrote:
> 
> Hi,
> Just getting into using GAM using the mgcv package. I've generated some
> models and extracted the splines for each of the variables and started
> visualizing them. I'm noticing that one of my variables is physically
> unrealistic.
> 
> In the example below, my interpretation of the following plot is that the
> y-axis is basically the equivalent of a "parameter" value of a GLM; in GAM
> this value can change as the functional relationship changes between x and
> y. In my case, I am predicting snowdepth based on the fractional snow
> covered area. In no case will snowdepth realistically decrease for a unit
> increase in fsca so my question is: *Is there a way to constrain the spline
> to positive values? *
> 

I would think that the mass or volume of snow might not realistically decrease with increase in area but I see no reason why increasing the area might not be associated with an decrease in mean depth. Depth would be "orthogonal" to area.



> Thanks
> Dominik
> 
> library(mgcv)
> library(dplyr)
> library(ggplot2)
> extract_splines=function(mdl){
>  sterms=predict(mdl,type='terms')
>  datplot=cbind(sterms,mdl$model) %>% tbl_df
>  datplot$intercept=attr(sterms,'constant')
>  datplot$yhat=rowSums(sterms)+attr(sterms,'constant')
>  return(datplot)
> }
> dat=data_frame(snowdepth=runif(100,min =
> 0.001,max=6.7),fsca=runif(100,0.01,.99))
> mdl=gam(snowdepth~s(fsca),data=dat)
> termdF=extract_splines(mdl)
> ggplot(termdF)+
>  geom_line(aes(x=fsca,y=`s(fsca)`))
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list