1
1
# Authors Mikis Stasinopoulos Bob Rigby and Vlasios Voudouris
2
2
# created 11-04-12
3
- # upadated 8--6-14
4
- # -------------------------------------------------------------------------------
3
+ # updated 8--6-14
4
+ # ###############################################################################
5
+ # ###############################################################################
6
+ # ###############################################################################
7
+ # ###############################################################################
5
8
# what is new June 2014
6
9
# i) the power transformation (x^p) function ptrans() now is defined to include
7
10
# zero as log(x)
8
11
# ii) the power transformation uses GAIC instead GD which was not reliable
9
12
# since different transformation will use differt effective df's
10
13
# iii) a prediction function for an lms object is created
11
- # -------------------------------------------------------------------------------
12
- # -------------------------------------------------------------------------------
14
+ # ###############################################################################
15
+ # ###############################################################################
16
+ # ###############################################################################
17
+ # ###############################################################################
13
18
# This function is design to help the user to construct centile estimation.
14
19
# It is only applicable when only "one" explanatory variable is available
15
20
# (usually age).
23
28
# If the response variable contains negative values and/or zeros then use
24
29
# the argument theSHASH theSHASH <- c("NO", "SHASHo") or add any other
25
30
# distribution which you think is appropriate
26
- # -------------------------------------------------------------------------------
31
+ # ###############################################################################
32
+ # ###############################################################################
33
+ # ###############################################################################
34
+ # ###############################################################################
27
35
# the LMS familily of distributions
28
36
LMS <- c(" BCCGo" , " BCPEo" , " BCTo" )
29
37
# the SHASH
30
38
theSHASH <- c(" NO" , " SHASHo" )
31
- # -------------------------------------------------------------------------------
32
- # -------------------------------------------------------------------------------
39
+ # ###############################################################################
40
+ # ###############################################################################
41
+ # ###############################################################################
42
+ # ###############################################################################
33
43
lms <- function (y , x ,
34
44
families = LMS ,
35
45
data = NULL ,
@@ -51,7 +61,7 @@ lms <- function(y, x,
51
61
...
52
62
)
53
63
{
54
- # -------------------------------------------------------------------------------
64
+ # ###############################################################################
55
65
# local function
56
66
findPower <- function (y , x , data = NULL , lim.trans = c(0 , 1.5 ), prof = FALSE , k = 2 , c.crit = 0.01 , step = 0.1 )
57
67
{
@@ -79,27 +89,30 @@ findPower <- function(y, x, data = NULL, lim.trans = c(0, 1.5), prof=FALSE, k=2
79
89
}
80
90
par
81
91
}
82
- # -------------------------------------------------------------------------------
92
+ # ###############################################################################
83
93
ptrans <- function (x , p ) if (p == 0 ) log(x ) else I(x ^ p )
84
- # -------------------------------------------------------------------------------
94
+ # ###############################################################################
85
95
# end of local function
86
- # -------------------------------------------------------------------------------
96
+ # ###############################################################################
97
+ # ###############################################################################
87
98
# # the families to fit
88
- FAM <- families
99
+ FAM <- families
100
+ # ###############################################################################
89
101
# # which method
90
102
method.pb <- match.arg(method.pb )
91
103
# # get the variables
92
104
ylab <- deparse(substitute(y ))
93
105
xlab <- deparse(substitute(x ))
94
106
y <- if (! is.null(data )) get(deparse(substitute(y )), envir = as.environment(data )) else y
95
107
x <- if (! is.null(data )) get(deparse(substitute(x )), envir = as.environment(data )) else x
96
- # # -----------------------------------------------------------------------------
108
+ # ###############################################################################
97
109
# # if need to check for transformation in x
98
110
if (is.null(fix.power ))
99
111
{
100
112
if (trans.x ) # if x^p
101
113
{
102
- par <- findPower(y , x , lim.trans = lim.trans , prof = prof , k = k , c.crit = c.crit , step = 0.1 )
114
+ par <- findPower(y , x , lim.trans = lim.trans , prof = prof , k = k ,
115
+ c.crit = c.crit , step = 0.1 )
103
116
ox <- x
104
117
x <- ptrans(x ,par )
105
118
}
@@ -112,6 +125,7 @@ ptrans<- function(x, p) if (p==0) log(x) else I(x^p)
112
125
# # starting model for fitted values for mu (we assuming that this will work).
113
126
# # Note no sigma is fitted here
114
127
# # fit the model --------------------------------------------------------------
128
+ # ###############################################################################
115
129
cat(' *** Initial fit***' ," \n " )
116
130
switch (method.pb ,
117
131
" ML" = {m0 <- gamlss(y ~ pb(x ), sigma.formula = ~ 1 , data = data , c.crit = 0.01 )},
@@ -122,7 +136,8 @@ ptrans<- function(x, p) if (p==0) log(x) else I(x^p)
122
136
aic <- AIC(m0 , k = k )
123
137
fits <- c(fits , aic )
124
138
whichdist <- 0
125
- # # fitting the diferent models in FAM ------------------------------------------
139
+ # ###############################################################################
140
+ # # fitting the different models in FAM
126
141
for (i in 1 : length(FAM ))
127
142
{
128
143
cat(' *** Fitting' , FAM [i ], " ***" ," \n " )
@@ -167,7 +182,21 @@ if(whichdist==0)
167
182
sigma.formula = ~ pb(x ),
168
183
data = data , c.crit = 0.01 )}) # # initial fit finish
169
184
}
170
- # # changing the call t look better in the output -------------------------------
185
+ # ###############################################################################
186
+ # ###############################################################################
187
+ # # calibration -----------------------------------------------------------------
188
+ if (calibration )
189
+ {
190
+ calibration(m0 , xvar = x , cent = cent , pch = 15 , cex = 0.5 , col = gray(0.7 ), ylab = ylab , xlab = xlab , legend = legend )
191
+ }
192
+ else
193
+ {
194
+ centiles(m0 , xvar = x , cent = cent , pch = 15 , cex = 0.5 ,
195
+ col = gray(0.7 ), ylab = ylab , xlab = xlab , legend = legend )
196
+ }
197
+ # ###############################################################################
198
+ # ###############################################################################
199
+ # # changing the call to look better in the output ------------------------------
171
200
m0 $ call $ mu.start <- NULL # this works OK
172
201
m0 $ call $ data <- substitute(data ) # this is OK
173
202
m0 $ call $ family <- if (whichdist == 0 ) " NO" else FAM [whichdist ] # this is OK
@@ -194,7 +223,6 @@ m0$call$mu.start <- NULL # this works OK
194
223
# FaM
195
224
# m0$call$family <- substitute(Fam)
196
225
# m0$call
197
- #
198
226
# sub("FAM[i]", as.character(FAM[i]), m0$call,)
199
227
# # transformation needed -------------------------------------------------------
200
228
if (trans.x ||! is.null(fix.power ))
@@ -211,16 +239,6 @@ m0$call$mu.start <- NULL # this works OK
211
239
m0 $ ylab <- ylab
212
240
m0 $ xlab <- xlab
213
241
if (! is.null(data )) m0 $ call $ data <- substitute(data )
214
- # # calibration -----------------------------------------------------------------
215
- if (calibration )
216
- {
217
- calibration(m0 , xvar = x , cent = cent , pch = 15 , cex = 0.5 , col = gray(0.7 ), ylab = ylab , xlab = xlab , legend = legend )
218
- }
219
- else
220
- {
221
- centiles(m0 , xvar = x , cent = cent , pch = 15 , cex = 0.5 ,
222
- col = gray(0.7 ), ylab = ylab , xlab = xlab , legend = legend )
223
- }
224
242
# # saving the fitted functions for mu sigma nu and tau for prediction --------
225
243
if (" mu" %in% m0 $ par )
226
244
{
@@ -242,7 +260,7 @@ if ("tau"%in%m0$par)
242
260
tauFun <- splinefun(x , fitted(m0 ," tau" ), method = " natural" )
243
261
m0 $ tauFun <- tauFun
244
262
}
245
- # ------------------------------------------------------------------------------
263
+ # ###############################################################################
246
264
# deparse((m0$call))
247
265
# toString(m0$call)
248
266
# toString(format(m0$call))
@@ -253,9 +271,10 @@ if ("tau"%in%m0$par)
253
271
class(m0 ) <- c(" lms" , class(m0 ))
254
272
m0 # save the last model
255
273
}
256
- # -------------------------------------------------------------------------------
257
- # -------------------------------------------------------------------------------
258
- # -------------------------------------------------------------------------------
274
+ # ###############################################################################
275
+ # ###############################################################################
276
+ # ###############################################################################
277
+ # ###############################################################################
259
278
# se ??
260
279
predict.lms <- function (object ,
261
280
what = c(" all" ," mu" , " sigma" , " nu" , " tau" ),
@@ -321,9 +340,10 @@ predict.lms <- function(object,
321
340
}
322
341
out
323
342
}
324
- # -------------------------------------------------------------------------------
325
- # -------------------------------------------------------------------------------
326
- # -------------------------------------------------------------------------------
343
+ # ###############################################################################
344
+ # ###############################################################################
345
+ # ###############################################################################
346
+ # ###############################################################################
327
347
# this function is appropriate to used when fitted model fails to c
328
348
calibration <- function (object , xvar , cent = c(0.4 , 2 , 10 , 25 , 50 , 75 , 90 , 98 , 99.6 ),# 100*pnorm((-4:4)*2/3),
329
349
legend = FALSE , fan = FALSE , ... )
@@ -337,18 +357,15 @@ calibration <- function(object, xvar, cent=c(0.4, 2, 10, 25, 50, 75, 90, 98, 99.
337
357
}
338
358
else
339
359
{
340
- cc <- centiles(object , cent = percent , legend = legend , save = TRUE , ... )
360
+ cc <- centiles(object , xvar = xvar , cent = percent , legend = legend , save = TRUE , ... )
341
361
cpp <- cbind(target = cent , calib. = cc [,1 ], sample = cc [,2 ])
342
362
print(cpp , digits = 3 )
343
363
}
344
364
}
345
- # -------------------------------------------------------------------------------
346
- # -------------------------------------------------------------------------------
347
- # -------------------------------------------------------------------------------
348
- # -------------------------------------------------------------------------------
349
- # -------------------------------------------------------------------------------
350
- # -------------------------------------------------------------------------------
351
- # -------------------------------------------------------------------------------
365
+ # ###############################################################################
366
+ # ###############################################################################
367
+ # ###############################################################################
368
+ # ###############################################################################
352
369
z.scores <- function (object , y ,x )
353
370
{
354
371
if (! is(object ," lms" )) stop(paste(" This is not an lms object" , " \n " , " " ))
@@ -371,6 +388,9 @@ z.scores <- function(object, y,x)
371
388
rqres <- qnorm(cdf )
372
389
rqres
373
390
}
374
-
391
+ # ###############################################################################
392
+ # ###############################################################################
393
+ # ###############################################################################
394
+ # ###############################################################################
375
395
376
396
0 commit comments