[R] Calculate the area under a curve
David Winsemius
dwinsemius at comcast.net
Mon Aug 24 18:10:54 CEST 2015
On Aug 24, 2015, at 7:23 AM, Jeff Newmiller wrote:
> Read ?predict, paying particular attention to the newdata argument.
I agree that reading help pages is needed but need both the ?predict page and the ?integrate page. I think proximate cause of the error is that `integrate` is not getting an integrand that is a function of "x" so that it can receive a sequence of x-values and return a sequence of y-values. The interpreter is complaining because it's not getting a proper function.
I suppose one could do that with predict, but you need to build that function to accept a single value as the newdata 'x'-argument. One would, of course, need to have the model name in the integrand function matching the name of the lm-object, which at the moment is also a further error waiting to be uncovered.
Decided to avoid creating extraneous objects and assume that the data is in the form of a dataframe named 'inp':
fitP <- lm( SOC~poly(Depth,3), data=inp ) # It's better to use correct column names with a data-argument
integrand <-function(x) predict(fitP, newdata= list(Depth=x)) # single value of x passed to correct name of column, see ?predict
integrate(integrand, lower = 25, upper = 80)
# 7.43651 with absolute error < 8.3e-14
> ---------------------------------------------------------------------------
> Jeff Newmiller The ..... ..... Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
> Live: OO#.. Dead: OO#.. Playing
> Research Engineer (Solar/Batteries O.O#. #.O#. with
> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> On August 24, 2015 2:10:11 AM PDT, CarstenH <cahoff at gmx.de> wrote:
>> Hi all
>>
>> I need to calculate the area under a curve (integral) for the following
>> data
>> pairs:
>>
>> Depth SOC
>> 22.5 0.143
>> 28.5 0.165
>> 34.5 0.131
>> 37.5 0.134
>> 40.5 0.138
>> 43.5 0.107
>> 46.5 0.132
>> 49.5 0.175
>> 52.5 0.087
>> 55.5 0.117
>> 58.5 0.126
>> 61.5 0.13
>> 64.5 0.122
>> 67.5 0.161
>> 71.5 0.144
>> 76.5 0.146
>> 82.5 0.156
>> 94.5 0.132
>>
>> (Table name is P)
>>
>> After reading the data set I assiged the collumns by:
>>
>> /x <- (P$Depth)
>> y <- (P$SOC)
>> /
>>
>> and decided to make a ploynominal function (3rd order):
>>
>> /fitP <- lm( y~poly(x,3,raw=TRUE) )/
>>
>> At the next step I failed. I can plot point and function but am not
>> able to
>> integrate the curve between e.g. depths 20 and 80.
>>
>> If I try:
>> /
>> integrand <-function(fitP1)
>> predict(y)
>> integrate(integrand, lower = 25, upper = 80)/
>>
>> the "Conosle" opend with the message: "Source unavailable or out of
>> sync"
>> and
>> /
>> function(fitP1)
>> predict(y)
>> /
>> )
>>
>>
>> Would be great if somebody could help!
>>
>> Thanks
>>
>> Carsten
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Calculate-the-area-under-a-curve-tp4711418.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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