0

I am trying to manually generate lines of a ggplot to display confidence intervals. I am extracting data from multiple different models external to the ggplot, and then plotting the results within the ggplot manually. However, I have a great number of models and specific variables to select, so I would like to automate this process.

How can I use the lapply function (or another) to do so. For example, if I wanted to plot this:

Suppose variable1, variable2, variable3 are all from different models.

This would be the rough code to plot one confidence interval (for variable1)

plot <- ggplot(data=NULL, aes(x= c("variable1","variable2", "variable3"), y=c(-1,1))) + labs(y="Estimate", x="Model") + geom_segment(aes(x="variable1",xend="variable1",y= (Betahat_variable1-1.96*se_variable1), yend=(1.96*se_variable1+Betahat_variable1))) + coord_flip()

How can I use lapply (or another syntax) to repeat this for variable1, variable2, variable3, etc... without having to manually write out each betahat/se? I can extract those variables from the models using the same format (because the models are similar), but how can I insert these character strings within the ggplot code, rather than writing the digits out themselves? This would allow me to draw the plot much more efficiently.

Dummy data:

This is how I extract the metrics to build the confidence intervals. All the models are felm regressions with fixed effects and multiple controls.

The listed felm regressions are compiled within the object models, from which I extract variable1 (models[[1]]), variable2 (models[[2]]), etc.

df: 

Variable Group Outcome1 Outcome2 Outcome3
1        1      2          4      0
2        4      5          6      0
3        2      3          2      4
4        1      1          6      1


models <- paste0("outcome", 1:10, " ~ variable | group|0|group") |> lapply(\(x) felm(as.formula(x), data = df))




#variable1 coefficient
models[[1]]$coefficients[1]

#variable 1 p value
models[[1]]$pval

Betahat_variable1 <- coef(models[[1]])[1]
se_variable1 <- models[[1]]$se[1]

I unfortunately cannot provide original data, but all of the data extracted from the felm models are numerical, between -1, and 1, so the extraction process is similar across all of the different models/variables.

flâneur
  • 633
  • 2
  • 8
  • 4
    I am sure if you make a reproducible example we could help you here. Could you perhaps create some dummy data and code to produce some models so we can see your actual set-up? Thanks. – Allan Cameron Sep 01 '22 at 16:29
  • @AllanCameron Thanks so much! I unfortunately cannot add original data, but I updated with code to show how I extract the data that needs to feed into the ggplot. Really any number would work, my issue is just creating code where I can repeat this insertion process for every level of model (models[[1]], [[2]],[[3]], etc, so that I can simply go straight from the data stored in the felm models to the ggplot lines. Many thanks! – flâneur Sep 01 '22 at 16:57
  • 1
    The dummy data edit doesn't help because the code it still not reproducible. We can't copy/paste it to run and test. It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. We don't need your original data. Feel free to make some up for your question – MrFlick Sep 01 '22 at 17:32
  • @MrFlick I tried adding sample data now that shows how I created the felm models. I hope that helps. It really shouldn't matter though, I suppose my question is more about how to place any externally extracted object in a ggplot by formula. Thank you for the help! – flâneur Sep 01 '22 at 17:52

0 Answers0