Prediction method for objects of class 'haz2ts'
Usage
predict_haz2ts(
x,
newdata = NULL,
originaldata = NULL,
u,
s,
z = NULL,
id = NULL,
ds = NULL
)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.- z
Covariates value
- 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)
z1 <- rbinom(n = 20, size = 20, prob = .5)
z2 <- rnorm(n = 20)
fakedata <- as.data.frame(cbind(id, u, s, ev, z1, z2))
fakedata2ts <- prepare_data(
u = fakedata$u,
s_out = fakedata$s,
ev = fakedata$ev,
ds = .5, individual = TRUE,
covs = subset(fakedata, select = c("z1", "z2"))
)
#> `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.03691994 0.07373935 0.017762288 0.9823945
#> 2 2 3.25 4.89 0.05969388 0.14566713 0.318814542 0.7270104
#> 3 3 8.15 0.92 0.01254272 0.03020352 0.010063143 0.9899873
#> 4 4 5.53 1.81 0.04679029 0.08544526 0.074861578 0.9278719
#> 5 5 7.28 2.02 0.02916007 0.05555869 0.042053493 0.9588185
#> 6 6 6.61 1.55 0.03067085 0.05923700 0.038915022 0.9618324
#> 7 7 5.91 3.16 0.05663655 0.09686140 0.129514882 0.8785215
#> 8 8 4.94 6.36 0.09168009 0.16490913 0.410773191 0.6631373
#> 9 9 4.25 0.66 0.05594961 0.10703447 0.038334704 0.9623908
#> 10 10 3.86 2.02 0.06348879 0.12012883 0.130186889 0.8779313
#> 11 11 4.05 1.22 0.06045518 0.11408250 0.076380018 0.9264641
#> 12 12 6.86 3.96 0.06468261 0.11661949 0.141714009 0.8678694
#> 13 13 4.94 7.07 0.10024826 0.18576795 0.478334160 0.6198150
#> 14 14 4.46 2.91 0.06337172 0.11600378 0.170531458 0.8432166
#> 15 15 2.14 3.38 0.05865931 0.14476898 0.247952176 0.7803973
#> 16 16 7.56 2.36 0.03046996 0.05772485 0.046086607 0.9549593
#> 17 17 5.55 1.74 0.04565293 0.08387444 0.069676595 0.9326954
#> 18 18 7.60 0.06 0.01081892 0.02878368 0.001081892 0.9989187
#> 19 19 6.46 5.76 0.11317190 0.22381996 0.316835884 0.7284503
#> 20 20 4.96 3.00 0.06187921 0.10936794 0.159733199 0.8523712
# 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 cumhazard se_hazard survival
#> 1 2.5 0.2 0.08126274 0.03284834 0.16959485 0.9676853
#> 2 3.4 0.5 0.06748275 0.04061256 0.13186860 0.9602011
#> 3 6.0 1.3 0.03576600 0.04321718 0.06870289 0.9577034
# Now - predict including covariates
predict(object = fakemod,
originaldata = fakedata,
u = "u", s = "s", id = "id",
z = c("z1", "z2")
)
#> id u s z1 z2 hazard se_hazard cumhazard survival
#> 1 1 5.43 0.44 11 -0.97585062 0.12826298 0.08315898 0.061707682 0.9401577
#> 2 2 3.25 4.89 13 1.06505732 0.26007865 0.38494470 1.389034543 0.2493159
#> 3 3 8.15 0.92 5 0.13167063 0.02209166 0.03958472 0.017724344 0.9824318
#> 4 4 5.53 1.81 13 0.48862881 0.20385936 0.12857413 0.326162404 0.7216880
#> 5 5 7.28 2.02 16 -1.69945057 0.17842897 0.20855439 0.257323174 0.7731183
#> 6 6 6.61 1.55 10 -1.47073631 0.09514783 0.07311543 0.120723078 0.8862794
#> 7 7 5.91 3.16 9 0.28415034 0.15689263 0.09554667 0.358777657 0.6985296
#> 8 8 4.94 6.36 12 1.33732041 0.35668318 0.40455787 1.598121205 0.2022762
#> 9 9 4.25 0.66 10 0.23669628 0.17356818 0.10304473 0.118922801 0.8878763
#> 10 10 3.86 2.02 11 1.31829338 0.22056539 0.15702850 0.452280194 0.6361759
#> 11 11 4.05 1.22 12 0.52390979 0.23520207 0.12539870 0.297157967 0.7429266
#> 12 12 6.86 3.96 10 0.60674805 0.20065988 0.24511053 0.439628464 0.6442757
#> 13 13 4.94 7.07 9 -0.10993567 0.27770427 0.31544402 1.325064787 0.2657857
#> 14 14 4.46 2.91 13 0.17218172 0.27610256 0.15489111 0.742983943 0.4756924
#> 15 15 2.14 3.38 12 -0.09032729 0.22821521 0.30601612 0.964662831 0.3811117
#> 16 16 7.56 2.36 8 1.92434334 0.07537206 0.11387117 0.114002178 0.8922560
#> 17 17 5.55 1.74 9 1.29839276 0.12646618 0.11188989 0.193015699 0.8244690
#> 18 18 7.60 0.06 12 0.74879127 0.04209123 0.07113787 0.004209123 0.9957997
#> 19 19 6.46 5.76 7 0.55622433 0.24998257 0.39698896 0.699850819 0.4966594
#> 20 20 4.96 3.00 8 -0.54825726 0.15306755 0.10775989 0.395124176 0.6735964
#> basehazard se_basehazard
#> 1 0.03691994 0.07373935
#> 2 0.05969388 0.14566713
#> 3 0.01254272 0.03020352
#> 4 0.04679029 0.08544526
#> 5 0.02916007 0.05555869
#> 6 0.03067085 0.05923700
#> 7 0.05663655 0.09686140
#> 8 0.09168009 0.16490913
#> 9 0.05594961 0.10703447
#> 10 0.06348879 0.12012883
#> 11 0.06045518 0.11408250
#> 12 0.06468261 0.11661949
#> 13 0.10024826 0.18576795
#> 14 0.06337172 0.11600378
#> 15 0.05865931 0.14476898
#> 16 0.03046996 0.05772485
#> 17 0.04565293 0.08387444
#> 18 0.01081892 0.02878368
#> 19 0.11317190 0.22381996
#> 20 0.06187921 0.10936794
# If one wants to predict with only one of the covariates at a different
# value than the baseline, the other one(s) should be fixed at their
# baseline levels
newdata2 <- as.data.frame(cbind("u" = c(2.5, 3.4, 6),
"s" = c(.2, .5, 1.3),
"z1" = c(1, 2, 3),
"z2" = c(0, 0, 0)))
predict(object = fakemod,
newdata = newdata2,
u = "u", s = "s", id = "id",
z = c("z1", "z2")
)
#> 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 z1 z2 hazard cumhazard se_hazard survival basehazard
#> 1 2.5 0.2 1 0 0.09100364 0.03678584 0.1778449 0.9638825 0.08126274
#> 2 3.4 0.5 2 0 0.08463059 0.05093250 0.1406920 0.9503428 0.06748275
#> 3 6.0 1.3 3 0 0.05023105 0.06069576 0.0734553 0.9411095 0.03576600
#> se_basehazard
#> 1 0.16959485
#> 2 0.13186860
#> 3 0.06870289