# Re: [R] Obtain gradient at multiple values for exponential decay model

```> On Apr 6, 2018, at 8:15 AM, David Winsemius <dwinsem...@comcast.net> wrote:
>
>>
>> On Apr 6, 2018, at 8:03 AM, David Winsemius <dwinsem...@comcast.net> wrote:
>>
>>
>>> On Apr 6, 2018, at 3:43 AM, g l <gnuli...@gmx.com> wrote:
>>>
>>>> Sent: Friday, April 06, 2018 at 5:55 AM
>>>> From: "David Winsemius" <dwinsem...@comcast.net>
>>>>
>>>>
>>>> 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
>>> "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
>
> 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
>>
>> ______________________________________________
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> 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
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help