Sharif Amlani
Sharif Amlani

Reputation: 1288

Input Covariates From Data Frame With Regression Results into Stargazer

I have a regression output in the form of a dataset. How can I input the estimates and standard errors into stargazer manually? Such that, stargazer creates its typical regression table?

                               term       estimate      std.error statistic      p.value
1                               rho  0.56782511824 0.016618530837 34.168190 0.000000e+00
2                       (Intercept) -4.10698330735 0.537699847356 -7.638059 2.198242e-14
4                 Unemployment_Rate  0.02288489900 0.016412419393  1.394365 1.632075e-01
5                         pop_sq_mi  0.00020135202 0.000045361286  4.438852 9.044016e-06
6                           prcntHS  0.13303000437 0.006002571434 22.162169 0.000000e+00
7                           prcntBA  0.03698563228 0.012723399878  2.906899 3.650316e-03
8                        prcntBlack  0.00877367484 0.004458885465  1.967683 4.910448e-02
9                        prcntMulti  0.01404154066 0.004182210799  3.357445 7.866653e-04
10                        prcntHisp  0.04316697336 0.003523552546 12.250980 0.000000e+00
11                 prcntForeignBorn  0.02229836451 0.009707563865  2.297009 2.161824e-02
12                     medianIncome -0.00002809549 0.000002933667 -9.576917 0.000000e+00
13                     per_gop_2016 -0.02366390363 0.002698813668 -8.768261 0.000000e+00

I have tried to use the following method (as an example) without much luck.

X1 <- sample(seq(1,100,1), 100,replace= T)
X2 <- sample(seq(1,100,1), 100,replace= T)
Y <- sample(seq(1,100,1), 100,replace= T)

df <- data.frame(Y, X1, X2)

Results <- lm(Y ~ X1 + X2, data = df)

library(broom)
Results_DF <- data.frame(tidy(Results))

stargazer(type = "text", 
          coef = list(Results_DF$estimate, Results_DF$estimate),
          se = list(Results_DF$std.error, Results_DF$std.error),
          omit.table.layout = "s")

Error in if (substr(inside[i], 1, nchar("list(")) == "list(") { : 
  missing value where TRUE/FALSE needed

Any advice would be greatly appreciated. Thank You!

Upvotes: 1

Views: 384

Answers (1)

desval
desval

Reputation: 2435

You are almost there.

Here you find a reproducible example. It should be possible to modify it so that it works with your data. Be careful with the t and p values. Check out the p.auto option in stargazer. Of course, you need to change manually or delete the regression footer containing observations, F-stat etc.

library(stargazer)

# coefficients data
d_lm <- data.frame(var = letters[1:4],
                   est = runif(4),
                   sd = runif(4),
                   t = runif(4),
                   p = runif(4))

# fake data
d <- data.frame(y = runif(30),
                a = runif(30),
                b = runif(30),
                c = runif(30),
                d = runif(30))

# fake regression
lm <- lm(y ~ a + b + c + d -1, d)

stargazer(lm,
          coef = list(d_lm$est),
          se = list(d_lm$sd),
          t = list(d_lm$t), # if not supplied stargazer will calculate t values for you
          p = list(d_lm$p), # if not supplied stargazer will calculate p values for you
          type = "text")

Upvotes: 2

Related Questions