Sharif Amlani
Sharif Amlani

Reputation: 1288

Omit the constant and shift values into the correct location in stargazer in R

I want to input the average marginal effects into a stargazer table in R.

The problem is that stargazer is treating the first marginal effects value as the constant in the table. The average marginal effects that I'm calculating do not include a constant. Therefore, I want to omit it from the table. Using the omit argument in stargazer just excludes the term altogether. Because it treats the first marginal effects value as the constant, it actually drops a coefficient.

How can I get stargazer to omit the constant while getting it to recognize the first value is actually a coefficient, not the constant?

Here is an example of the problem:

#devtools::install_github("vincentarelbundock/marginaleffects")
library(marginaleffects)
library(stargazer)

Y <- sample(c(0,1), 1000, replace = T)
X <- sample(seq(1,50,1), 1000, replace = T)
Z <- sample(seq(1,50,1), 1000, replace = T)

DF <- data.frame(Y,X, Z)
Test <- glm(Y ~ X + Z, data = DF, family=binomial())
Test_DF <- data.frame(summary(marginaleffects(Test)))

Test_DF

#********************* Table **********************
#Fake Regression - to Make Table
Fake_RE <- glm(Y ~ X + Z, data = DF, family=binomial())

stargazer(list(Fake_RE), type = "text",
          coef = list(Test_DF$estimate),
          se = list(Test_DF$std.error))

Average Marginal Effects:

> Test_DF
      type term     estimate   std.error statistic   p.value     conf.low    conf.high
1 response    X  0.001540192 0.001109148  1.388627 0.1649462 -0.000633697 0.0037140816
2 response    Z -0.001215714 0.001087828 -1.117561 0.2637546 -0.003347817 0.000916389

Subsequent Table:

> stargazer(list(Fake_RE), type = "text",
+           coef = list(Test_DF$estimate),
+           se = list(Test_DF$std.error))

=============================================
                      Dependent variable:    
                  ---------------------------
                               Y             
---------------------------------------------
X                           -0.001           
                            (0.001)          
                                             
Z                                            
                                             
                                             
Constant                     0.002           
                            (0.001)          
                                             
---------------------------------------------
Observations                 1,000           
Log Likelihood             -690.097          
Akaike Inf. Crit.          1,386.194         
=============================================
Note:             *p<0.1; **p<0.05; ***p<0.01

Upvotes: 0

Views: 178

Answers (1)

Vincent
Vincent

Reputation: 17785

The stargazer package does not officially support objects of this type, and since that package has not been updated since 2018, I do not think it is reasonable to expect support any time soon.

If you are willing to consider an alternative package, you may want to try the modelsummary package (disclaimer: I am the author):

#devtools::install_github("vincentarelbundock/marginaleffects")
library(marginaleffects)
library(modelsummary)

Y <- sample(c(0,1), 1000, replace = T)
X <- sample(seq(1,50,1), 1000, replace = T)
Z <- sample(seq(1,50,1), 1000, replace = T)

DF <- data.frame(Y,X, Z)
Test <- glm(Y ~ X + Z, data = DF, family=binomial())
mfx <- marginaleffects(Test)

modelsummary(mfx)
Model 1
X 0.000
(0.001)
Z 0.002
(0.001)
Num.Obs. 1000
AIC 1389.3
BIC 1404.0
Log.Lik. -691.653

Upvotes: 1

Related Questions