user14514023
user14514023

Reputation: 27

Sort variables in ggcoef_compare output

I am using ggcoef_compare in R to compare different models coefficients. However, the output displays the variables in a different order than that of the models. I tried to change it repeatedly but none of the solutions below work for me. Can anybody help? Thank you!

Using sort but I got this error:

Error in (function (data, x = "estimate", y = "label", exponentiate = FALSE, : unused argument (sort = "ascending")

I found this on Github: #temporary fix

d \<- ggcoef_compare(models = list(m1, m2, m3), return_data = TRUE)
d \<- d %\>% arrange(model, var_label)
ggcoef_plot(d, colour = "model", dodged = TRUE)

but I got this error in the second line:

Error in UseMethod("arrange") : no applicable method for 'arrange' applied to an object of class "c('gg', 'ggplot')"

In general, it seems that the problem is solved (see here: Github) but my code isn't working.

Here it is:

remotes::install_github("ggobi/ggally")
models <- list(
    "FDI model" = negb_FDI_autoc,
    "PD model" = negb_PD_autoc,
    "Full model" = negb2_autoc)
    ggcoef_compare(models, type = "faceted", variable_labels = c(
                                   share_women_PPG = "Share of women in the PPG",
                                   government = "Government",
                                   reproductive_mean = "GT: reproductive rights",
                                   violence_mean = "GT: violence against women",
                                   labour_mean = "GT: women in the labour market",
                                   `lag(n, 1)` = "N of obs on women’s issues (t–1)",
                                   woman_leader = "Woman leader",
                                   woman_PPGleader = "Woman leader of the PPG",
                                   party_dummy = "Party (FDI = 0)"),
               stripped_rows = FALSE) +
    labs(x = "Negative Binomial Regression Coefficients") +
    ggplot2:: theme(axis.title.x = element_text(margin = unit(c(5,0,0,0), "mm")))

But the output is the one you see in the picture.

Output image

Model 1 is run on this dataset (I show only a part):

structure(list(party = c("FDI", "FDI", "FDI", "FDI", "FDI", "FDI", 
"FDI", "FDI", "FDI", "FDI", "FDI", "FDI", "FDI", "FDI", "FDI", 
"FDI", "FDI", "FDI", "FDI", "FDI", "FDI", "FDI", "FDI", "FDI", 
"FDI"), month = structure(c(15765, 15796, 15826, 15857, 15887, 
15918, 15949, 15979, 16010, 16040, 16071, 16102, 16130, 16161, 
16191, 16222, 16252, 16283, 16314, 16344, 16375, 16405, 16436, 
16467, 16495), class = "Date"), n = c(0, 0, 3, 4, 1, 0, 0, 0, 
2, 0, 0, 3, 0, 1, 2, 3, 6, 1, 0, 3, 1, 0, 0, 1, 0), perChange = c(NA, 
0, 0, 33.3333333333333, -75, -100, 0, 0, 0, -100, 0, 0, -100, 
0, 100, 50, 100, -83.3333333333333, -100, 0, -66.6666666666667, 
-100, 0, 0, -100), `legislative term` = c(17, 17, 17, 17, 17, 
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
17, 17, 17, 17), share_women_PPG = c(23.1, 23.1, 23.1, 23.1, 
23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 
23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1, 23.1), 
    woman_leader = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), government = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0), woman_PPGleader = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), reproductive_mean = c(35.6, 
    34.8, 36.7, 34, 36.4, 33.5, 35.6, 32.7, 34.9, 33.3, 36.1, 
    34, 36.9, 39.5, 38.8, 28.8, 33.4, 35.6, 35, 31.5, 32.6, 27.1, 
    35.3, 40.9, 36), violence_mean = c(19.64, 17.43, 20.79, 20.21, 
    17.21, 17.57, 17.79, 22.36, 22.93, 17.79, 16.64, 18.86, 21.07, 
    16.93, 18.29, 15, 15.5, 13.29, 16.79, 16.57, 23.93, 17.21, 
    16.79, 16.21, 16.36), labour_mean = c(15.25, 12.63, 10.25, 
    10, 11.63, 9.25, 12.5, 7, 10.25, 9.25, 12.5, 9.75, 34.5, 
    8.38, 14.5, 11.38, 11.25, 3.88, 9.63, 4.5, 5.38, 8.25, 9, 
    8, 12.88), PD_Schlein = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 
-25L), class = c("tbl_df", "tbl", "data.frame"))

Model 2 is run on this dataset (I show only a part):

structure(list(party = c("PD", "PD", "PD", "PD", "PD", "PD", 
"PD", "PD", "PD", "PD", "PD", "PD", "PD", "PD", "PD", "PD", "PD", 
"PD", "PD", "PD", "PD", "PD", "PD", "PD", "PD"), month = structure(c(15765, 
15796, 15826, 15857, 15887, 15918, 15949, 15979, 16010, 16040, 
16071, 16102, 16130, 16161, 16191, 16222, 16252, 16283, 16314, 
16344, 16375, 16405, 16436, 16467, 16495), class = "Date"), n = c(13, 
3, 26, 16, 15, 13, 5, 27, 10, 14, 8, 3, 18, 16, 16, 8, 14, 1, 
4, 18, 19, 10, 2, 4, 7), perChange = c(NA, -76.9230769230769, 
766.666666666667, -38.4615384615385, -6.25, -13.3333333333333, 
-61.5384615384615, 440, -62.962962962963, 40, -42.8571428571429, 
-62.5, 500, -11.1111111111111, 0, -50, 75, -92.8571428571429, 
300, 350, 5.55555555555556, -47.3684210526316, -80, 100, 75), 
    `legislative term` = c(17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
    17), share_women_PPG = c(37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 
    37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 
    37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 37.2, 37.2), woman_leader = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), government = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), woman_PPGleader = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), reproductive_mean = c(35.6, 34.8, 36.7, 34, 
    36.4, 33.5, 35.6, 32.7, 34.9, 33.3, 36.1, 34, 36.9, 39.5, 
    38.8, 28.8, 33.4, 35.6, 35, 31.5, 32.6, 27.1, 35.3, 40.9, 
    36), violence_mean = c(19.64, 17.43, 20.79, 20.21, 17.21, 
    17.57, 17.79, 22.36, 22.93, 17.79, 16.64, 18.86, 21.07, 16.93, 
    18.29, 15, 15.5, 13.29, 16.79, 16.57, 23.93, 17.21, 16.79, 
    16.21, 16.36), labour_mean = c(15.25, 12.63, 10.25, 10, 11.63, 
    9.25, 12.5, 7, 10.25, 9.25, 12.5, 9.75, 34.5, 8.38, 14.5, 
    11.38, 11.25, 3.88, 9.63, 4.5, 5.38, 8.25, 9, 8, 12.88), 
    FDI_gov = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -25L), class = c("tbl_df", 
"tbl", "data.frame"))

Model 3 is based on a dataset which is a simple full_join of the two. For the three models I run a glm.nb.

Upvotes: 0

Views: 70

Answers (1)

David
David

Reputation: 610

Thank you very much for providing your data! The reason why the github solution didn't work for you is because d in that example with the swiss data set had a d$model term (from lm) and so arrange could be used.

Important note: to view the plotting data set such that one may get access to the model and var_label, one needs to use the return_data = TRUE flag. They did this in the github example.

With the glm.nb, you may also access the model and var_label terms using the return_data = TRUE flag. This will create a data set instead of creating a plot. When you view this data set, the row/column ordering will be the order in which it will plot in the facets. I would suggest sorting/arranging on this facet data set explicitly to get the order that you would like.

I was able to recreate a small subset of your plot using a set of crude neg binomial models with glm.nb and the data that you kindly provided. Using the return_data = TRUE flag, I could modify the ordering of the data to be plotted. So I think this should work for you.

Unfortunately, you then have to use ggcoef_plot() to plot your newly-arranged data set. This will likely require some work to get it how you like it. I don't know enough about ggcoef_plot to make the plot as pretty as your original one.

Hope this helps!

Upvotes: 1

Related Questions