Ruth O'Brien
Ruth O'Brien

Reputation: 163

Function for dpareto in R

I'm wondering if anybody has a function for dpareto written in R?

I'm not able to use the in built function in R as I can't install the libraries.

Thanks!

Upvotes: 1

Views: 1582

Answers (1)

Chris Wheadon
Chris Wheadon

Reputation: 840

It's relatively easy to rip out functions from R packages, for example, from:

http://cran.r-project.org/web/packages/VGAM/VGAM.pdf

dpareto <- function(x, location, shape, log = FALSE) {
    if (!is.logical(log.arg <- log) || length(log) != 1)
        stop("bad input for argument 'log'")
    rm(log)

    L = max(length(x), length(location), length(shape)) 
    x = rep(x, length.out = L);
    location = rep(location, length.out = L);
    shape = rep(shape, length.out = L)

    logdensity = rep(log(0), length.out = L)
    xok = (x > location)
    logdensity[xok] = log(shape[xok]) + shape[xok] * log(location[xok]) -
            (shape[xok]+1) * log(x[xok])
    if (log.arg) logdensity else exp(logdensity)
}

alpha <- 3; k <- exp(1); x <- seq(2.8, 8, len = 300)
## Not run: 
plot(x, dpareto(x, location = alpha, shape = k), type = "l",
        main = "Pareto density split into 10 equal areas")

Upvotes: 2

Related Questions