Obtain gradient at multiple values for exponetial decay model

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

Obtain gradient at multiple values for exponetial decay model

 Readers, Data set: t,c 0,100 40,78 80,59 120,38 160,25 200,21 240,16 280,12 320,10 360,9 400,7 graphdata<-read.csv('~/tmp/data.csv') graphmodeld<-lm(log(graphdata[,2])~graphdata[,1]) graphmodelp<-exp(predict(graphmodeld)) plot(graphdata[,2]~graphdata[,1]) lines(graphdata[,1],graphmodelp) Please what is the function and syntax to obtain gradient values for the model curve at various requested values, e.g.: when graphdata[,1] at values = 100, 250, 350 ? ______________________________________________ [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: Obtain gradient at multiple values for exponetial decay model

 This smells like homework, which the Posting Guide indicates is off topic. I am not aware of "the function" that will solve this, but if you know what a gradient is analytically then you should be able to put together a solution very similar to the code you already have with the addition of using the coef function. -- Sent from my phone. Please excuse my brevity. On April 5, 2018 3:44:03 AM PDT, g l <[hidden email]> wrote: >Readers, > >Data set: > >t,c >0,100 >40,78 >80,59 >120,38 >160,25 >200,21 >240,16 >280,12 >320,10 >360,9 >400,7 > >graphdata<-read.csv('~/tmp/data.csv') >graphmodeld<-lm(log(graphdata[,2])~graphdata[,1]) >graphmodelp<-exp(predict(graphmodeld)) >plot(graphdata[,2]~graphdata[,1]) >lines(graphdata[,1],graphmodelp) > >Please what is the function and syntax to obtain gradient values for >the model curve at various requested values, e.g.: > >when graphdata[,1] at values = 100, 250, 350 ? > >______________________________________________ >[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: Obtain gradient at multiple values for exponetial decay model

 > Sent: Thursday, April 05, 2018 at 4:40 PM > From: "Jeff Newmiller" <[hidden email]> > > the coef function. > For the benefit of other novices, used the following command to read the documentation: ?coef Then tried and obtained: > cvalue100<-coef(graphmodelp~100) > cvalue100 NULL Then looked at the model values which of course correspond to original non-modelled values. graphmodelp         1         2         3         4         5         6         7         8 91.244636 69.457794 52.873083 40.248368 30.638107 23.322525 17.753714 13.514590         9        10        11 10.287658  7.831233  5.961339 This prompted to think that interpolation is required, but the function 'approx' only seems to perform constant interpolation. Is the correct thinking to find a function to perform interpolation, then find/write a function to differentiate the model at a specific value of x, to find gradient at that point? ______________________________________________ [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: Obtain gradient at multiple values for exponetial decay model

 Try coef( graphmodeld ) And you don't need to approximate if you use the newdata argument to the predict function. I think reading the "Introduction to R" that comes with R would help. -- Sent from my phone. Please excuse my brevity. On April 5, 2018 2:00:45 PM PDT, g l <[hidden email]> wrote: >> Sent: Thursday, April 05, 2018 at 4:40 PM >> From: "Jeff Newmiller" <[hidden email]> >> >> the coef function. >> > >For the benefit of other novices, used the following command to read >the documentation: > >?coef > >Then tried and obtained: > >> cvalue100<-coef(graphmodelp~100) >> cvalue100 >NULL > >Then looked at the model values which of course correspond to original >non-modelled values. > >graphmodelp >1         2         3         4         5         6         7         8 > >91.244636 69.457794 52.873083 40.248368 30.638107 23.322525 17.753714 >13.514590 >        9        10        11 >10.287658  7.831233  5.961339 > >This prompted to think that interpolation is required, but the function >'approx' only seems to perform constant interpolation. > >Is the correct thinking to find a function to perform interpolation, >then find/write a function to differentiate the model at a specific >value of x, to find gradient at that point? ______________________________________________ [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: Obtain gradient at multiple values for exponetial decay model

 In reply to this post by g l > On Apr 5, 2018, at 2:00 PM, g l <[hidden email]> wrote: > >> Sent: Thursday, April 05, 2018 at 4:40 PM >> From: "Jeff Newmiller" <[hidden email]> >> >> the coef function. >> > > For the benefit of other novices, used the following command to read the documentation: > > ?coef > > Then tried and obtained: > >> cvalue100<-coef(graphmodelp~100) >> cvalue100 > NULL Should have been:   coef(graphmodelp) > > Then looked at the model values which of course correspond to original non-modelled values. > > graphmodelp >        1         2         3         4         5         6         7         8 > 91.244636 69.457794 52.873083 40.248368 30.638107 23.322525 17.753714 13.514590 >        9        10        11 > 10.287658  7.831233  5.961339 Read up on ?predict and what it delivers when only a model is offered as input. > > This prompted to think that interpolation is required, but the function 'approx' only seems to perform constant interpolation. > > Is the correct thinking to find a function to perform interpolation, then find/write a function to differentiate the model at a specific value of x, to find gradient at that point? Not correct. You already have `predict`. It is capale of using the `newdata` values to do interpolation with the values of the coefficients in the model. See: ?predict The original question asked for a derivative (i.e. a "gradient"), but so far it's not clear that you understand the mathematical definiton of that term. We also remain unclear whether this is homework. > > ______________________________________________ > [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. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 In reply to this post by Jeff Newmiller > Sent: Friday, April 06, 2018 at 4:53 AM > From: "Jeff Newmiller" <[hidden email]> > To: "g l" <[hidden email]> > coef( graphmodeld ) > coef(graphmodelp) Error: \$ operator is invalid for atomic vectors A quick search engine query revealed primarily references to the dollar sign (\$) operator which does not seem relevant to this question. ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 In reply to this post by David Winsemius > Sent: Friday, April 06, 2018 at 5:55 AM > From: "David Winsemius" <[hidden email]> > > > Not correct. You already have `predict`. It is capale of using the `newdata` values to do interpolation with the values of the coefficients in the model. See: > > ?predict > The § details did not mention interpolation explicity; thanks.   > The original question asked for a derivative (i.e. a "gradient"), but so far it's not clear that you understand the mathematical definiton of that term. We also remain unclear whether this is homework. > The motivation of this post was simple differentiation of a tangent point (dy/dx) manually, then wondering how to re-think in modern-day computing terms. Hence the original question about asking the appropriate functions/syntax to read further ("curiosity"), not the answer (indeed, "homework"). :) Personal curiosity should be considered "homework". ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 In reply to this post by g l You did not try my suggestion. You tried David's, which has a leftover mistake from your guesses about what the argument to coef should be. -- Sent from my phone. Please excuse my brevity. On April 6, 2018 3:30:10 AM PDT, g l <[hidden email]> wrote: >> Sent: Friday, April 06, 2018 at 4:53 AM >> From: "Jeff Newmiller" <[hidden email]> >> To: "g l" <[hidden email]> >> coef( graphmodeld ) >> > >coef(graphmodelp) >Error: \$ operator is invalid for atomic vectors > >A quick search engine query revealed primarily references to the dollar >sign (\$) operator which does not seem relevant to this question. ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 > Sent: Friday, April 06, 2018 at 1:44 PM > From: "Jeff Newmiller" <[hidden email]> > > You did not try my suggestion. You tried David's, which has a leftover mistake from your guesses about what the argument to coef should be. Yes, sorry for the mistake. coef(graphmodeld)    (Intercept) graphdata[, 1]    4.513544204   -0.006820623 This corresponds to the linear model but it is still not understood how this helps to determine gradients at specific points for the exponential model. ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 > coef(graphmodeld) >    (Intercept) graphdata[, 1] >    4.513544204   -0.006820623 > > This corresponds to the linear model but it is still not understood how this helps > to determine gradients at specific points for the exponential model. You have fitted log(y) against x. Your fitted linear model is therefore log(y) = 4.5135 - 0.00682x From there: - do the basic algebra to get y in terms of x - differentiate analytically to get dy/dx as a function of x. - write that calculation in R to get your gradients at any value of x. Or you could get R to give you the differential function from your function for y; see ?D S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}} ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 In reply to this post by g l > On Apr 6, 2018, at 3:43 AM, g l <[hidden email]> wrote: > >> Sent: Friday, April 06, 2018 at 5:55 AM >> From: "David Winsemius" <[hidden email]> >> >> >> Not correct. You already have `predict`. It is capale of using the `newdata` values to do interpolation with the values of the coefficients in the model. See: >> >> ?predict >> > > The § details did not mention interpolation explicity; thanks. > >> The original question asked for a derivative (i.e. a "gradient"), but so far it's not clear that you understand the mathematical definiton of that term. We also remain unclear whether this is homework. >> > > The motivation of this post was simple differentiation of a tangent point (dy/dx) manually, then wondering how to re-think in modern-day computing terms. Hence the original question about asking the appropriate functions/syntax to read further ("curiosity"), not the answer (indeed, "homework"). :) > > Personal curiosity should be considered "homework". Besides symbolic differentiation, there is also the option of numeric differentiation. Here's an amateurish attempt: myNumDeriv <- function(x){ (exp( predict (graphmodeld, newdata=data.frame(t=x+.0001))) -                                             exp( predict (graphmodeld, newdata=data.frame(t=x) )))/                                           .0001 } myNumDeriv(c(100, 250, 350)) David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 > On Apr 6, 2018, at 8:03 AM, David Winsemius <[hidden email]> wrote: > > >> On Apr 6, 2018, at 3:43 AM, g l <[hidden email]> wrote: >> >>> Sent: Friday, April 06, 2018 at 5:55 AM >>> From: "David Winsemius" <[hidden email]> >>> >>> >>> Not correct. You already have `predict`. It is capale of using the `newdata` values to do interpolation with the values of the coefficients in the model. See: >>> >>> ?predict >>> >> >> The § details did not mention interpolation explicity; thanks. >> >>> The original question asked for a derivative (i.e. a "gradient"), but so far it's not clear that you understand the mathematical definiton of that term. We also remain unclear whether this is homework. >>> >> >> The motivation of this post was simple differentiation of a tangent point (dy/dx) manually, then wondering how to re-think in modern-day computing terms. Hence the original question about asking the appropriate functions/syntax to read further ("curiosity"), not the answer (indeed, "homework"). :) >> >> Personal curiosity should be considered "homework". > > Besides symbolic differentiation, there is also the option of numeric differentiation. Here's an amateurish attempt: > > myNumDeriv <- function(x){ (exp( predict (graphmodeld, newdata=data.frame(t=x+.0001))) - >                                            exp( predict (graphmodeld, newdata=data.frame(t=x) )))/ >                                          .0001 } > myNumDeriv(c(100, 250, 350)) I realized that this would not work in the context of your construction. I had earlier made a more symbolic version using R formulae:  graphdata<-read.csv(text='t,c  0,100  40,78  80,59  120,38  160,25  200,21  240,16  280,12  320,10  360,9  400,7')  graphmodeld<-lm(log(c)~t, graphdata)  graphmodelp<-exp(predict(graphmodeld))  plot(c~t, graphdata)  lines(graphdata[,1],graphmodelp)  myNumDeriv(c(100, 250, 350), graphmodeld ) #----------------------------------------------           1           2           3 -0.31464102 -0.11310753 -0.05718414 > > > > David Winsemius > Alameda, CA, USA > > 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law > > ______________________________________________ > [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. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 > On Apr 6, 2018, at 8:15 AM, David Winsemius <[hidden email]> wrote: > >> >> On Apr 6, 2018, at 8:03 AM, David Winsemius <[hidden email]> wrote: >> >> >>> On Apr 6, 2018, at 3:43 AM, g l <[hidden email]> wrote: >>> >>>> Sent: Friday, April 06, 2018 at 5:55 AM >>>> From: "David Winsemius" <[hidden email]> >>>> >>>> >>>> Not correct. You already have `predict`. It is capale of using the `newdata` values to do interpolation with the values of the coefficients in the model. See: >>>> >>>> ?predict >>>> >>> >>> The § details did not mention interpolation explicity; thanks. >>> >>>> The original question asked for a derivative (i.e. a "gradient"), but so far it's not clear that you understand the mathematical definiton of that term. We also remain unclear whether this is homework. >>>> >>> >>> The motivation of this post was simple differentiation of a tangent point (dy/dx) manually, then wondering how to re-think in modern-day computing terms. Hence the original question about asking the appropriate functions/syntax to read further ("curiosity"), not the answer (indeed, "homework"). :) >>> >>> Personal curiosity should be considered "homework". >> >> Besides symbolic differentiation, there is also the option of numeric differentiation. Here's an amateurish attempt: >> >> myNumDeriv <- function(x){ (exp( predict (graphmodeld, newdata=data.frame(t=x+.0001))) - >>                                           exp( predict (graphmodeld, newdata=data.frame(t=x) )))/ >>                                         .0001 } >> myNumDeriv(c(100, 250, 350)) > > I realized that this would not work in the context of your construction. I had earlier made a more symbolic version using R formulae: > > graphdata<-read.csv(text='t,c > 0,100 > 40,78 > 80,59 > 120,38 > 160,25 > 200,21 > 240,16 > 280,12 > 320,10 > 360,9 > 400,7') > graphmodeld<-lm(log(c)~t, graphdata) > graphmodelp<-exp(predict(graphmodeld)) > plot(c~t, graphdata) > lines(graphdata[,1],graphmodelp) Again I attempt to correct my incomplete code with this definition that had be modified to take a model object as its second argument: myNumDeriv <- function(x, mod) (exp( predict (mod, newdata=data.frame(t=x+.0001))) - exp( predict (mod, newdata=data.frame(t=x) )))/.0001 > myNumDeriv(c(100, 250, 350), graphmodeld ) > #---------------------------------------------- >          1           2           3 > -0.31464102 -0.11310753 -0.05718414 > > >> >> >> >> David Winsemius >> Alameda, CA, USA >> >> 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law >> >> ______________________________________________ >> [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. > > David Winsemius > Alameda, CA, USA > > 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law > > ______________________________________________ > [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. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 In reply to this post by David Winsemius I have never found the R symbolic differentiation helpful because my functions are typically quite complicated, but was prompted by Steve Ellison's suggestion to try it out in this case: ################# reprex (see reprex package) graphdta <- read.csv( text = "t,c 0,100 40,78 80,59 120,38 160,25 200,21 240,16 280,12 320,10 360,9 400,7 ", header = TRUE ) nd <- c( 100, 250, 300 ) graphmodeld <- lm( log(c) ~ t, data = graphdta ) graphmodelplin <- predict( graphmodeld                           , newdata = data.frame( t = nd )                           ) graphmodelp <- exp(graphmodelplin) graphmodelp #>        1        2        3 #> 46.13085 16.58317 11.79125 # derivative of exp( a + b*t ) is b * exp( a + b*t ) graphmodeldpdt <- coef( graphmodeld )[ 2 ] * graphmodelp graphmodeldpdt #>           1           2           3 #> -0.31464113 -0.11310757 -0.08042364 # Ellison suggestion - fancy, only works for simple functions dc <- deriv( expression( exp( a + b * t ) )             , namevec = "t"             ) dcf <- function( t ) {    cgm <- coef( graphmodeld )    a <- cgm[ 1 ]    b <- cgm[ 2 ]    eval(dc) } result <- dcf( nd ) result #> [1] 46.13085 16.58317 11.79125 #> attr(,"gradient") #>                t #> [1,] -0.31464113 #> [2,] -0.11310757 #> [3,] -0.08042364 attr( result, "gradient" )[ , 1 ] #> [1] -0.31464113 -0.11310757 -0.08042364 ################# On Fri, 6 Apr 2018, David Winsemius wrote: > >> On Apr 6, 2018, at 8:03 AM, David Winsemius <[hidden email]> wrote: >> >> >>> On Apr 6, 2018, at 3:43 AM, g l <[hidden email]> wrote: >>> >>>> Sent: Friday, April 06, 2018 at 5:55 AM >>>> From: "David Winsemius" <[hidden email]> >>>> >>>> >>>> Not correct. You already have `predict`. It is capale of using the `newdata` values to do interpolation with the values of the coefficients in the model. See: >>>> >>>> ?predict >>>> >>> >>> The ? details did not mention interpolation explicity; thanks. >>> >>>> The original question asked for a derivative (i.e. a "gradient"), but so far it's not clear that you understand the mathematical definiton of that term. We also remain unclear whether this is homework. >>>> >>> >>> The motivation of this post was simple differentiation of a tangent point (dy/dx) manually, then wondering how to re-think in modern-day computing terms. Hence the original question about asking the appropriate functions/syntax to read further ("curiosity"), not the answer (indeed, "homework"). :) >>> >>> Personal curiosity should be considered "homework". >> >> Besides symbolic differentiation, there is also the option of numeric differentiation. Here's an amateurish attempt: >> >> myNumDeriv <- function(x){ (exp( predict (graphmodeld, newdata=data.frame(t=x+.0001))) - >>                                            exp( predict (graphmodeld, newdata=data.frame(t=x) )))/ >>                                          .0001 } >> myNumDeriv(c(100, 250, 350)) > > I realized that this would not work in the context of your construction. I had earlier made a more symbolic version using R formulae: > > graphdata<-read.csv(text='t,c > 0,100 > 40,78 > 80,59 > 120,38 > 160,25 > 200,21 > 240,16 > 280,12 > 320,10 > 360,9 > 400,7') > graphmodeld<-lm(log(c)~t, graphdata) > graphmodelp<-exp(predict(graphmodeld)) > plot(c~t, graphdata) > lines(graphdata[,1],graphmodelp) > myNumDeriv(c(100, 250, 350), graphmodeld ) > #---------------------------------------------- >          1           2           3 > -0.31464102 -0.11310753 -0.05718414 > > >> >> >> >> David Winsemius >> Alameda, CA, USA >> >> 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law >> >> ______________________________________________ >> [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. > > David Winsemius > Alameda, CA, USA > > 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law > > ______________________________________________ > [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. > --------------------------------------------------------------------------- Jeff Newmiller                        The     .....       .....  Go Live... DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...                                        Live:   OO#.. Dead: OO#..  Playing Research Engineer (Solar/Batteries            O.O#.       #.O#.  with /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k ______________________________________________ [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: Obtain gradient at multiple values for exponential decay model

 R users may want to note that there are some extensions in packages for symbolic derivatives. In particular, Duncan Murdoch added some "all in R" tools in the package nlsr that I maintain. This is a substitute for the nls() function that uses a fairly unsatisfactory forward difference derivative approximation. Moreover, the solver in nlsr is a variant of the Marquardt stabilized Gauss-Newton approach, rather than the straight Gauss-Newton that often gives a "singular gradient" error. Nothing is free, of course, and the Marquardt approach often uses more iterations when the problem is uncomplicated. On the other hand, it is rather like a pit bull in trying to find a solution. There is also the Deriv package, also "all in R". Both it and nlsr allow extensions to the derivatives table, though I think most users will need to do some homework to become comfortable with doing that. There is also a Google Summer of Code proposal this year to wrap the Julia Automatic Differentiation tools to R. This would allow derivatives of coded functions to be computed avoiding approximations. I believe it will be at least partly successful in achieving its goals. Unfortunately (and I would rather hope that someone can say eventually that I am wrong), I believe no tool is universally applicable. JN On 2018-04-07 02:19 AM, Jeff Newmiller wrote: > I have never found the R symbolic differentiation helpful because my functions are typically quite complicated, but was > prompted by Steve Ellison's suggestion to try it out in this case: > > ################# reprex (see reprex package) > graphdta <- read.csv( text = > "t,c > 0,100 > 40,78 > 80,59 > 120,38 > 160,25 > 200,21 > 240,16 > 280,12 > 320,10 > 360,9 > 400,7 > ", header = TRUE ) > > nd <- c( 100, 250, 300 ) > graphmodeld <- lm( log(c) ~ t, data = graphdta ) > graphmodelplin <- predict( graphmodeld >                          , newdata = data.frame( t = nd ) >                          ) > graphmodelp <- exp(graphmodelplin) > graphmodelp > #>        1        2        3 > #> 46.13085 16.58317 11.79125 > # derivative of exp( a + b*t ) is b * exp( a + b*t ) > graphmodeldpdt <- coef( graphmodeld )[ 2 ] * graphmodelp > graphmodeldpdt > #>           1           2           3 > #> -0.31464113 -0.11310757 -0.08042364 > > # Ellison suggestion - fancy, only works for simple functions > dc <- deriv( expression( exp( a + b * t ) ) >            , namevec = "t" >            ) > dcf <- function( t ) { >   cgm <- coef( graphmodeld ) >   a <- cgm[ 1 ] >   b <- cgm[ 2 ] >   eval(dc) > } > result <- dcf( nd ) > result > #> [1] 46.13085 16.58317 11.79125 > #> attr(,"gradient") > #>                t > #> [1,] -0.31464113 > #> [2,] -0.11310757 > #> [3,] -0.08042364 > attr( result, "gradient" )[ , 1 ] > #> [1] -0.31464113 -0.11310757 -0.08042364 > ################# > > On Fri, 6 Apr 2018, David Winsemius wrote: > >> >>> On Apr 6, 2018, at 8:03 AM, David Winsemius <[hidden email]> wrote: >>> >>> >>>> On Apr 6, 2018, at 3:43 AM, g l <[hidden email]> wrote: >>>> >>>>> Sent: Friday, April 06, 2018 at 5:55 AM >>>>> From: "David Winsemius" <[hidden email]> >>>>> >>>>> >>>>> Not correct. You already have `predict`. It is capale of using the `newdata` values to do interpolation with the >>>>> values of the coefficients in the model. See: >>>>> >>>>> ?predict >>>>> >>>> >>>> The ? details did not mention interpolation explicity; thanks. >>>> >>>>> The original question asked for a derivative (i.e. a "gradient"), but so far it's not clear that you understand the >>>>> mathematical definiton of that term. We also remain unclear whether this is homework. >>>>> >>>> >>>> The motivation of this post was simple differentiation of a tangent point (dy/dx) manually, then wondering how to >>>> re-think in modern-day computing terms. Hence the original question about asking the appropriate functions/syntax to >>>> read further ("curiosity"), not the answer (indeed, "homework"). :) >>>> >>>> Personal curiosity should be considered "homework". >>> >>> Besides symbolic differentiation, there is also the option of numeric differentiation. Here's an amateurish attempt: >>> >>> myNumDeriv <- function(x){ (exp( predict (graphmodeld, newdata=data.frame(t=x+.0001))) - >>>                                            exp( predict (graphmodeld, newdata=data.frame(t=x) )))/ >>>                                          .0001 } >>> myNumDeriv(c(100, 250, 350)) >> >> I realized that this would not work in the context of your construction. I had earlier made a more symbolic version >> using R formulae: >> >> graphdata<-read.csv(text='t,c >> 0,100 >> 40,78 >> 80,59 >> 120,38 >> 160,25 >> 200,21 >> 240,16 >> 280,12 >> 320,10 >> 360,9 >> 400,7') >> graphmodeld<-lm(log(c)~t, graphdata) >> graphmodelp<-exp(predict(graphmodeld)) >> plot(c~t, graphdata) >> lines(graphdata[,1],graphmodelp) >> myNumDeriv(c(100, 250, 350), graphmodeld ) >> #---------------------------------------------- >>          1           2           3 >> -0.31464102 -0.11310753 -0.05718414 >> >> >>> >>> >>> >>> David Winsemius >>> Alameda, CA, USA >>> >>> 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law >>> >>> ______________________________________________ >>> [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. >> >> David Winsemius >> Alameda, CA, USA >> >> 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law >> >> ______________________________________________ >> [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. >> > > --------------------------------------------------------------------------- > Jeff Newmiller                        The     .....       .....  Go Live... > DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go... >                                       Live:   OO#.. Dead: OO#..  Playing > Research Engineer (Solar/Batteries            O.O#.       #.O#.  with > /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k > > ______________________________________________ > [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.