Created
October 18, 2024 23:09
-
-
Save sientifiko/819a76814ee7de3490ab0000f8924770 to your computer and use it in GitHub Desktop.
Script para generar una visualización de las distribuciones de efectos condicionales a posterior a partir de un objeto de tipo estimateEffect (básicamente el que creas cuando haces la regresión)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(stm) | |
library(tidytext) | |
library(ggridges) | |
n_topicos = 20 | |
n_nuestras = 100 | |
# iteración sobre tópicos | |
lapply(1:n_topicos, function(i){ | |
# iteración sobre muestra | |
lapply(1:n_muestras, function(j){ | |
modeloEfectos_STM$parameters[[i]][[j]]["est"] %>% | |
unlist() | |
}) %>% | |
do.call("rbind", .) %>% | |
as.data.frame() -> muestra | |
muestra$topico <- i | |
muestra | |
}) %>% | |
do.call("rbind", .) %>% | |
as.data.frame() %>% | |
gather("variable", | |
"estimado", 1:(ncol(.)-1)) -> tempposterior | |
tempposterior %>% | |
ggplot() + | |
aes(x = estimado, y = reorder_within(as.factor(topico), | |
estimado, variable), | |
group = topico, | |
fill = variable) + | |
geom_density_ridges(scale = 1) + | |
facet_wrap(.~variable, scales = "free_y") + | |
theme_minimal(base_size = 21) + | |
scale_x_continuous(expand = c(0,0)) + | |
scale_y_reordered() + | |
theme(legend.position = "none", | |
axis.text.y = element_text(size = 10)) + | |
geom_vline(xintercept = 0) + | |
labs(x="Efecto", y="Tópicos") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Por si quedara alguna duda, modeloEfectos_STM se crea con una rutina del tipo
modeloEfectos_STM <- estimateEffect(topicos ~ covariables,
modelo_elegido, # esto lo sacan del selectModel
meta = out$meta,
uncertainty = "Global", # estos últimos 2, a gusto del consumidor
nsims = 100)