RoyalTS
RoyalTS

Reputation: 10203

analytical derivative of splinefun()

I'm trying to fit a natural cubit spline to probabilistic data (probabilities that a random variable is smaller than certain values) to obtain a cumulative distribution function, which works well enough using splinefun():

cutoffs <- c(-90,-60,-30,0,30,60,90,120)
probs <- c(0,0,0.05,0.25,0.5,0.75,0.9,1)
CDF.spline <- splinefun(cutoffs,probs, method="natural")
plot(cutoffs,probs)
curve(CDF.spline(x), add=TRUE, col=2, n=1001)

I would then, however, like to use the density function, i.e. the derivative of the spline, to perform various calculations (e.g. to obtain the expected value of the random variable).

Is there any way of obtaining this derivative as a function rather than just evaluated at a discrete number of points via splinefun(x, deriv=1)?

This is pretty close to what I'm looking for, but alas the example doesn't seem to work in R version 2.15.0.

Barring an analytical solution, what's the cleanest numerical way of going about this?

Upvotes: 0

Views: 1136

Answers (1)

IRTFM
IRTFM

Reputation: 263362

If you change the environment assignment line for g in the code the Berwin Turlach provided on R-help to this:

environment(g) <- environment(f)

... you succeed in R 2.15.1.

Upvotes: 1

Related Questions