setting constraints on gam

 Classic List Threaded
4 messages
Reply | Threaded
Open this post in threaded view
|

setting constraints on gam

 I am fitting a model in which the response variable y is a function of two independent, quantitative variables x1 and x2; thus: y = f(x1, x2). For reasons I do not believe to be important for the purpose of this post, I find it desirable to find f by means of GAM; also, I require principal effects and interactions to be specified separately, so I am using using te and ti tensors. Thus, I am using the following command: f = gam(y ~ te(x1) + te(x2) + ti(x1, x2)) This results in a model that corresponds to one of the hypotheses I am testing. Nevertheless, another hypothesis requires that, when one of the independent variables (say x2) is zero, the value of y is unaffected by the other variable (in this example x1). In other words f(x1, 0) = k for every value of x1, where k is a constant to be estimated. For x2 values other than zero I would like to let GAM choose the appropriate function relating x1 and y. Is there a way to specify such model in mgcv? ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: setting constraints on gam

 There probably is a way, but it involves some programming. You would need to clone a smooth constructor (e.g. for the "cr" class), and then modify it to add a linear constraint matrix C to the returned smooth object. If b are the smooth coefficients then C should  be the matrix such that s(0) = Cb (you can get this from the Predict.matrix method for the class). Then the constraint Cb=0 will be applied during basis setup, and is equivalent to s(0)=0. Now you can use your cloned class in a tensor product smooth, using the 'ti' constructor. Suppose your cloned smooth class is called "foo", then ti(x,z,bs="foo",mc=c(0,1)) will create a smooth for which s(x,0)=0. Your requirement that s(x,0)=k is then taken care of by the model intercept. If you want to try something similar with the full nested structure it's more complicated still. Then I think you would need something like s(x,by=as.numeric(z!=0)) + s(z) + ti(x,z,bs=c("cr","foo")) Simon On 11/01/18 22:33, Alejandra Martínez Blancas wrote: > I am fitting a model in which the response variable y is a function of > two independent, quantitative variables x1 and x2; thus: y = f(x1, > x2). For reasons I do not believe to be important for the purpose of > this post, I find it desirable to find f by means of GAM; also, I > require principal effects and interactions to be specified separately, > so I am using using te and ti tensors. Thus, I am using the following > command: > > > > f = gam(y ~ te(x1) + te(x2) + ti(x1, x2)) > > > > This results in a model that corresponds to one of the hypotheses I am > testing. Nevertheless, another hypothesis requires that, when one of > the independent variables (say x2) is zero, the value of y is > unaffected by the other variable (in this example x1). In other words > f(x1, 0) = k for every value of x1, where k is a constant to be > estimated. For x2 values other than zero I would like to let GAM > choose the appropriate function relating x1 and y. Is there a way to > specify such model in mgcv? > > ______________________________________________ > [hidden email] 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. -- Simon Wood, School of Mathematics, University of Bristol BS8 1TW UK +44 (0)117 33 18273     http://www.maths.bris.ac.uk/~sw15190______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: setting constraints on gam

 Thanks Simon, by cloning a smooth construct do you mean copying and modifying the smooth constructor code? Could you pleas elaborate on your answer? Which is the Predict.matrix method? 2018-01-12 3:20 GMT-06:00 Simon Wood <[hidden email]>: > There probably is a way, but it involves some programming. You would need to > clone a smooth constructor (e.g. for the "cr" class), and then modify it to > add a linear constraint matrix C to the returned smooth object. If b are the > smooth coefficients then C should  be the matrix such that s(0) = Cb (you > can get this from the Predict.matrix method for the class). Then the > constraint Cb=0 will be applied during basis setup, and is equivalent to > s(0)=0. > > Now you can use your cloned class in a tensor product smooth, using the 'ti' > constructor. Suppose your cloned smooth class is called "foo", then > > ti(x,z,bs="foo",mc=c(0,1)) > > will create a smooth for which s(x,0)=0. Your requirement that s(x,0)=k is > then taken care of by the model intercept. > > If you want to try something similar with the full nested structure it's > more complicated still. Then I think you would need something like > > s(x,by=as.numeric(z!=0)) + s(z) + ti(x,z,bs=c("cr","foo")) > > Simon > > > > On 11/01/18 22:33, Alejandra Martínez Blancas wrote: >> >> I am fitting a model in which the response variable y is a function of >> two independent, quantitative variables x1 and x2; thus: y = f(x1, >> x2). For reasons I do not believe to be important for the purpose of >> this post, I find it desirable to find f by means of GAM; also, I >> require principal effects and interactions to be specified separately, >> so I am using using te and ti tensors. Thus, I am using the following >> command: >> >> >> >> f = gam(y ~ te(x1) + te(x2) + ti(x1, x2)) >> >> >> >> This results in a model that corresponds to one of the hypotheses I am >> testing. Nevertheless, another hypothesis requires that, when one of >> the independent variables (say x2) is zero, the value of y is >> unaffected by the other variable (in this example x1). In other words >> f(x1, 0) = k for every value of x1, where k is a constant to be >> estimated. For x2 values other than zero I would like to let GAM >> choose the appropriate function relating x1 and y. Is there a way to >> specify such model in mgcv? >> >> ______________________________________________ >> [hidden email] 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. > > > > -- > Simon Wood, School of Mathematics, University of Bristol BS8 1TW UK > +44 (0)117 33 18273     http://www.maths.bris.ac.uk/~sw15190> > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: setting constraints on gam

 Dear Alejandra in case you want to move on before Simon replies see inline On 12/01/2018 22:50, Alejandra Martínez Blancas wrote: > Thanks Simon, by cloning a smooth construct do you mean copying and > modifying the smooth constructor code? That is what I understand him to mean yes. (I believe it is clon in Spanish if that helps).   Could you pleas elaborate on > your answer? Which is the Predict.matrix method? > > 2018-01-12 3:20 GMT-06:00 Simon Wood <[hidden email]>: >> There probably is a way, but it involves some programming. You would need to >> clone a smooth constructor (e.g. for the "cr" class), and then modify it to >> add a linear constraint matrix C to the returned smooth object. If b are the >> smooth coefficients then C should  be the matrix such that s(0) = Cb (you >> can get this from the Predict.matrix method for the class). Then the >> constraint Cb=0 will be applied during basis setup, and is equivalent to >> s(0)=0. >> >> Now you can use your cloned class in a tensor product smooth, using the 'ti' >> constructor. Suppose your cloned smooth class is called "foo", then >> >> ti(x,z,bs="foo",mc=c(0,1)) >> >> will create a smooth for which s(x,0)=0. Your requirement that s(x,0)=k is >> then taken care of by the model intercept. >> >> If you want to try something similar with the full nested structure it's >> more complicated still. Then I think you would need something like >> >> s(x,by=as.numeric(z!=0)) + s(z) + ti(x,z,bs=c("cr","foo")) >> >> Simon >> >> >> >> On 11/01/18 22:33, Alejandra Martínez Blancas wrote: >>> >>> I am fitting a model in which the response variable y is a function of >>> two independent, quantitative variables x1 and x2; thus: y = f(x1, >>> x2). For reasons I do not believe to be important for the purpose of >>> this post, I find it desirable to find f by means of GAM; also, I >>> require principal effects and interactions to be specified separately, >>> so I am using using te and ti tensors. Thus, I am using the following >>> command: >>> >>> >>> >>> f = gam(y ~ te(x1) + te(x2) + ti(x1, x2)) >>> >>> >>> >>> This results in a model that corresponds to one of the hypotheses I am >>> testing. Nevertheless, another hypothesis requires that, when one of >>> the independent variables (say x2) is zero, the value of y is >>> unaffected by the other variable (in this example x1). In other words >>> f(x1, 0) = k for every value of x1, where k is a constant to be >>> estimated. For x2 values other than zero I would like to let GAM >>> choose the appropriate function relating x1 and y. Is there a way to >>> specify such model in mgcv? >>> >>> ______________________________________________ >>> [hidden email] 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. >> >> >> >> -- >> Simon Wood, School of Mathematics, University of Bristol BS8 1TW UK >> +44 (0)117 33 18273     http://www.maths.bris.ac.uk/~sw15190>> >> ______________________________________________ >> [hidden email] 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. > > ______________________________________________ > [hidden email] 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. > -- Michael http://www.dewey.myzen.co.uk/home.html______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.