I have a series of 33 fixed effects regressions. Each regression has its own unique dependent variable: the cost of different trade product types. Each trade product type has two regressions for two different key independent variables, STC_exp and STC_import.
lval28_exports_tradeonly <- felm(lval28 ~ STC_exp | pair + year | 0 | pair, subset(STC_Data, NoTrade28 == 1))
lval28_imports_tradeonly <- felm(lval28 ~ STC_imp | pair + year | 0 | pair, subset(STC_Data, NoTrade28 == 1))
lval29_exports_tradeonly <- felm(lval29 ~ STC_exp | pair + year | 0 | pair, subset(STC_Data, NoTrade29 == 1))
lval29_imports_tradeonly <- felm(lval29 ~ STC_imp | pair + year | 0 | pair, subset(STC_Data, NoTrade29 == 1))
What I want to do is create a coefficient plot so that the two independent variables for each of the dependent variables either share the same line on the coefficient plot or can be grouped together. I tried doing this with plot_summs
in jtools
package and some other packages, but I'm not having much success.
I can make each figure individually:
ore <- plot_summs(lval26_imports_tradeonly, lval26_exports_tradeonly, coefs = c("Ore" = "STC_exp", "Ore" = "STC_imp"), model.names = c("STC on importer", "STC on exporter"))
inorganic.chemicals <- plot_summs(lval28_imports_tradeonly, lval28_exports_tradeonly, coefs = c("Inorganic Chemicals" = "STC_exp", "Inorganic Chemicals" = "STC_imp"), model.names = c("STC on importer", "STC on exporter"))
But I would like to be able to combine them in some way. Perhaps the jtools package isn't the right away to go?
You could just make it with ggplot
directly.
Below, I make some example data that looks to have similar properties to yours. Without your data, I can't replicate your example directly.
library(tibble)
library(dplyr)
library(tidyr)
set.seed(25443)
dat <- tibble(STC_exp = runif(500, -3, 3),
STC_imp = runif(500, -3, 3))
b1 <- runif(33, .1, .5)
b2 <- runif(33, .1, .5)
for(i in 1:33){
dat[[paste0("lval", i)]] <- b1[i] * dat$STC_exp + b2[i] * dat$STC_imp + rnorm(500, 0, .25)
}
reshape the data to long-format on both STC_
variables and all of the lval
variables.
library(tidyr)
dat <- dat %>% pivot_longer(cols=c("STC_exp", "STC_imp"), names_to= "ie", values_to = "stc")
dat <- dat %>% pivot_longer(cols=starts_with("lval"), names_to="var", values_to = "lval")
Run the models and collect the output.
library(purrr)
library(broom)
mods <- dat %>%
group_by(ie, var) %>%
summarise(lm_mod= list(lm(lval ~ stc))) %>%
mutate(tidied = map(lm_mod,tidy,conf.int = TRUE)) %>%
unnest(tidied)
mods <- select(mods, -lm_mod) %>%
filter(term == "stc")
Make the plot
library(ggplot2)
mods %>%
mutate(ie = factor(ie, levels=c("STC_exp", "STC_imp"),
labels=c("STC on Exporter", "STC on Importer"))) %>%
ggplot(aes(x=estimate, y=var, colour=ie)) +
geom_point(position = position_dodge(width=.75)) +
geom_errorbarh(aes(xmin=conf.low, xmax=conf.high), position=position_dodge(width=.75), height=0) +
labs(x="Estimate", y="", colour="Model") +
theme_bw()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.