7

I'm wondering how to get custom model names in the stargazer package for R.

There is an option for model.names which can be set to TRUEor FALSE, but it does not support a vector or names such as model.names = c('OLS','2SLS','GLS').

Is there any way to override the function to use custom names passed as parameters instead of reading the model names from the objects passed?

Nicolas
  • 2,297
  • 3
  • 28
  • 40
  • Not the solution you ask for, but the most expedient way is probably to just name your models as you want them labeled. You can use backticks for non-standard names (like `\`2SLS\` <- model_2`) – Gregor Thomas Dec 29 '15 at 19:05
  • Could you please clarify the syntax for this (renaming the models)? Not the backticks, but rather the way to modify the model name in the `lm()` object. Thanks! – Nicolas Dec 29 '15 at 23:23
  • Possible duplicate of [Stargazer: model names instead of numbers?](http://stackoverflow.com/questions/27671317/stargazer-model-names-instead-of-numbers) – scoa Dec 30 '15 at 08:03

1 Answers1

8

Stargazer optionally includes the object names, so if you models are

m1 = lm(mpg ~ wt, data = mtcars)
m2 = lm(mpg ~ wt + disp, data = mtcars)

You can do

stargazer(m1, m2, object.names = TRUE,
          column.labels = c("lab 1", "lab 2e"))

to get both custom labels and the object names, m1 and m2. This can be usefully abused by using non-standard names matching the extra model names that you want

OLS = m1
`2SLS` = m2
    stargazer(OLS, `2SLS`, object.names = TRUE,
              column.labels = c("lab 1", "lab 2e"))

Though, unfortunately, the backticks are included in the output. (As an additional hack you could capture.output() and remove them with gsub).

The model names used by stargazer are not part of the model object, rather stargazer examines the model object and attempts to extract them. You can see the .model.identify function on github. You could attempt to fixInNamespace to adjust this, but I think a post-hoc hack is easier.

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