I am getting some very weird results. I have created a density plot for various continuous response variables, which are independent of each other.

enter image description here

I have noticed that the Density value seems to be very small. I know the height is meaningless and what needs to be interpreted is the area under the curve. Therefore I have tried to check the area under my curve, but I have obtained value much smaller than 1 - namely -15411.15:

strassenverkehr.densities <-plot(density.gs8m, ylim = c(0, 0.03), col = 9, lty = 1, 
                                 xlab = "Abweichung in %", xlim = c(-100,100) )
lines(density.ks12m, col=1, col = 7, lty = 8)
lines(density.bruecke4m, col = 2,lty = 3)
lines(density.bruecke8m, col = 3,lty = 4)
lines(density.bruecke12m, col = 4,lty = 5)
lines(density.FeldwegKies, col = 5,lty = 6)
lines(density.FeldwegAsph, col = 6,lty = 7)

> density.bruecke4m <- density (diff.bruecke4m)
> trapz(density.bruecke4m$x[density.bruecke4m$x < -40])
[1] -15411.45

I am afraid there is something seriously wrong with my analysis. Any ideas?

  • 2
    $\begingroup$ That should indeed not happen. Then again, it's hard to help you if we can't recreate your problem. Can you please edit your post to contain a minimum (!) working (!) example? Thanks! $\endgroup$ Commented Jun 7, 2017 at 8:32
  • 2
    $\begingroup$ Most of your curves have a base about 100 units long and typical densities about 0.01. That checks: 100 * 0.01 = 1. The height units are not meaningless at all: density is probability per unit deviation. That is no more meaningless than density in physical science being mass per unit volume or population density being people per unit area. $\endgroup$
    – Nick Cox
    Commented Jun 7, 2017 at 10:11
  • 1
    $\begingroup$ Please describe the calculation you're trying to do, so we can see that makes sense, and then (if it does) you can ask on a site that takes programming questions whether your code actually does that. $\endgroup$
    – Glen_b
    Commented Jun 7, 2017 at 10:16

1 Answer 1


I guess it is because you wrote

trapz(density.bruecke4m$x[density.bruecke4m$x < -40])

In trapz, x and y should be supplied. Maybe try something like

index <- (density.bruecke4m$x < -40)
trapz(x=density.bruecke4m$x[index], y=density.bruecke4m$y[index])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.