Prediction method for objects of class 'haz2ts'
Arguments
- x
an object of class
'haz2ts', the output of the functionfit2ts().- newdata
(optional) A dataframe with columns cointaing the values of the variable
uand the time scalesfor which predictions are to be obtained.- originaldata
(optional) The original dataset. Provide it to obtain individual predictions for each observation in the data.
- u
The name of the variable in
newdata, or inoriginaldatacontaining values for the variableu.- s
The name of the variable in
newdata, or inoriginaldatacontaining values for the variables. Note that over thesaxis predictions are provided only within intervals of values, as it is necessary to approximate cumulated quantities.- id
(optional) The name of the variable in
newdata, or inoriginaldatacontaining the identification of each observation. It is not required for predictions on a new dataset.- ds
(optional) The distance between two consecutive points on the
saxis. If not provided, an optimal minimum value will be chosen automatically and a warning is returned.
Value
A dataframe. This can be the original dataframe (originaldata), where only the
variables id, u and s are selected, or the new data frame (newdata),
together with the predicted values for the hazard hazard and its
standard errors se_hazard, the cumulative hazard cumhazard and the
survival probability survival.
Details
Predictions of cumulated quantities can be provided only within intervals of values on the s time scale.
Examples
# Create the same fake data as in other examples
id <- 1:20
u <- c(
5.43, 3.25, 8.15, 5.53, 7.28, 6.61, 5.91, 4.94, 4.25, 3.86, 4.05, 6.86,
4.94, 4.46, 2.14, 7.56, 5.55, 7.60, 6.46, 4.96
)
s <- c(
0.44, 4.89, 0.92, 1.81, 2.02, 1.55, 3.16, 6.36, 0.66, 2.02, 1.22, 3.96,
7.07, 2.91, 3.38, 2.36, 1.74, 0.06, 5.76, 3.00
)
ev <- c(1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1)
fakedata <- as.data.frame(cbind(id, u, s, ev))
fakedata2ts <- prepare_data(
u = fakedata$u,
s_out = fakedata$s,
ev = fakedata$ev,
ds = .5
)
#> `s_in = NULL`. I will use `s_in = 0` for all observations.
#> `s_in = NULL`. I will use `s_in = 0` for all observations.
# Fit a fake model - not optimal smoothing
fakemod <- fit2ts(fakedata2ts,
optim_method = "grid_search",
lrho = list(
seq(1, 1.5, .5),
seq(1, 1.5, .5)
)
)
# Create a new dataset for prediction
newdata <- as.data.frame(cbind("u" = c(2.5, 3.4, 6), "s" = c(.2, .5, 1.3)))
# First - predict on original data
predict(object = fakemod,
originaldata = fakedata, u = "u", s = "s", id = "id"
)
#> id u s hazard se_hazard cumhazard survival
#> 1 1 5.43 0.44 0.13740670 0.07599797 0.068130275 0.9341388
#> 2 2 3.25 4.89 0.19402638 0.26679324 0.878037756 0.4155976
#> 3 3 8.15 0.92 0.07537982 0.08786874 0.072945278 0.9296517
#> 4 4 5.53 1.81 0.14230098 0.05878897 0.261603915 0.7698159
#> 5 5 7.28 2.02 0.09957468 0.07538578 0.197452069 0.8208195
#> 6 6 6.61 1.55 0.11306136 0.06689691 0.174471579 0.8399007
#> 7 7 5.91 3.16 0.13795103 0.07874811 0.419712583 0.6572357
#> 8 8 4.94 6.36 0.17380895 0.16712236 1.023687049 0.3592679
#> 9 9 4.25 0.66 0.16062214 0.09048239 0.111104019 0.8948457
#> 10 10 3.86 2.02 0.17251792 0.08212664 0.350826575 0.7041059
#> 11 11 4.05 1.22 0.16653113 0.08299609 0.211805170 0.8091223
#> 12 12 6.86 3.96 0.11983805 0.13696457 0.436677400 0.6461799
#> 13 13 4.94 7.07 0.17804729 0.19944893 1.147048980 0.3175726
#> 14 14 4.46 2.91 0.16848507 0.07543581 0.486023334 0.6150675
#> 15 15 2.14 3.38 0.19342902 0.25092140 0.611328238 0.5426296
#> 16 16 7.56 2.36 0.09528778 0.08449232 0.212827761 0.8082953
#> 17 17 5.55 1.74 0.14135558 0.05906538 0.246494518 0.7815356
#> 18 18 7.60 0.06 0.08160222 0.10813840 0.008160222 0.9918730
#> 19 19 6.46 5.76 0.13931938 0.21728709 0.717137521 0.4881476
#> 20 20 4.96 3.00 0.15953865 0.06751365 0.475091887 0.6218279
# Now - predict on new dataset
predict(object = fakemod,
newdata = newdata, u = "u", s = "s"
)
#> chosen interval: ds = 0.2
#> Warning: Right boundary adjusted to max(x) = 7.5
#> Warning: Right boundary adjusted to max(x) = 7.5
#> Warning: Right boundary adjusted to max(x) = 7.5
#> u s hazard se_hazard cumhazard survival
#> 1 2.5 0.2 0.1689391 0.2039663 0.06733798 0.9348792
#> 2 3.4 0.5 0.1670538 0.1338833 0.09949784 0.9052919
#> 3 6.0 1.3 0.1276804 0.0642310 0.17422551 0.8401074