0

I am doing a thesis, with the help of self learning, and seeing answers in StackExchange, I almost reached the end of the problem I need to solve. Here is the final hurdle in completing my project.

How to measure area between 2 distribution curves in R / ggplot2

in the answer given in the above link, how to calculate the area between the curves and the region whose X value is greater than 40?

 jj<-which(h>0 & seq_along(h) %% 5==0); j<-i[jj]; 
 segments(j, f1(j), j, f1(j)+h[jj])
 p<-h <- f1(i)-f0(i)
 p<-f1(i)-f0(i)
 area<-sum( (p[-1]+p[-length(p)]) /2 *diff(i) *(p[-1]>=0+0))
 area
 [1] 0.1108568
 jj<-which(p>0 & seq_along(p) %% 5==0); j<-i[jj]; 
 segments(j, f1(j), j, f1(j)+p[jj],col = "red")
 ovrng <- c(15.5(28.8(d0$x), 15.5(d1$x)), 15.5(28.8(d0$x), 28.8(d1$x)))

Error: attempt to apply non-function

the condition of X value greater than a certain number. And the area between the curves satisfying the condition?

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Could you explain what you are attempting to achieve when you type `15.5(28.8(d0$x)`, `15.5(d1$x)` , `15.5(28.8(d0$x)` and `28.8(d1$x)` on the last line please? Are you trying to multiply these values? – NM_ Mar 24 '19 at 19:14
  • Hi, thanks for the reply. those are the range of X values in which i am trying to find the area between the curves – chanakya chan Mar 24 '19 at 19:21
  • Numeric values have to be separated by a comma, so the format is `value1, value2, ... , valueN`. An expression like `15.5(28.8(d0$x)` will not calculate a numeric value. If you just type `15.5(28.8(d0$x)` into the R console, you will see that you will get an error since this expression does not perform a calculation. – NM_ Mar 24 '19 at 19:26
  • Thanks, @NM_ for the reply, but it didn't work. this is the result , if i reapplied ----- ovrng <- c(15(29(d0$x), 15(d1$x)), 15(28.8(d0$x), 29(d1$x))) Error: attempt to apply non-function – chanakya chan Mar 24 '19 at 19:39
  • maybe there has been some misunderstanding. `15.5(28.8(d0$x)`, `15.5(d1$x))`, `15.5(28.8(d0$x), 28.8(d1$x))` are NOT correct expressions. They do not calculate any values. – NM_ Mar 24 '19 at 19:41
  • from your [i was following this](https://stackoverflow.com/questions/24742677/how-to-measure-area-between-2-distribution-curves-in-r-ggplot2), you'll see that you perhaps want to put `ovrng <- c(max(min(d0$x), min(d1$x)), min(max(d0$x), max(d1$x)))`, as max and min are functions and will get you out of the attempt to apply non-function error. You appear to be plugging in values, your 15.5 & 28.8, of a variable that must have a range d0$x, but your might want to ask if 15.5 would be the max when 15.5 < 28.8 . And then merely copying the next lines should get you out of the woods. HTH – Chris Mar 25 '19 at 03:22

0 Answers0