Calculating an area under a continuous density plot

前端 未结 2 1593
旧巷少年郎
旧巷少年郎 2021-02-10 16:07

I have two density curves plotted using this:

Network <- Mydf$Networks
quartiles <-  quantile(Mydf$Avg.Position,  probs=c(25,50,75)/100)
density <- ggpl         


        
2条回答
  •  粉色の甜心
    2021-02-10 16:28

    Calculate the density seperately and plot that one to start with. Then you can use basic arithmetics to get the estimate. An integration is approximated by adding together the area of a set of little squares. I use the mean method for that. the length is the difference between two x-values, the height is the mean of the y-value at the begin and at the end of the interval. I use the rollmeans function in the zoo package, but this can be done using the base package too.

    require(zoo)
    
    X <- rnorm(100)
    # calculate the density and check the plot
    Y <- density(X) # see ?density for parameters
    plot(Y$x,Y$y, type="l") #can use ggplot for this too
    # set an Avg.position value
    Avg.pos <- 1
    
    # construct lengths and heights
    xt <- diff(Y$x[Y$x

    This gives you a good approximation up to 3 digits behind the decimal sign. If you know the density function, take a look at ?integrate

提交回复
热议问题