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.
Hi Carsten, This list is meant to help you solve specific coding problems. What have you tried? A quick google search will provide several packages including caTools, ROCR, AUC, pROC. Look in to some of them, try them out and report back if you have problems 'using' a function instead of just asking 'how can I do this?' As with everything in R, there are many different ways to accomplish the same thing. Regards, Charles On Mon, Aug 24, 2015 at 4:10 AM, 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. >[[alternative HTML version deleted]]
Read ?predict, paying particular attention to the newdata argument. --------------------------------------------------------------------------- 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.
Hi Charles I am new in R and would like to learn/use it in the beginning for easy operations. I already used google for houres and did not find a solution for this in my eyes easy calculation process. I spent long time with reading forums, package handbooks, and tried and error but always failed. But I still believe that somebody could tell me something like: Load package XY and than use that command Z... Sorry if I am naive or half-baked but I guess there shout be a very easy command to calculate the area under a function. E.g. If I use the package AUC: auc(fitP, min=20, max=100) there is an error report which I do not understand: /Error in auc(fitP1, min = 20, max = 100) : object 'ans' not found. / My task in short: I have 20 x and associated 20 y values and created a function by fitP <- lm( y~poly(x,3,raw=TRUE) ) the function looks like this: fitP /Call: lm(formula = y2 ~ poly(x2, 3, raw = TRUE)) Coefficients: (Intercept) poly(x2, 3, raw = TRUE)1 1.125e+01 -5.262e-01 poly(x2, 3, raw = TRUE)2 poly(x2, 3, raw = TRUE)3 9.156e-03 -4.771e-05 / The last step now is to integrate the function. If I put this data by hand in any of many AUC-calculators in the web it works. But I still not find the "easy" way within R. -- View this message in context: http://r.789695.n4.nabble.com/Calculate-the-area-under-a-curve-tp4711418p4711434.html Sent from the R help mailing list archive at Nabble.com.
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
An alternative to your approach is to pass your data to the approxfun or splinefun functions and then use the integrate function on the result. On Mon, Aug 24, 2015 at 3:10 AM, 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
Hi Your data are rather wild so I am not sure if following function can do what you want. I designed it primarily for intagration of peaks from different sources. integ1 <- function (x, y, dm = -Inf, hm = +Inf) { ifelse(dm == -Inf, dm <- min(x), dm <- dm) ifelse(hm == +Inf, hm <- max(x), hm <- hm) vyber <- x <= hm & x >= dm f3 <- splinefun(x, y) osum <- integrate(f3, dm, hm)$value o1 <- (y[x == min(x[vyber])] + y[x == max(x[vyber])]) * (max(x[vyber]) - min(x[vyber]))/2 cista <- osum - o1 return(c(osum, cista)) } From your data:> integ1(P[,1], P[,2])[1] 10.1677308 0.2677308>> integ1(P[,1], P[,2], dm=25, hm=80)[1] 7.5668092 0.1028092>The first number shall be the area between zero and the curve, the second number is area between baseline constructed as a line between lower and margin. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of > CarstenH > Sent: Monday, August 24, 2015 11:10 AM > To: r-help at r-project.org > Subject: [R] Calculate the area under a curve > > 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.