Skip to content

Commit 5f04604

Browse files
lms calibrartion is called before reconstructiong the object
1 parent 78b7d2b commit 5f04604

File tree

1 file changed

+63
-43
lines changed

1 file changed

+63
-43
lines changed

R/lms.R

+63-43
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# Authors Mikis Stasinopoulos Bob Rigby and Vlasios Voudouris
22
# created 11-04-12
3-
# upadated 8--6-14
4-
#-------------------------------------------------------------------------------
3+
# updated 8--6-14
4+
################################################################################
5+
################################################################################
6+
################################################################################
7+
################################################################################
58
# what is new June 2014
69
# i) the power transformation (x^p) function ptrans() now is defined to include
710
# zero as log(x)
811
# ii) the power transformation uses GAIC instead GD which was not reliable
912
# since different transformation will use differt effective df's
1013
# iii) a prediction function for an lms object is created
11-
#-------------------------------------------------------------------------------
12-
#-------------------------------------------------------------------------------
14+
################################################################################
15+
################################################################################
16+
################################################################################
17+
################################################################################
1318
#This function is design to help the user to construct centile estimation.
1419
#It is only applicable when only "one" explanatory variable is available
1520
# (usually age).
@@ -23,13 +28,18 @@
2328
# If the response variable contains negative values and/or zeros then use
2429
# the argument theSHASH theSHASH <- c("NO", "SHASHo") or add any other
2530
# distribution which you think is appropriate
26-
#-------------------------------------------------------------------------------
31+
################################################################################
32+
################################################################################
33+
################################################################################
34+
################################################################################
2735
# the LMS familily of distributions
2836
LMS <- c("BCCGo", "BCPEo", "BCTo")
2937
# the SHASH
3038
theSHASH <- c("NO", "SHASHo")
31-
#-------------------------------------------------------------------------------
32-
#-------------------------------------------------------------------------------
39+
################################################################################
40+
################################################################################
41+
################################################################################
42+
################################################################################
3343
lms <- function(y, x,
3444
families = LMS,
3545
data = NULL,
@@ -51,7 +61,7 @@ lms <- function(y, x,
5161
...
5262
)
5363
{
54-
#-------------------------------------------------------------------------------
64+
################################################################################
5565
# local function
5666
findPower <- function(y, x, data = NULL, lim.trans = c(0, 1.5), prof=FALSE, k=2, c.crit = 0.01, step=0.1)
5767
{
@@ -79,27 +89,30 @@ findPower <- function(y, x, data = NULL, lim.trans = c(0, 1.5), prof=FALSE, k=2
7989
}
8090
par
8191
}
82-
#-------------------------------------------------------------------------------
92+
################################################################################
8393
ptrans<- function(x, p) if (p==0) log(x) else I(x^p)
84-
#-------------------------------------------------------------------------------
94+
################################################################################
8595
# end of local function
86-
#-------------------------------------------------------------------------------
96+
################################################################################
97+
################################################################################
8798
## the families to fit
88-
FAM <- families
99+
FAM <- families
100+
################################################################################
89101
## which method
90102
method.pb <- match.arg(method.pb)
91103
## get the variables
92104
ylab <- deparse(substitute(y))
93105
xlab <- deparse(substitute(x))
94106
y <- if (!is.null(data)) get(deparse(substitute(y)), envir=as.environment(data)) else y
95107
x <- if (!is.null(data)) get(deparse(substitute(x)), envir=as.environment(data)) else x
96-
## -----------------------------------------------------------------------------
108+
################################################################################
97109
## if need to check for transformation in x
98110
if (is.null(fix.power))
99111
{
100112
if (trans.x) # if x^p
101113
{
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)
103116
ox <- x
104117
x <- ptrans(x,par)
105118
}
@@ -112,6 +125,7 @@ ptrans<- function(x, p) if (p==0) log(x) else I(x^p)
112125
## starting model for fitted values for mu (we assuming that this will work).
113126
## Note no sigma is fitted here
114127
## fit the model --------------------------------------------------------------
128+
################################################################################
115129
cat('*** Initial fit***'," \n")
116130
switch(method.pb,
117131
"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)
122136
aic <- AIC(m0, k=k)
123137
fits <- c(fits, aic)
124138
whichdist <- 0
125-
## fitting the diferent models in FAM ------------------------------------------
139+
################################################################################
140+
## fitting the different models in FAM
126141
for (i in 1:length(FAM))
127142
{
128143
cat('*** Fitting', FAM[i], "***","\n")
@@ -167,7 +182,21 @@ if(whichdist==0)
167182
sigma.formula=~pb(x),
168183
data =data, c.crit = 0.01)}) ## initial fit finish
169184
}
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 ------------------------------
171200
m0$call$mu.start <- NULL # this works OK
172201
m0$call$data <- substitute(data) # this is OK
173202
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
194223
# FaM
195224
# m0$call$family <- substitute(Fam)
196225
# m0$call
197-
#
198226
# sub("FAM[i]", as.character(FAM[i]), m0$call,)
199227
## transformation needed -------------------------------------------------------
200228
if (trans.x||!is.null(fix.power))
@@ -211,16 +239,6 @@ m0$call$mu.start <- NULL # this works OK
211239
m0$ylab <- ylab
212240
m0$xlab <- xlab
213241
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-
}
224242
## saving the fitted functions for mu sigma nu and tau for prediction --------
225243
if ("mu"%in%m0$par)
226244
{
@@ -242,7 +260,7 @@ if ("tau"%in%m0$par)
242260
tauFun <- splinefun(x, fitted(m0,"tau"), method="natural")
243261
m0$tauFun <- tauFun
244262
}
245-
#------------------------------------------------------------------------------
263+
################################################################################
246264
# deparse((m0$call))
247265
# toString(m0$call)
248266
# toString(format(m0$call))
@@ -253,9 +271,10 @@ if ("tau"%in%m0$par)
253271
class(m0) <- c("lms", class(m0))
254272
m0 # save the last model
255273
}
256-
#-------------------------------------------------------------------------------
257-
#-------------------------------------------------------------------------------
258-
#-------------------------------------------------------------------------------
274+
################################################################################
275+
################################################################################
276+
################################################################################
277+
################################################################################
259278
# se ??
260279
predict.lms <- function(object,
261280
what = c("all","mu", "sigma", "nu", "tau"),
@@ -321,9 +340,10 @@ predict.lms <- function(object,
321340
}
322341
out
323342
}
324-
#-------------------------------------------------------------------------------
325-
#-------------------------------------------------------------------------------
326-
#-------------------------------------------------------------------------------
343+
################################################################################
344+
################################################################################
345+
################################################################################
346+
################################################################################
327347
# this function is appropriate to used when fitted model fails to c
328348
calibration <- function(object, xvar, cent=c(0.4, 2, 10, 25, 50, 75, 90, 98, 99.6),#100*pnorm((-4:4)*2/3),
329349
legend=FALSE, fan=FALSE, ...)
@@ -337,18 +357,15 @@ calibration <- function(object, xvar, cent=c(0.4, 2, 10, 25, 50, 75, 90, 98, 99.
337357
}
338358
else
339359
{
340-
cc <- centiles(object, cent=percent, legend=legend, save=TRUE, ...)
360+
cc <- centiles(object, xvar=xvar, cent=percent, legend=legend, save=TRUE, ...)
341361
cpp<-cbind(target=cent, calib.=cc[,1], sample=cc[,2])
342362
print(cpp, digits=3)
343363
}
344364
}
345-
#-------------------------------------------------------------------------------
346-
#-------------------------------------------------------------------------------
347-
#-------------------------------------------------------------------------------
348-
#-------------------------------------------------------------------------------
349-
#-------------------------------------------------------------------------------
350-
#-------------------------------------------------------------------------------
351-
#-------------------------------------------------------------------------------
365+
################################################################################
366+
################################################################################
367+
################################################################################
368+
################################################################################
352369
z.scores <- function(object, y,x)
353370
{
354371
if (!is(object,"lms")) stop(paste("This is not an lms object", "\n", ""))
@@ -371,6 +388,9 @@ z.scores <- function(object, y,x)
371388
rqres <- qnorm(cdf)
372389
rqres
373390
}
374-
391+
################################################################################
392+
################################################################################
393+
################################################################################
394+
################################################################################
375395

376396

0 commit comments

Comments
 (0)