
This function takes a model and adds a non-linear function if
the likelihood-ratio supports this (via the
anova(..., test = "chisq")
test for stats
while for rms you need to use the rcs()
spline
that is automatically evaluated for non-linearity).
addNonlinearity(
model,
variable,
spline_fn,
flex_param = 2:7,
min_fn = AIC,
sig_level = 0.05,
verbal = FALSE,
workers,
...
)# S3 method for negbin
addNonlinearity(model, ...)
The model that is to be evaluated and adapted for non-linearity
The name of the parameter that is to be tested for non-linearity. Note that the variable should be included plain (i.e. as a linear variable) form in the model.
Either a string or a function that is to be used for testing alternative non-linearity models
A vector
with values that are to be tested as the
default second parameter for the non-linearity function that you want to
evaluate. This defaults to 2:7, for the ns()
it tests
the degrees of freedom ranging between 2 and 7.
This is the function that we want to minimized if the variable supports
the non-linearity assumption. E.g. BIC()
or
AIC
, note that the BIC()
will in the majority of cases
support a lower complexity than the AIC()
.
The significance level for which the non-linearity is deemed as significant, defaults to 0.05.
Set this to TRUE
if you want print statements with the
anova test and the chosen knots.
The function tries to run everything in parallel. Under some
circumstances you may want to restrict the number of parallel threads to less
than the default detectCores() - 1
, e.g. you may run out of memory
then you can provide this parameter. If you do not want to use parallel then
simply set workers to FALSE
. The cluster created using makeCluster()
function.
Passed onto internal prNlChooseDf()
function.
library(Greg)
library(magrittr)
data("melanoma", package = "boot", envir = environment())
library(dplyr)
library(magrittr)
melanoma %<>%
mutate(status = factor(status,
levels = 1:3,
labels = c("Died from melanoma",
"Alive",
"Died from other causes")),
ulcer = factor(ulcer,
levels = 0:1,
labels = c("Absent", "Present")),
time = time/365.25, # All variables should be in the same time unit
sex = factor(sex,
levels = 0:1,
labels = c("Female", "Male")))
library(survival)
model <- coxph(Surv(time, status == "Died from melanoma") ~ sex + age,
data = melanoma)
nl_model <- addNonlinearity(model, "age",
spline_fn = "pspline",
verbal = TRUE,
workers = FALSE)
# Note that there is no support for nonlinearity in this case
Run the code above in your browser using DataLab