58 lines
2.0 KiB
Plaintext
Raw Normal View History

2025-01-12 00:52:51 +08:00
R Under development (unstable) (2019-04-05 r76323) -- "Unsuffered Consequences"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> #
> # Test out the "return.all" argument of xpred
> # The data set has the virtue of continuous, categorical, and missings
> #
> library(rpart)
> require(survival)
Loading required package: survival
> set.seed(10)
>
> fit1 <- rpart(Surv(pgtime, pgstat) ~ age + eet + g2+grade+gleason +ploidy,
+ stagec, method='poisson')
>
> xgrp <- rep(1:3, length=nrow(stagec)) # explicitly set the xval groups
>
> xfit1 <- xpred.rpart(fit1, xval=xgrp, return.all=T)
> xfit2 <- array(0, dim=dim(xfit1))
> cplist <- as.numeric(dimnames(xfit1)[[2]])
>
> for (i in 1:3) {
+ tfit <- rpart(Surv(pgtime, pgstat) ~ age + eet + g2+grade+gleason +ploidy,
+ stagec, method='poisson', subset=(xgrp !=i))
+ # xvals are actually done on the absolute risk (node's risk /n), not on
+ # the rescaled risk ((node risk)/ (top node risk)) which is the basis
+ # for the printed CP. To get the right answer we need to rescale.
+ cp2 <- cplist * (fit1$frame$dev[1] / fit1$frame$n[1]) /
+ (tfit$frame$dev[1] / tfit$frame$n[1])
+
+ for (j in 1:length(cp2)) {
+ tfit2 <- prune(tfit, cp=cp2[j])
+ temp <- predict(tfit2, newdata=stagec[xgrp==i,], type='matrix')
+ xfit2[xgrp==i, j,] <- temp
+ }
+ }
>
> all.equal(xfit1, xfit2, check.attributes=FALSE)
[1] TRUE
>
> proc.time()
user system elapsed
0.752 0.035 0.782