predict.flexPM: Prediction from Fitted Flexible Parametric Models

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/flexPM.R

Description

Predicts the distribution function and simulates new data from a fitted model.

Usage

1
2
## S3 method for class 'flexPM'
predict(object, type = c("CDF", "QF", "sim"), newdata, p, ...)

Arguments

object

an object of class “flexPM”.

type

the type of prediction (see ‘Details’).

newdata

an optional data frame in which to look for variables with which to predict. If omitted, the model frame of the object is used.

p

the order(s) of the quantile to be computed (for type = "QF")

...

for future methods.

Details

New data can be supplied: observe that for type = "CDF", newdata must include the values of the response variable, and not just the covariates.

Value

All types of prediction are computed at newdata, if supplied, or at the observed data, otherwise.

Author(s)

Paolo Frumento paolo.frumento@ki.se

See Also

flexPM

Examples

 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
# Using simulated data

set.seed(1111); n <- 1000
x <- runif(n)
t <- rnorm(n, 1 + x, 1 + x)
model <- flexPM(Surv(t) ~ x + I(x^2)) 
# using polynomials (e.g. x^2) to achieve flexibility



# Prediction of the conditional cumulative distribution function (CDF)
# and the probability density function (PDF)

pred <- predict(model, type = "CDF") # predict the CDF and PDF

plot(pnorm(t, 1 + x, 1 + x), exp(pred$log.F))
abline(0,1, col = "green", lwd = 3) # true vs fitted CDF

plot(dnorm(t, 1 + x, 1 + x), exp(pred$log.f))
abline(0,1, col = "green", lwd = 3) # true vs fitted PDF



# Prediction of quantiles

predMe <- predict(model, type = "QF", p = 0.5) # predict the median
plot(x,t)
points(x, predMe$p0.5, col = "green") # fitted median
abline(1,1, col = "red", lwd = 3) # true median = 1 + x



# Simulate data from the fitted model

t.sim <- predict(model, type = "sim")
plot(quantile(t.sim, (1:9)/10), quantile(t, (1:9)/10)); abline(0,1)
# if the model is good, t and t.sim should have a similar distribution



######### Using new data #############################

newdata <- data.frame(t = c(0,1,2), x = c(0.1,0.5,0.9))
# note that new 't' is only needed for type = "CDF"

predict(model, type = "CDF", newdata = newdata)
predict(model, type = "QF", newdata = newdata, p = c(0.25,0.5,0.75))
predict(model, type = "sim", newdata = newdata)

flexPM documentation built on May 2, 2019, 10:17 a.m.