# tutorial

## package preparation

    library(rms)
    library(base.rms)
    library(survival)

## 1. transform between linear regressions

### base to rms

    fit <- lm(mpg~cyl+vs,data=mtcars)
    lm2ols(fit)

    ## Linear Regression Model
    ##
    ##  ols(formula = mpg ~ cyl + vs, data = mtcars, model = TRUE, x = TRUE,
    ##      y = TRUE)
    ##
    ##                  Model Likelihood    Discrimination
    ##                        Ratio Test           Indexes
    ##  Obs      32    LR chi2     41.70    R2       0.728
    ##  sigma3.2481    d.f.            2    R2 adj   0.710
    ##  d.f.     29    Pr(> chi2) 0.0000    g        5.641
    ##
    ##  Residuals
    ##
    ##       Min       1Q   Median       3Q      Max
    ##  -4.92324 -1.95291 -0.08097  1.31867  7.57676
    ##
    ##
    ##            Coef    S.E.   t     Pr(>|t|)
    ##  Intercept 39.6250 4.2246  9.38 <0.0001
    ##  cyl       -3.0907 0.5581 -5.54 <0.0001
    ##  vs        -0.9391 1.9775 -0.47 0.6384
    ##

### rms to base

    fit <- ols(mpg~cyl+vs,data=mtcars)
    ols2lm(fit)

    ##
    ## Call:
    ## lm(formula = mpg ~ cyl + vs, data = mtcars, model = TRUE, x = TRUE,
    ##     y = TRUE)
    ##
    ## Coefficients:
    ## (Intercept)          cyl           vs
    ##     39.6250      -3.0907      -0.9391

## 2. transform between logistic regressions

### base to rms

    fit <- glm(vs~mpg,data=mtcars,family = binomial(link='logit'))
    logit2lrm(fit)

    ## Logistic Regression Model
    ##
    ##  lrm(formula = vs ~ mpg, data = mtcars, model = TRUE, x = TRUE,
    ##      y = TRUE)
    ##
    ##                         Model Likelihood    Discrimination    Rank Discrim.
    ##                               Ratio Test           Indexes          Indexes
    ##  Obs            32    LR chi2      18.33    R2       0.584    C       0.911
    ##   0             18    d.f.             1    g        2.925    Dxy     0.821
    ##   1             14    Pr(> chi2) <0.0001    gr      18.637    gamma   0.825
    ##  max |deriv| 2e-05                          gp       0.399    tau-a   0.417
    ##                                             Brier    0.130
    ##
    ##            Coef    S.E.   Wald Z Pr(>|Z|)
    ##  Intercept -8.8331 3.1623 -2.79  0.0052
    ##  mpg        0.4304 0.1584  2.72  0.0066
    ##

### rms to base

    fit <- lrm(vs~mpg,data=mtcars)
    lrm2logit(fit)

    ##
    ## Call:  glm(formula = vs ~ mpg, family = binomial(link = "logit"), data = mtcars,
    ##     model = TRUE, x = TRUE, y = TRUE)
    ##
    ## Coefficients:
    ## (Intercept)          mpg
    ##     -8.8331       0.4304
    ##
    ## Degrees of Freedom: 31 Total (i.e. Null);  30 Residual
    ## Null Deviance:       43.86
    ## Residual Deviance: 25.53     AIC: 29.53

## 3. transform between cox regressions

### base to rms

    fit <- coxph(Surv(mpg,vs)~am+gear,data=mtcars)
    coxph2cph(fit)

    ## Cox Proportional Hazards Model
    ##
    ##  cph(formula = Surv(mpg, vs) ~ am + gear, data = mtcars, model = TRUE,
    ##      x = TRUE, y = TRUE)
    ##
    ##                          Model Tests    Discrimination
    ##                                                Indexes
    ##  Obs         32    LR chi2      9.35    R2       0.311
    ##  Events      14    d.f.            2    Dxy      0.514
    ##  Center -2.0911    Pr(> chi2) 0.0093    g        1.275
    ##                    Score chi2  10.16    gr       3.579
    ##                    Pr(> chi2) 0.0062
    ##
    ##       Coef    S.E.   Wald Z Pr(>|Z|)
    ##  am   -2.0159 0.9041 -2.23  0.0258
    ##  gear -0.3450 0.6999 -0.49  0.6221
    ##

### rms to base

    fit <- cph(Surv(mpg,vs)~am+gear,data=mtcars)
    cph2coxph(fit)

    ## Call:
    ## coxph(formula = Surv(mpg, vs) ~ am + gear, data = mtcars, model = TRUE,
    ##     x = TRUE, y = TRUE)
    ##
    ##         coef exp(coef) se(coef)      z      p
    ## am   -2.0159    0.1332   0.9041 -2.230 0.0258
    ## gear -0.3450    0.7082   0.6999 -0.493 0.6221
    ##
    ## Likelihood ratio test=9.35  on 2 df, p=0.009328
    ## n= 32, number of events= 14