This repository was archived by the owner on Apr 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscalingFun.R
More file actions
149 lines (114 loc) · 4.66 KB
/
scalingFun.R
File metadata and controls
149 lines (114 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
##===============================================================
## 'scalingFun1d' computes the 'scaling' transformation at
## the one-dimensional design points x.
##
## The knots and (positive) derivatives definitng the transfo-
## -mation are given in numeric vectors 'knots' and 'eta'.
##
##===============================================================
#' @example plot(function(x)scalingFun1d(x,c(0,1),c(.5,.5)))
#' @example plot(function(x)scalingFun1d(x,c(0,1),c(.5,.75)))
#' @example plot(function(x)scalingFun1d(x,0,.5))
#' @example plot(function(x)scalingFun1d(x,c(0,1),c(.5,.75)),xlim=c(-1,2))
#' @example plot(function(x)scalingFun1d(x,c(0,1.5),c(.5,1.75)),xlim=c(-1,2))
#' @example plot(function(x)scalingFun1d(x,c(0,.5,1),c(10.5,.5,1.75)),xlim=c(-1,2))
scalingFun1d <- function(x, knots, eta){
n <- length(x)
nKnots <- length(knots)
#if ( any(x < knots[1]-1E-6) | any(x > knots[nKnots]+1E-6) )
# warning("'x' values should be inside the knots (otherwise using closest knot)\nknots =",paste(knots,collapse=","),"\nx = ",paste(x,collapse=","))
ix_upper= which(x > knots[nKnots])
ix_lower = which(x < knots[1])
if (length(ix_lower)>0 & length(ix_upper)>0)
ix_inside = (1:n)[-c(ix_upper,ix_lower)]
else if (length(ix_lower)>0 & length(ix_upper)==0)
ix_inside = (1:n)[-ix_lower]
else if (length(ix_upper)>0 & length(ix_lower)==0)
ix_inside = (1:n)[-ix_upper]
else ix_inside=1:n
newscale_inside = scalingFun1d.inside(x[ix_inside],knots, eta)
newscale <- rep(NA, n)
newscale[ix_inside] = newscale_inside
## Support for outside knots
if(length(ix_lower)>0) newscale[ix_lower]=eta[1]*(x[ix_lower]-knots[1])
if(length(ix_upper)>0) {
y0=scalingFun1d.inside(knots[nKnots],knots, eta)
newscale[ix_upper]=eta[nKnots]*(x[ix_upper]-knots[nKnots])+y0
}
return(newscale)
}
#' Do NOT support when x is outside bounds of knots...
#' @perf x=runif(100); k=seq(0,1,,10);e=runif(10); scalingFun1d.inside.faster(x,k,e)
#' @perf Rprof("scaling.prof", line.profiling=TRUE); x=runif(100); k=seq(0,1,,10);e=runif(10); for(i in 1:10000) scalingFun1d.inside(x,k,e); Rprof(NULL); summaryRprof("scaling.prof", lines = "show")
scalingFun1d.inside <- function(x, knots, eta){
n <- length(x)
nKnots <- length(knots)
if (nKnots == 1) return(eta*(x-knots))
if ( any(x < knots[1]) | any(x > knots[nKnots]) )
stop("'x' values should be inside the knots (otherwise using closest knot)\nknots =",paste(knots,collapse=","),"\nx = ",paste(x,collapse=","))
if (length(x)>1){
xs <- sort(x, index.return = TRUE)
xsorted <- xs$x
ind <- xs$ix
} else{
xsorted=x
ind=1
}
scale <- rep(0, n)
## cat("Calling C\n")
res <- .C("Scale",
n = as.integer(n),
nKnots = as.integer(nKnots),
x = as.double(xsorted),
knots = as.double(knots),
eta = as.double(eta),
scale = as.double(scale))
## CAUTION here: the values are for SORTED x values
## they must be put back in the original order.
newscale <- rep(NA, n)
newscale[ind] <- res$scale
return(newscale)
}
##==================================================================
## 'scalingFun' applies d one-dimensional 'scaling' transformations
## to n d-dimensional design points
##
## o 'X' must be a n*d matrix,
##
## o 'knots' and 'eta' are list of lentgh d. In both cases,
## the element i contains the knots/derivatives for the
## dimension i
##
## At this time, no control on the list/matrix dimension or length
##
##==================================================================
scalingFun <- function(X, knots, eta, plot = FALSE) {
d <- NCOL(X)
transX <- matrix(NA, nrow = NROW(X), ncol = NCOL(X))
dimnames(transX) <- dimnames(X)
X <- as.matrix(X)
for (i in 1:d){
transX[ , i] <- scalingFun1d(x = X[ , i], knots = knots[[i]],
eta = eta[[i]])
}
if (plot) {
for(i in 1:d) {
plot(X[ ,i], transX[ ,i], type = "o",
pch = 21, col = "orangered", cex = 0.8,
xlab = "x", ylab = "x 'scaled'")
abline(v = knots[[i]], col = "SpringGreen3")
}
}
return(transX)
}
affineScalingFun <- function(X, knots, eta) {
# Here X is meant to be a n*d matrix,
# knots is a (K+1) vector (in this special case),
# and eta is a d*(K+1) matrix of coefficients.
d <- dim(X)[2]
transformed_X <- matrix(NA, nrow=nrow(X), ncol=ncol(X))
for(i in seq(1,d)){
transformed_X[,i] <- scalingFun1d(x=X[,i], knots=knots, eta = eta[i,])
}
return(transformed_X)
}