Description Usage Arguments Value See Also Examples

Input a parametric model and get a forest.

1 2 3 4 5 6 7 8 9 | ```
pmforest(model, data = NULL, zformula = ~., ntree = 500L,
perturb = list(replace = FALSE, fraction = 0.632), mtry = NULL,
applyfun = NULL, cores = NULL, control = ctree_control(teststat =
"quad", testtype = "Univ", mincriterion = 0, saveinfo = FALSE, lookahead
= TRUE, ...), trace = FALSE, ...)
## S3 method for class 'pmforest'
gettree(object, tree = 1L, saveinfo = TRUE,
coeffun = coef, ...)
``` |

`model` |
a model object. The model can be a parametric model with a single binary covariate. |

`data` |
data. If |

`zformula` |
formula describing which variable should be used for partitioning.
Default is to use all variables in data that are not in the model (i.e. |

`ntree` |
number of trees. |

`perturb` |
a list with arguments replace and fraction determining which type of
resampling with |

`mtry` |
number of input variables randomly sampled as candidates at each
node (Default |

`applyfun` |
see |

`cores` |
see |

`control` |
control parameters, see |

`trace` |
a logical indicating if a progress bar shall be printed while the forest grows. |

`...` |
additional parameters passed on to model fit such as weights. |

`object` |
an object returned by pmforest. |

`tree` |
an integer, the number of the tree to extract from the forest. |

`saveinfo` |
logical. Should the model info be stored in terminal nodes? |

`coeffun` |
function that takes the model object and returns the coefficients. Useful when coef() does not return all coefficients (e.g. survreg). |

cforest object

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | ```
library("model4you")
if(require("mvtnorm") & require("survival")) {
## function to simulate the data
sim_data <- function(n = 500, p = 10, beta = 3, sd = 1){
## treatment
lev <- c("C", "A")
a <- rep(factor(lev, labels = lev, levels = lev), length = n)
## correlated z variables
sigma <- diag(p)
sigma[sigma == 0] <- 0.2
ztemp <- rmvnorm(n, sigma = sigma)
z <- (pnorm(ztemp) * 2 * pi) - pi
colnames(z) <- paste0("z", 1:ncol(z))
z1 <- z[,1]
## outcome
y <- 7 + 0.2 * (a %in% "A") + beta * cos(z1) * (a %in% "A") + rnorm(n, 0, sd)
data.frame(y = y, a = a, z)
}
## simulate data
set.seed(123)
beta <- 3
ntrain <- 500
ntest <- 50
simdata <- simdata_s <- sim_data(p = 5, beta = beta, n = ntrain)
tsimdata <- tsimdata_s <- sim_data(p = 5, beta = beta, n = ntest)
simdata_s$cens <- rep(1, ntrain)
tsimdata_s$cens <- rep(1, ntest)
## base model
basemodel_lm <- lm(y ~ a, data = simdata)
## forest
frst_lm <- pmforest(basemodel_lm, ntree = 20,
perturb = list(replace = FALSE, fraction = 0.632),
control = ctree_control(mincriterion = 0))
## personalised models
# (1) return the model objects
pmodels_lm <- pmodel(x = frst_lm, newdata = tsimdata, fun = identity)
class(pmodels_lm)
# (2) return coefficients only (default)
coefs_lm <- pmodel(x = frst_lm, newdata = tsimdata)
# compare predictive objective functions of personalised models versus
# base model
sum(objfun(pmodels_lm)) # -RSS personalised models
sum(objfun(basemodel_lm, newdata = tsimdata)) # -RSS base model
if(require("ggplot2")) {
## dependence plot
dp_lm <- cbind(coefs_lm, tsimdata)
ggplot(tsimdata) +
stat_function(fun = function(z1) 0.2 + beta * cos(z1),
aes(color = "true treatment\neffect")) +
geom_point(data = dp_lm,
aes(y = aA, x = z1, color = "estimates lm"),
alpha = 0.5) +
ylab("treatment effect") +
xlab("patient characteristic z1")
}
}
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.