-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcumfd.R
57 lines (40 loc) · 1.71 KB
/
cumfd.R
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
cumfd <- function(xrnd, xrng, nbreaks=7, nfine=101) {
# Compute cdf_fd over a closed interval using smooth.morph.
# Only the values of x within the interior of xrng are used
# in order to avoid distortion due to boundary inflation.
# Arguments:
# xrnd ... A vector of variable values (unsorted)
# xrng ... A vector of length 2 containing the boundary values.
# Wnbasis ... Number of basis functions used by smooth.morph.
# Snbasis ... Number of basis functions used by smooth.basis.
# nfine ... Length of vector of equally spaced values spanning xrng.
# Last modified 25 March 2022 by Jim Ramsay
# check that values of x are within xrng
if (min(xrnd) < xrng[1] || max(xrnd) > xrng[2])
stop("Values of x outside of xrng.")
# sort the data and set up probability values
xsort <- sort(xrnd[xrnd > xrng[1] & xrnd < xrng[2]])
N <- length(xsort)
prbvec <- (1:N)/(N+1)
# add boundary values
pmesh <- c(0, prbvec, 1)
xmesh <- c(xrng[1], xsort, xrng[2])
# set up fdPar object for smooth.morph
index = c(1, round(N*2:(nbreaks-1)/nbreaks), N+2)
Wnorder <- 4
Wnbasis <- nbreaks + Wnorder - 2
Wbreaks <- xmesh[index]
Wbasis <- create.bspline.basis(xrng, Wnbasis, Wnorder, Wbreaks)
WfdPar <- fdPar(fd(matrix(0,Wnbasis,1), Wbasis))
# use smooth.morph to map sorted data into the interior of [0,1]
result <- smooth.morph(xmesh, pmesh, c(0,1), WfdPar)
xfine <- seq(0,1,len=101)
Wfdobj <- result$Wfdobj
cdffine <- result$hfine
cdffine[1] <- 0
cdffine[length(cdffine)] <- 1
# plot(xfine, cdffine, type="l")
# points(xmesh, pmesh)
# plot(Wfdobj)
return(list(Wfdobj=Wfdobj, cdffine=cdffine))
}