How to get formula used for plotting?

Dear all,
this is my first message I hope it will not be too naive.
I'm using several packages to calculate the hazard function (i.e. fitSmoothHazard, presmooth).
I can plot the results. But I would like to see the formula used to create those plots, something like y = ax + b or y = ax^b + ax or whatever the formula is.
More in general, whenever I calculate a function (regression function, hazard function, density probability function) there is a way to see the final formula?

Thanks a lot, I hope I have been clear.

Cheers,
G.

It varies by what a function within a package brought into namespace with a library call returns as a value. Some functions will return an object that contains an element, which may or may not be shown by default, but can be examined with str(). It is often identified as formula or call.

In the case of fitSmoothHazard() from the {casebase} package, the return value is an oddball S4 object.

library(casebase)
#> See example usage at http://sahirbhatnagar.com/casebase/
# Simulate censored survival data for two outcome types from exponential
# distributions
library(data.table)
nobs <- 500
tlim <- 20

# simulation parameters
b1 <- 200
b2 <- 50

# event type 0-censored, 1-event of interest, 2-competing event
# t observed time/endpoint
# z is a binary covariate
DT <- data.table(z = rbinom(nobs, 1, 0.5))
DT[, `:=`(
  "t_event" = rweibull(nobs, 1, b1),
  "t_comp" = rweibull(nobs, 1, b2)
)]
DT[, `:=`(
  "event" = 1 * (t_event < t_comp) + 2 * (t_event >= t_comp),
  "time" = pmin(t_event, t_comp)
)]
DT[time >= tlim, `:=`("event" = 0, "time" = tlim)]

out_linear <- fitSmoothHazard(event ~ time + z, DT, ratio = 10)
#> 'time' will be used as the time variable
out_log <- fitSmoothHazard(event ~ log(time) + z, DT, ratio = 10)
#> 'time' will be used as the time variable

out_linear@misc$formula
#> event ~ time + z + offset(offset)

# discovered by digging with\
str(out_linear)
#> Formal class 'CompRisk' [package "casebase"] with 41 slots
#>   ..@ originalData    :'data.frame': 500 obs. of  5 variables:
#>   .. ..$ z      : int [1:500] 0 0 1 0 0 1 0 1 1 1 ...
#>   .. ..$ t_event: num [1:500] 167 250 348.5 25.9 129.4 ...
#>   .. ..$ t_comp : num [1:500] 53.06 231.31 27.33 129.95 6.72 ...
#>   .. ..$ event  : num [1:500] 0 0 0 0 2 2 0 2 0 2 ...
#>   .. ..$ time   : num [1:500] 20 20 20 20 6.72 ...
#>   ..@ typeEvents      : num [1:3] 0 1 2
#>   ..@ timeVar         : chr "time"
#>   ..@ eventVar        : chr "event"
#>   ..@ extra           :List of 4
#>   .. ..$ y.integer    : logi TRUE
#>   .. ..$ use.refLevel : num 1
#>   .. ..$ signvec.damlm: num [1:3] 1 1 1
#>   .. ..$ colnames.y   : chr [1:3] "0" "1" "2"
#>   ..@ family          :Formal class 'vglmff' [package "VGAM"] with 23 slots
#>   .. .. ..@ blurb             : chr [1:7] "M" "ultinomial logit model\n\n" "" "Links:    " ...
#>   .. .. ..@ constraints       :  expression({  constraints <- cm.VGAM(matrix(1, M, 1), x = x, bool = FALSE, apply.int = TRUE,  constraints = const| __truncated__
#>   .. .. ..@ deviance          :function (mu, y, w, residuals = FALSE, eta, extra = NULL, summation = TRUE)  
#>   .. .. ..@ fini              :  expression({ })
#>   .. .. ..@ first             :  expression({ })
#>   .. .. ..@ infos             :function (...)  
#>   .. .. ..@ initialize        :  expression({  if (is.factor(y) && is.ordered(y))  warning("response should be nominal, not ordinal")  delete.zero| __truncated__
#>   .. .. ..@ last              :  expression({  misc$link <- "multilogitlink"  misc$earg <- list(multilogitlink = list(M = M, refLevel = use.refLev| __truncated__
#>   .. .. ..@ linkfun           :function (mu, extra = NULL)  
#>   .. .. ..@ linkinv           :function (eta, extra = NULL)  
#>   .. .. ..@ loglikelihood     :function (mu, y, w, residuals = FALSE, eta, extra = NULL, summation = TRUE)  
#>   .. .. ..@ middle            :  expression({ })
#>   .. .. ..@ middle2           :  expression({ })
#>   .. .. ..@ summary.dispersion: logi(0) 
#>   .. .. ..@ vfamily           : chr [1:2] "multinomial" "VGAMcategorical"
#>   .. .. ..@ validparams       :function (eta, y, extra = NULL)  
#>   .. .. ..@ validfitted       :function ()  
#>   .. .. ..@ simslot           :function ()  
#>   .. .. ..@ hadof             :function (eta, extra = list(), linpred.index = 1, w = 1, dim.wz = c(NROW(eta), 
#>     NCOL(eta) * (NCOL(eta) + 1)/2), deriv = 1, ...)  
#>   .. .. ..@ charfun           :function ()  
#>   .. .. ..@ rqresslot         :function ()  
#>   .. .. ..@ deriv             :  expression({  signvec <- extra$signvec.damlm  use.refLevel <- extra$use.refLevel  d.mlm <- extra$d.mlm  ansd <- (| __truncated__
#>   .. .. ..@ weight            :  expression({  mytiny <- (mu < sqrt(.Machine$double.eps)) | (mu > 1 - sqrt(.Machine$double.eps))  if (M == 1) {  w| __truncated__
#>   ..@ iter            : num 6
#>   ..@ predictors      : num [1:2211, 1:2] -4.05 -4.14 -4.25 -3.82 -4.14 ...
#>   .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..$ : chr [1:2211] "5" "6" "8" "10" ...
#>   .. .. ..$ : chr [1:2] "log(mu[,2]/mu[,1])" "log(mu[,3]/mu[,1])"
#>   ..@ assign          :List of 3
#>   .. ..$ (Intercept): int 1
#>   .. ..$ time       : int 2
#>   .. ..$ z          : int 3
#>   ..@ callXm2         : language `<undef>`()
#>   ..@ contrasts       : list()
#>   ..@ df.residual     : int 4416
#>   ..@ df.total        : int 4422
#>   ..@ dispersion      : num 1
#>   ..@ effects         : Named num [1:4422] 30.8859 47.2362 1.2632 -1.5339 -0.0351 ...
#>   .. ..- attr(*, "names")= chr [1:4422] "(Intercept):1" "(Intercept):2" "time:1" "time:2" ...
#>   ..@ offset          : num [1:2211, 1] 1.35 1.35 1.35 1.35 1.35 ...
#>   ..@ qr              :List of 5
#>   .. ..$ qr   : num [1:4422, 1:6] -6.1875 0 0.0192 0 0.0182 ...
#>   .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. ..$ : chr [1:4422] "5:1" "5:2" "6:1" "6:2" ...
#>   .. .. .. ..$ : chr [1:6] "(Intercept):1" "(Intercept):2" "time:1" "time:2" ...
#>   .. ..$ qraux: num [1:6] 1.02 1.02 1.02 1.02 1.02 ...
#>   .. ..$ pivot: int [1:6] 1 2 3 4 5 6
#>   .. ..$ tol  : num 1e-07
#>   .. ..$ rank : int 6
#>   ..@ R               : num [1:6, 1:6] -6.19 0 0 0 0 ...
#>   .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..$ : chr [1:6] "(Intercept):1" "(Intercept):2" "time:1" "time:2" ...
#>   .. .. ..$ : chr [1:6] "(Intercept):1" "(Intercept):2" "time:1" "time:2" ...
#>   .. ..- attr(*, "rank")= int 6
#>   ..@ rank            : int 6
#>   ..@ ResSS           : num 4425
#>   ..@ smart.prediction: list()
#>   ..@ terms           :List of 1
#>   .. ..$ terms:Classes 'terms', 'formula'  language event ~ time + z + offset(offset)
#>   .. .. .. ..- attr(*, "variables")= language list(event, time, z, offset(offset))
#>   .. .. .. ..- attr(*, "offset")= int 4
#>   .. .. .. ..- attr(*, "factors")= int [1:4, 1:2] 0 1 0 0 0 0 1 0
#>   .. .. .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. .. .. ..$ : chr [1:4] "event" "time" "z" "offset(offset)"
#>   .. .. .. .. .. ..$ : chr [1:2] "time" "z"
#>   .. .. .. ..- attr(*, "term.labels")= chr [1:2] "time" "z"
#>   .. .. .. ..- attr(*, "order")= int [1:2] 1 1
#>   .. .. .. ..- attr(*, "intercept")= int 1
#>   .. .. .. ..- attr(*, "response")= int 1
#>   .. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   .. .. .. ..- attr(*, "predvars")= language list(event, time, z, offset(offset))
#>   .. .. .. ..- attr(*, "dataClasses")= Named chr [1:4] "numeric" "numeric" "numeric" "numeric"
#>   .. .. .. .. ..- attr(*, "names")= chr [1:4] "event" "time" "z" "offset(offset)"
#>   ..@ Xm2             : num[0 , 0 ] 
#>   ..@ Ym2             : num[0 , 0 ] 
#>   ..@ xlevels         : list()
#>   ..@ call            : language fitSmoothHazard(formula = event ~ time + z, data = DT, ratio = 10)
#>   ..@ coefficients    : Named num [1:6] -5.6303 -3.7325 0.0335 -0.0219 0.0197 ...
#>   .. ..- attr(*, "names")= chr [1:6] "(Intercept):1" "(Intercept):2" "time:1" "time:2" ...
#>   ..@ constraints     :List of 3
#>   .. ..$ (Intercept): num [1:2, 1:2] 1 0 0 1
#>   .. ..$ time       : num [1:2, 1:2] 1 0 0 1
#>   .. ..$ z          : num [1:2, 1:2] 1 0 0 1
#>   ..@ control         :List of 16
#>   .. ..$ checkwz        : logi TRUE
#>   .. ..$ Check.rank     : logi TRUE
#>   .. ..$ Check.cm.rank  : logi TRUE
#>   .. ..$ convergence    :  expression({  switch(criterion, coefficients = if (iter == 1) iter < maxit else (iter <  maxit && max(abs(new.cri| __truncated__
#>   .. ..$ criterion      : chr "deviance"
#>   .. ..$ epsilon        : num 1e-07
#>   .. ..$ half.stepsizing: logi TRUE
#>   .. ..$ maxit          : num 31
#>   .. ..$ noWarning      : logi FALSE
#>   .. ..$ min.criterion  : Named logi TRUE
#>   .. .. ..- attr(*, "names")= chr "deviance"
#>   .. ..$ save.weights   : logi FALSE
#>   .. ..$ stepsize       : num 1
#>   .. ..$ trace          : logi FALSE
#>   .. ..$ wzepsilon      : num 1.82e-12
#>   .. ..$ xij            : NULL
#>   .. ..$ panic          : logi FALSE
#>   ..@ criterion       :List of 2
#>   .. ..$ deviance     : num 1540
#>   .. ..$ loglikelihood: num -770
#>   ..@ fitted.values   : num [1:2211, 1:3] 0.911 0.9 0.895 0.909 0.909 ...
#>   .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..$ : chr [1:2211] "5" "6" "8" "10" ...
#>   .. .. ..$ : chr [1:3] "0" "1" "2"
#>   ..@ misc            :List of 22
#>   .. ..$ colnames.x      : chr [1:3] "(Intercept)" "time" "z"
#>   .. ..$ colnames.X.vlm  : chr [1:6] "(Intercept):1" "(Intercept):2" "time:1" "time:2" ...
#>   .. ..$ criterion       : chr "deviance"
#>   .. ..$ function.name   : chr "vglm"
#>   .. ..$ history         : num [1:6, 1] 1639 1553 1541 1540 1540 ...
#>   .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. ..$ : NULL
#>   .. .. .. ..$ : chr "deviance"
#>   .. ..$ intercept.only  : logi FALSE
#>   .. ..$ predictors.names: chr [1:2] "log(mu[,2]/mu[,1])" "log(mu[,3]/mu[,1])"
#>   .. ..$ M               : int 2
#>   .. ..$ n               : int 2211
#>   .. ..$ nonparametric   : logi FALSE
#>   .. ..$ nrow.X.vlm      : int 4422
#>   .. ..$ orig.assign     :List of 3
#>   .. .. ..$ (Intercept): int 1
#>   .. .. ..$ time       : int 2
#>   .. .. ..$ z          : int 3
#>   .. ..$ p               : int 3
#>   .. ..$ ncol.X.vlm      : int 6
#>   .. ..$ ynames          : chr [1:3] "0" "1" "2"
#>   .. ..$ link            : chr "multilogitlink"
#>   .. ..$ earg            :List of 1
#>   .. .. ..$ multilogitlink:List of 2
#>   .. .. .. ..$ M       : int 2
#>   .. .. .. ..$ refLevel: num 1
#>   .. ..$ parallel        : logi FALSE
#>   .. ..$ refLevel        : num 1
#>   .. ..$ refLevel.orig   : num 1
#>   .. ..$ dataname        : chr "sampleData"
#>   .. ..$ formula         :Class 'formula'  language event ~ time + z + offset(offset)
#>   .. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   ..@ model           :'data.frame': 0 obs. of  0 variables
#>   ..@ na.action       : list()
#>   ..@ post            : list()
#>   ..@ preplot         : list()
#>   ..@ prior.weights   : num[0 , 0 ] 
#>   ..@ residuals       : num [1:2211, 1:2] 6.44e-05 8.78e-05 1.10e-04 2.26e-05 8.12e-05 ...
#>   .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..$ : chr [1:2211] "5" "6" "8" "10" ...
#>   .. .. ..$ : chr [1:2] "log(mu[,2]/mu[,1])" "log(mu[,3]/mu[,1])"
#>   ..@ weights         : num[0 , 0 ] 
#>   ..@ x               : num [1:2211, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
#>   .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..$ : chr [1:2211] "5" "6" "8" "10" ...
#>   .. .. ..$ : chr [1:3] "(Intercept)" "time" "z"
#>   .. ..- attr(*, "assign")=List of 3
#>   .. .. ..$ (Intercept): int 1
#>   .. .. ..$ time       : int 2
#>   .. .. ..$ z          : int 3
#>   .. ..- attr(*, "orig.assign.lm")= int [1:3] 0 1 2
#>   ..@ y               : num [1:2211, 1:3] 0 0 0 0 0 0 0 0 0 0 ...
#>   .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..$ : chr [1:2211] "5" "6" "8" "10" ...
#>   .. .. ..$ : chr [1:3] "0" "1" "2"

Created on 2023-01-27 with reprex v2.0.2

Dear Technocrat,
thanks for your answer. I will try to follow your indication hoping I can get what I need.

Best regards,
Giovanni

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.