https://github.com/cran/Hmisc
Raw File
Tip revision: b3af9c0b37f7854d668d17579dd9c690bfa60d9f authored by Charles Dupont on 10 February 2012, 16:36:05 UTC
version 3.9-2
Tip revision: b3af9c0
inverseFunction.r
library(Hmisc)

z <- 
structure(list(x = c(-1.01157732356344, -0.844512148091014, -0.723389895873506, 
-0.598091014269186, -0.518735055919784, -0.42684920940995, -0.347493251060548, 
-0.263960663324335, -0.113602005399152, 0.195468569224836, 0.441889703046664, 
0.746783648283841, 0.901318935595835, 0.947261858850752, 0.99738141149248
), y = c(-1.0034980323568, -0.861827721906428, -0.668211630957586, 
-0.49820725841714, -0.309313511149978, -0.0920857017927416, 0.0637516397026673, 
0.0920857017927417, 0.0212505465675558, -0.0826410144293835, 
-0.0873633581110625, 0.0684739833843463, 0.517096633143857, 0.75321381722781, 
0.894884127678181)), .Names = c("x", "y"))
library(rms)
dd <- datadist(as.data.frame(z)); options(datadist='dd')
f <- ols(y ~ rcs(x,5), data=z)
par(mfrow=c(1,2))
plot(f)
abline(v=c(-.1772,.31375))
points(z)
xx <- seq(-1,1,length=1000)
g <- Function(f)
h <- inverseFunction(xx, g(xx))
plot(xx[-1], diff(g(xx)))
abline(h=0)
par(mfrow=c(1,1))

plot(f)
turns <- formals(h)$turns
abline(v=turns)

a <- seq(-1.2,1.2,by=.001)
w <- h(a)
for(i in 1:ncol(w)) lines(w[,i], a, col=i+1)
w <- h(a, what='sample')
points(w, a, col='gray')


x <- seq(-1, 1, by=.01)
y <- x^2
h <- inverseFunction(x,y)
formals(h)$turns   # vertex
a <- seq(0, 1, by=.01)
plot(0, 0, type='n', xlim=c(-.5,1.5))
lines(a, h(a)[,1])            ## first inverse
lines(a, h(a)[,2], col='red') ## second inverse
a <- c(-.1, 1.01, 1.1, 1.2)
points(a, h(a)[,1])


back to top