1
+ cumfd <- function (xrnd , xrng , nbreaks = 7 , nfine = 101 ) {
2
+ # Compute cdf_fd over a closed interval using smooth.morph.
3
+ # Only the values of x within the interior of xrng are used
4
+ # in order to avoid distortion due to boundary inflation.
5
+ # Arguments:
6
+ # xrnd ... A vector of variable values (unsorted)
7
+ # xrng ... A vector of length 2 containing the boundary values.
8
+ # Wnbasis ... Number of basis functions used by smooth.morph.
9
+ # Snbasis ... Number of basis functions used by smooth.basis.
10
+ # nfine ... Length of vector of equally spaced values spanning xrng.
11
+
12
+ # Last modified 25 March 2022 by Jim Ramsay
13
+
14
+ # check that values of x are within xrng
15
+
16
+ if (min(xrnd ) < xrng [1 ] || max(xrnd ) > xrng [2 ])
17
+ stop(" Values of x outside of xrng." )
18
+
19
+ # sort the data and set up probability values
20
+
21
+ xsort <- sort(xrnd [xrnd > xrng [1 ] & xrnd < xrng [2 ]])
22
+ N <- length(xsort )
23
+ prbvec <- (1 : N )/ (N + 1 )
24
+
25
+ # add boundary values
26
+
27
+ pmesh <- c(0 , prbvec , 1 )
28
+ xmesh <- c(xrng [1 ], xsort , xrng [2 ])
29
+
30
+ # set up fdPar object for smooth.morph
31
+
32
+ index = c(1 , round(N * 2 : (nbreaks - 1 )/ nbreaks ), N + 2 )
33
+
34
+ Wnorder <- 4
35
+ Wnbasis <- nbreaks + Wnorder - 2
36
+ Wbreaks <- xmesh [index ]
37
+ Wbasis <- create.bspline.basis(xrng , Wnbasis , Wnorder , Wbreaks )
38
+ WfdPar <- fdPar(fd(matrix (0 ,Wnbasis ,1 ), Wbasis ))
39
+
40
+ # use smooth.morph to map sorted data into the interior of [0,1]
41
+
42
+ result <- smooth.morph(xmesh , pmesh , c(0 ,1 ), WfdPar )
43
+ xfine <- seq(0 ,1 ,len = 101 )
44
+ Wfdobj <- result $ Wfdobj
45
+
46
+ cdffine <- result $ hfine
47
+ cdffine [1 ] <- 0
48
+ cdffine [length(cdffine )] <- 1
49
+
50
+ # plot(xfine, cdffine, type="l")
51
+ # points(xmesh, pmesh)
52
+
53
+ # plot(Wfdobj)
54
+
55
+ return (list (Wfdobj = Wfdobj , cdffine = cdffine ))
56
+
57
+ }
0 commit comments