# nonmonotonic glm?

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

## nonmonotonic glm?

 I have the following problem. DV is binomial p IV is quantitative variable that goes from negative to positive values. The data look like this (need nonproportional font to view): o                                     o       o                      o           o           o              o     o                 o ----------------------+------------------------                            0 If these data were symmetrical about zero, I could use abs(IV) and do glm(p ~ absIV). I suppose I could fit two glms, one to positive and one to negative IV values. Seems a rather ugly approach. (To complicate things further, this is within-subjects design) Any suggestions welcome. Thanks! Bill         [[alternative HTML version deleted]] ______________________________________________ [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: nonmonotonic glm?

 Stanislav Aggerwal gmail.com> writes: > > I have the following problem. > DV is binomial p > IV is quantitative variable that goes from negative to positive values. > > The data look like this (need nonproportional font to view):   [snip to make gmane happy] > If these data were symmetrical about zero, > I could use abs(IV) and do glm(p > ~ absIV). > I suppose I could fit two glms, one to positive and one to negative IV > values. Seems a rather ugly approach. > [snip]   What's wrong with a GLM with quadratic terms in the predictor variable? This is perfectly respectable, well-defined, and easy to implement:   glm(y~poly(x,2),family=binomial,data=...) or   y~x+I(x^2)  or y~poly(x,2,raw=TRUE) > (To complicate things further, this is within-subjects design) glmer, glmmPQL, glmmML, etc. should all support this just fine. ______________________________________________ [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: nonmonotonic glm?

 > On Jan 11, 2015, at 4:00 PM, Ben Bolker <[hidden email]> wrote: > > Stanislav Aggerwal gmail.com> writes: > >> >> I have the following problem. >> DV is binomial p >> IV is quantitative variable that goes from negative to positive values. >> >> The data look like this (need nonproportional font to view): > > >  [snip to make gmane happy] > >> If these data were symmetrical about zero, >> I could use abs(IV) and do glm(p >> ~ absIV). >> I suppose I could fit two glms, one to positive and one to negative IV >> values. Seems a rather ugly approach. >> > > [snip] > > >  What's wrong with a GLM with quadratic terms in the predictor variable? > > This is perfectly respectable, well-defined, and easy to implement: > >  glm(y~poly(x,2),family=binomial,data=...) > > or   y~x+I(x^2)  or y~poly(x,2,raw=TRUE) > >> (To complicate things further, this is within-subjects design) > > glmer, glmmPQL, glmmML, etc. should all support this just fine. As an alternative to Ben's recommendation, consider using a piecewise cubic spline on the IV. This can be done using glm():   # splines is part of the Base R distribution   # I am using 'df = 5' below, but this can be adjusted up or down as may be apropos   require(splines)   glm(DV ~ ns(IV, df = 5), family = binomial, data = YourDataFrame) and as Ben's notes, is more generally supported in mixed models. If this was not mixed model, another logistic regression implementation is in Frank's rms package on CRAN, using his lrm() instead of glm() and rcs() instead of ns(): # after installing rms from CRAN require(rms) lrm(DV ~ rcs(IV, 5), data = YourDataFrame) Regards, Marc Schwartz ______________________________________________ [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: nonmonotonic glm?

 If you're going to use splines, another possibility is mgcv::gam (also part of standard R installation)   require(mgcv)   gam(DV ~ s(IV), data= YourDataFrame, family=binomial) this has the advantage that the complexity of the spline is automatically adjusted/selected by the fitting algorithm (although occasionally you need to use s(IV,k=something_bigger) to adjust the default *maximum* complexity chosen by the code) On Sun, Jan 11, 2015 at 5:23 PM, Marc Schwartz <[hidden email]> wrote: > >> On Jan 11, 2015, at 4:00 PM, Ben Bolker <[hidden email]> wrote: >> >> Stanislav Aggerwal gmail.com> writes: >> >>> >>> I have the following problem. >>> DV is binomial p >>> IV is quantitative variable that goes from negative to positive values. >>> >>> The data look like this (need nonproportional font to view): >> >> >>  [snip to make gmane happy] >> >>> If these data were symmetrical about zero, >>> I could use abs(IV) and do glm(p >>> ~ absIV). >>> I suppose I could fit two glms, one to positive and one to negative IV >>> values. Seems a rather ugly approach. >>> >> >> [snip] >> >> >>  What's wrong with a GLM with quadratic terms in the predictor variable? >> >> This is perfectly respectable, well-defined, and easy to implement: >> >>  glm(y~poly(x,2),family=binomial,data=...) >> >> or   y~x+I(x^2)  or y~poly(x,2,raw=TRUE) >> >>> (To complicate things further, this is within-subjects design) >> >> glmer, glmmPQL, glmmML, etc. should all support this just fine. > > > As an alternative to Ben's recommendation, consider using a piecewise cubic spline on the IV. This can be done using glm(): > >   # splines is part of the Base R distribution >   # I am using 'df = 5' below, but this can be adjusted up or down as may be apropos >   require(splines) >   glm(DV ~ ns(IV, df = 5), family = binomial, data = YourDataFrame) > > > and as Ben's notes, is more generally supported in mixed models. > > If this was not mixed model, another logistic regression implementation is in Frank's rms package on CRAN, using his lrm() instead of glm() and rcs() instead of ns(): > > # after installing rms from CRAN > require(rms) > lrm(DV ~ rcs(IV, 5), data = YourDataFrame) > > > Regards, > > Marc Schwartz > > ______________________________________________ [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: nonmonotonic glm?

 Thanks very much Marc and Ben for the helpful suggestions Stan On Sun, Jan 11, 2015 at 10:28 PM, Ben Bolker <[hidden email]> wrote: > If you're going to use splines, another possibility is mgcv::gam (also > part of standard R installation) > >   require(mgcv) >   gam(DV ~ s(IV), data= YourDataFrame, family=binomial) > > this has the advantage that the complexity of the spline is > automatically adjusted/selected by the fitting algorithm (although > occasionally you need to use s(IV,k=something_bigger) to adjust the > default *maximum* complexity chosen by the code) > > > On Sun, Jan 11, 2015 at 5:23 PM, Marc Schwartz <[hidden email]> > wrote: > > > >> On Jan 11, 2015, at 4:00 PM, Ben Bolker <[hidden email]> wrote: > >> > >> Stanislav Aggerwal gmail.com> writes: > >> > >>> > >>> I have the following problem. > >>> DV is binomial p > >>> IV is quantitative variable that goes from negative to positive values. > >>> > >>> The data look like this (need nonproportional font to view): > >> > >> > >>  [snip to make gmane happy] > >> > >>> If these data were symmetrical about zero, > >>> I could use abs(IV) and do glm(p > >>> ~ absIV). > >>> I suppose I could fit two glms, one to positive and one to negative IV > >>> values. Seems a rather ugly approach. > >>> > >> > >> [snip] > >> > >> > >>  What's wrong with a GLM with quadratic terms in the predictor variable? > >> > >> This is perfectly respectable, well-defined, and easy to implement: > >> > >>  glm(y~poly(x,2),family=binomial,data=...) > >> > >> or   y~x+I(x^2)  or y~poly(x,2,raw=TRUE) > >> > >>> (To complicate things further, this is within-subjects design) > >> > >> glmer, glmmPQL, glmmML, etc. should all support this just fine. > > > > > > As an alternative to Ben's recommendation, consider using a piecewise > cubic spline on the IV. This can be done using glm(): > > > >   # splines is part of the Base R distribution > >   # I am using 'df = 5' below, but this can be adjusted up or down as > may be apropos > >   require(splines) > >   glm(DV ~ ns(IV, df = 5), family = binomial, data = YourDataFrame) > > > > > > and as Ben's notes, is more generally supported in mixed models. > > > > If this was not mixed model, another logistic regression implementation > is in Frank's rms package on CRAN, using his lrm() instead of glm() and > rcs() instead of ns(): > > > > # after installing rms from CRAN > > require(rms) > > lrm(DV ~ rcs(IV, 5), data = YourDataFrame) > > > > > > Regards, > > > > Marc Schwartz > > > > >         [[alternative HTML version deleted]] ______________________________________________ [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: nonmonotonic glm?

 dear Stanislav, Your data show two slopes with a kink at around 0. Thus, yet another approach would be to use segmented regression to fit a piecewise linear relationship with unknown breakpoint (being estimated as part of model fitting). While the resulting fitting is likely to be (slightly) worse than the one coming from splines, the advantage is that you get interpretable parameter estimates, left and right slopes and breakpoint. Relevant syntax is library(segmented) o<-glm(DV~IV, data= YourDataFrame, family=binomial) os<-segmented(o, ~IV, psi=0) vito Il 12/01/2015 13.45, Stanislav Aggerwal ha scritto: > Thanks very much Marc and Ben for the helpful suggestions > > Stan > > On Sun, Jan 11, 2015 at 10:28 PM, Ben Bolker <[hidden email]> wrote: > >> If you're going to use splines, another possibility is mgcv::gam (also >> part of standard R installation) >> >>    require(mgcv) >>    gam(DV ~ s(IV), data= YourDataFrame, family=binomial) >> >> this has the advantage that the complexity of the spline is >> automatically adjusted/selected by the fitting algorithm (although >> occasionally you need to use s(IV,k=something_bigger) to adjust the >> default *maximum* complexity chosen by the code) >> >> >> On Sun, Jan 11, 2015 at 5:23 PM, Marc Schwartz <[hidden email]> >> wrote: >>> >>>> On Jan 11, 2015, at 4:00 PM, Ben Bolker <[hidden email]> wrote: >>>> >>>> Stanislav Aggerwal gmail.com> writes: >>>> >>>>> >>>>> I have the following problem. >>>>> DV is binomial p >>>>> IV is quantitative variable that goes from negative to positive values. >>>>> >>>>> The data look like this (need nonproportional font to view): >>>> >>>> >>>>   [snip to make gmane happy] >>>> >>>>> If these data were symmetrical about zero, >>>>> I could use abs(IV) and do glm(p >>>>> ~ absIV). >>>>> I suppose I could fit two glms, one to positive and one to negative IV >>>>> values. Seems a rather ugly approach. >>>>> >>>> >>>> [snip] >>>> >>>> >>>>   What's wrong with a GLM with quadratic terms in the predictor variable? >>>> >>>> This is perfectly respectable, well-defined, and easy to implement: >>>> >>>>   glm(y~poly(x,2),family=binomial,data=...) >>>> >>>> or   y~x+I(x^2)  or y~poly(x,2,raw=TRUE) >>>> >>>>> (To complicate things further, this is within-subjects design) >>>> >>>> glmer, glmmPQL, glmmML, etc. should all support this just fine. >>> >>> >>> As an alternative to Ben's recommendation, consider using a piecewise >> cubic spline on the IV. This can be done using glm(): >>> >>>    # splines is part of the Base R distribution >>>    # I am using 'df = 5' below, but this can be adjusted up or down as >> may be apropos >>>    require(splines) >>>    glm(DV ~ ns(IV, df = 5), family = binomial, data = YourDataFrame) >>> >>> >>> and as Ben's notes, is more generally supported in mixed models. >>> >>> If this was not mixed model, another logistic regression implementation >> is in Frank's rms package on CRAN, using his lrm() instead of glm() and >> rcs() instead of ns(): >>> >>> # after installing rms from CRAN >>> require(rms) >>> lrm(DV ~ rcs(IV, 5), data = YourDataFrame) >>> >>> >>> Regards, >>> >>> Marc Schwartz >>> >>> >> > > [[alternative HTML version deleted]] > > ______________________________________________ > [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. > -- ============================================== Vito M.R. Muggeo Dip.to Sc Statist e Matem `Vianelli' Università di Palermo viale delle Scienze, edificio 13 90128 Palermo - ITALY tel: 091 23895240 fax: 091 485726 http://dssm.unipa.it/vmuggeo28th IWSM International Workshop on Statistical Modelling July 8-12, 2013, Palermo http://iwsm2013.unipa.it______________________________________________ [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: nonmonotonic glm?

 Comments in line On 12/01/2015 13:13, Vito M. R. Muggeo wrote: > dear Stanislav, > Your data show two slopes with a kink at around 0. Thus, yet another > approach would be to use segmented regression to fit a piecewise linear > relationship with unknown breakpoint (being estimated as part of model > fitting). While the resulting fitting is likely to be (slightly) worse > than the one coming from splines, the advantage is that you get > interpretable parameter estimates, left and right slopes and breakpoint. Dear Stanislav You might also want to search for 'broken stick', another name for this sort of model. I suppose it is also a linear spline. If you know on scientific grounds where the breakpoint is you can force its position. I am not sure how relevant this is but in your original example the slopes would have been constrained to be equal although I wonder whether that was really what you intended. Michael > Relevant syntax is > > library(segmented) > o<-glm(DV~IV, data= YourDataFrame, family=binomial) > os<-segmented(o, ~IV, psi=0) > > vito > > > Il 12/01/2015 13.45, Stanislav Aggerwal ha scritto: >> Thanks very much Marc and Ben for the helpful suggestions >> >> Stan >> >> On Sun, Jan 11, 2015 at 10:28 PM, Ben Bolker <[hidden email]> wrote: >> >>> If you're going to use splines, another possibility is mgcv::gam (also >>> part of standard R installation) >>> >>>    require(mgcv) >>>    gam(DV ~ s(IV), data= YourDataFrame, family=binomial) >>> >>> this has the advantage that the complexity of the spline is >>> automatically adjusted/selected by the fitting algorithm (although >>> occasionally you need to use s(IV,k=something_bigger) to adjust the >>> default *maximum* complexity chosen by the code) >>> >>> >>> On Sun, Jan 11, 2015 at 5:23 PM, Marc Schwartz <[hidden email]> >>> wrote: >>>> >>>>> On Jan 11, 2015, at 4:00 PM, Ben Bolker <[hidden email]> wrote: >>>>> >>>>> Stanislav Aggerwal gmail.com> writes: >>>>> >>>>>> >>>>>> I have the following problem. >>>>>> DV is binomial p >>>>>> IV is quantitative variable that goes from negative to positive >>>>>> values. >>>>>> >>>>>> The data look like this (need nonproportional font to view): >>>>> >>>>> >>>>>   [snip to make gmane happy] >>>>> >>>>>> If these data were symmetrical about zero, >>>>>> I could use abs(IV) and do glm(p >>>>>> ~ absIV). >>>>>> I suppose I could fit two glms, one to positive and one to >>>>>> negative IV >>>>>> values. Seems a rather ugly approach. >>>>>> >>>>> >>>>> [snip] >>>>> >>>>> >>>>>   What's wrong with a GLM with quadratic terms in the predictor >>>>> variable? >>>>> >>>>> This is perfectly respectable, well-defined, and easy to implement: >>>>> >>>>>   glm(y~poly(x,2),family=binomial,data=...) >>>>> >>>>> or   y~x+I(x^2)  or y~poly(x,2,raw=TRUE) >>>>> >>>>>> (To complicate things further, this is within-subjects design) >>>>> >>>>> glmer, glmmPQL, glmmML, etc. should all support this just fine. >>>> >>>> >>>> As an alternative to Ben's recommendation, consider using a piecewise >>> cubic spline on the IV. This can be done using glm(): >>>> >>>>    # splines is part of the Base R distribution >>>>    # I am using 'df = 5' below, but this can be adjusted up or down as >>> may be apropos >>>>    require(splines) >>>>    glm(DV ~ ns(IV, df = 5), family = binomial, data = YourDataFrame) >>>> >>>> >>>> and as Ben's notes, is more generally supported in mixed models. >>>> >>>> If this was not mixed model, another logistic regression implementation >>> is in Frank's rms package on CRAN, using his lrm() instead of glm() and > >>> rcs() instead of ns(): >>>> >>>> # after installing rms from CRAN >>>> require(rms) >>>> lrm(DV ~ rcs(IV, 5), data = YourDataFrame) >>>> >>>> >>>> Regards, >>>> >>>> Marc Schwartz >>>> >>>> >>> >> >>     [[alternative HTML version deleted]] >> >> ______________________________________________ >> [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______________________________________________ [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: nonmonotonic glm?

 ... but do realize that after you have looked at the data to determine the appropriate modeling approach, no statistical inference (significance tests, confidence intervals, etc.) should be done on the model used. Or more precisely, any that is done is wrong. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Mon, Jan 12, 2015 at 7:26 AM, Michael Dewey <[hidden email]> wrote: > Comments in line > > On 12/01/2015 13:13, Vito M. R. Muggeo wrote: >> >> dear Stanislav, >> Your data show two slopes with a kink at around 0. Thus, yet another >> approach would be to use segmented regression to fit a piecewise linear >> relationship with unknown breakpoint (being estimated as part of model >> fitting). While the resulting fitting is likely to be (slightly) worse >> than the one coming from splines, the advantage is that you get >> interpretable parameter estimates, left and right slopes and breakpoint. > > > Dear Stanislav > You might also want to search for 'broken stick', another name for this sort > of model. I suppose it is also a linear spline. If you know on scientific > grounds where the breakpoint is you can force its position. > > I am not sure how relevant this is but in your original example the slopes > would have been constrained to be equal although I wonder whether that was > really what you intended. > > Michael > >> Relevant syntax is >> >> library(segmented) >> o<-glm(DV~IV, data= YourDataFrame, family=binomial) >> os<-segmented(o, ~IV, psi=0) >> >> vito >> >> >> Il 12/01/2015 13.45, Stanislav Aggerwal ha scritto: >>> >>> Thanks very much Marc and Ben for the helpful suggestions >>> >>> Stan >>> >>> On Sun, Jan 11, 2015 at 10:28 PM, Ben Bolker <[hidden email]> wrote: >>> >>>> If you're going to use splines, another possibility is mgcv::gam (also >>>> part of standard R installation) >>>> >>>>    require(mgcv) >>>>    gam(DV ~ s(IV), data= YourDataFrame, family=binomial) >>>> >>>> this has the advantage that the complexity of the spline is >>>> automatically adjusted/selected by the fitting algorithm (although >>>> occasionally you need to use s(IV,k=something_bigger) to adjust the >>>> default *maximum* complexity chosen by the code) >>>> >>>> >>>> On Sun, Jan 11, 2015 at 5:23 PM, Marc Schwartz <[hidden email]> >>>> wrote: >>>>> >>>>> >>>>>> On Jan 11, 2015, at 4:00 PM, Ben Bolker <[hidden email]> wrote: >>>>>> >>>>>> Stanislav Aggerwal gmail.com> writes: >>>>>> >>>>>>> >>>>>>> I have the following problem. >>>>>>> DV is binomial p >>>>>>> IV is quantitative variable that goes from negative to positive >>>>>>> values. >>>>>>> >>>>>>> The data look like this (need nonproportional font to view): >>>>>> >>>>>> >>>>>> >>>>>>   [snip to make gmane happy] >>>>>> >>>>>>> If these data were symmetrical about zero, >>>>>>> I could use abs(IV) and do glm(p >>>>>>> ~ absIV). >>>>>>> I suppose I could fit two glms, one to positive and one to >>>>>>> negative IV >>>>>>> values. Seems a rather ugly approach. >>>>>>> >>>>>> >>>>>> [snip] >>>>>> >>>>>> >>>>>>   What's wrong with a GLM with quadratic terms in the predictor >>>>>> variable? >>>>>> >>>>>> This is perfectly respectable, well-defined, and easy to implement: >>>>>> >>>>>>   glm(y~poly(x,2),family=binomial,data=...) >>>>>> >>>>>> or   y~x+I(x^2)  or y~poly(x,2,raw=TRUE) >>>>>> >>>>>>> (To complicate things further, this is within-subjects design) >>>>>> >>>>>> >>>>>> glmer, glmmPQL, glmmML, etc. should all support this just fine. >>>>> >>>>> >>>>> >>>>> As an alternative to Ben's recommendation, consider using a piecewise >>>> >>>> cubic spline on the IV. This can be done using glm(): >>>>> >>>>> >>>>>    # splines is part of the Base R distribution >>>>>    # I am using 'df = 5' below, but this can be adjusted up or down as >>>> >>>> may be apropos >>>>> >>>>>    require(splines) >>>>>    glm(DV ~ ns(IV, df = 5), family = binomial, data = YourDataFrame) >>>>> >>>>> >>>>> and as Ben's notes, is more generally supported in mixed models. >>>>> >>>>> If this was not mixed model, another logistic regression implementation >>>> >>>> is in Frank's rms package on CRAN, using his lrm() instead of glm() and >> >> >>>> rcs() instead of ns(): >>>>> >>>>> >>>>> # after installing rms from CRAN >>>>> require(rms) >>>>> lrm(DV ~ rcs(IV, 5), data = YourDataFrame) >>>>> >>>>> >>>>> Regards, >>>>> >>>>> Marc Schwartz >>>>> >>>>> >>>> >>> >>>     [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> [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> > ______________________________________________ > [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.