Custom model names in Stargazer package for R
Asked Answered
L

1

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?

Lexicology answered 29/12, 2015 at 18:25 Comment(3)
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)Parliamentarianism
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!Lexicology
Possible duplicate of Stargazer: model names instead of numbers?Monostome
P
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.

Parliamentarianism answered 29/12, 2015 at 23:47 Comment(1)
Thanks, I guess the capture.output() method is the way to go then!Lexicology

© 2022 - 2024 — McMap. All rights reserved.