0

I am sure there is a way to do this but I am not sure how to show the variable names for my output along with my exponentiated results for a relative risk output for a project I am working on. Below includes some sample code. It is probably relatively easy, but I am not sure how to extract the variable name/factor level column so that it is side-by-side with the risk ratio outputs so that I do not have to look at the numbered rows from the prior output and compare with my exponentiated output. The first column isn't truly stored as a column, from what I can tell. Basically, for my output I want the first one to say "(Intercept) 0.24 (0.06-1.01)" and so on. Thanks!!!

> cov.m1 <- vcovHC(sexrr_modadj_mm, type = "HC0")
> std.err <- sqrt(diag(cov.m1))
> q.val <- qnorm(0.975)
> r.est <- cbind(
+   Estimate = coef(sexrr_modadj_mm)
+   , "Robust SE" = std.err
+   , z = (coef(sexrr_modadj_mm)/std.err)
+   , "Pr(>|z|) "= 2 * pnorm(abs(coef(sexrr_modadj_mm)/std.err), lower.tail = FALSE)
+   , LL = coef(sexrr_modadj_mm) - q.val  * std.err
+   , UL = coef(sexrr_modadj_mm) + q.val  * std.err
+ )
> r.est
                                                   Estimate    Robust SE            z     Pr(>|z|)             LL
(Intercept)                                   -1.430923e+00 0.7337880617  -1.95004986  5.117018e-02 -2.869121e+00
PrimaryAnesthesiaTypeGeneral                   5.283219e-02 0.5460422343   0.09675477  9.229211e-01 -1.017391e+00
PrimaryAnesthesiaTypeMonitor Anesthesia Care   1.874183e+00 0.9023864714   2.07691834  3.780910e-02  1.055380e-01
PrimaryAnesthesiaTypeNeuraxial Nerve Block    -2.096085e+00 1.1418850877  -1.83563536  6.641160e-02 -4.334138e+00
PrimaryAnesthesiaTypePeripheral Nerve Block   -1.315466e+01 0.6931926700 -18.97691303  2.646985e-80 -1.451329e+01
PrimaryAnesthesiaTypeRegional                 -7.875437e-02 0.4793121244  -0.16430706  8.694894e-01 -1.018189e+00
PrimaryBlockEpidural                           2.211594e-01 0.1356256444   1.63066036  1.029620e-01 -4.466202e-02

> paste(round(exp(r.est[,1]), digits = 2), " (", round(exp(r.est[,5]), digits = 2), "-", round(exp(r.est[,6]), digits = 2), ")", sep = "")

 [1] "0.24 (0.06-1.01)"    "1.05 (0.36-3.07)"    "6.52 (1.11-38.2)"    "0.12 (0.01-1.15)"    "0 (0-0)"            
 [6] "0.92 (0.36-2.36)"    "1.25 (0.96-1.63)"    "0 (0-0)"             "0 (0-0)"             "0 (0-0)"  

davidk
  • 133
  • 2
  • 11
  • 2
    `paste(round(... the rest of your code ...)) |> setNames(row.names(r.est))` will take the row names from `r.est` and apply them to your vector. – Gregor Thomas Mar 10 '23 at 15:48
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Maybe just share `dput(r.est)` because it doesn't really seem to matter what the model was if you are just working with the matrix output. – MrFlick Mar 10 '23 at 15:48
  • 2
    If you like `dplyr` or working with data frames, you might find the `broom` package a more convenient way to extract and work with your coefficient estimates. – Gregor Thomas Mar 10 '23 at 15:48

1 Answers1

3

You can apply names to a vector with setNames. Seems like you want to apply the row names from your r.est matrix, so this should work:

paste(
  round(exp(r.est[,1]), digits = 2),
  " (",
  round(exp(r.est[,5]), digits = 2),
  "-",
  round(exp(r.est[,6]), digits = 2),
  ")", sep = ""
) |> 
  setNames(row.names(r.est))

I find your code pretty hard to read, often sprintf is cleaner for "fill-in-the-blank" style string construction. And the format string %.2f will round to 2 decimal places without another function call. We can also use column names to make things clearer. Something like this (though I'm confused becuase the r.est you show seems to only have 5 columns yet you reference the 6th... I guess UL didn't make it into your question.)

sprintf(
  "%.2f (%.2f-%.2f)",
  exp(r.est[, "Estimate"]), 
  exp(r.est[, "LL"]), 
  exp(r.est[, "UL"])
) |>
  setNames(row.names(r.est))

I'd also suggest looking at the broom package to get everything into nice data frames.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294