How to export margins of odered logit model from R to latex?

241 Views Asked by At

I am running an ordered logit model using the package: oglmx

After running the regression, I calculated the margins using: oglmx.margins. However, I cannot export this output to latex or word or excel. I have tried stargazer, export_summs, and broom. All these methods failed to work.

The codes that I have used.

b19_ologit_model_PCM<-oglmx(ABoards_decile ~ Caste_SC_ST+ten_decile+Annual_inc+Sex_Female, data=b19_PCM_nob,link="logit",
                    constantMEAN = FALSE, constantSD = FALSE,
                    delta=0,threshparam = NULL)
summary(b19_ologit_model_PCM)
b19_ologit_margins_PCM<-margins.oglmx(b19_ologit_model_PCM,ascontinuous = TRUE)

tidy(b19_ologit_margins_PCM)
Error: No tidy method for objects of class margins.oglmx

export_summs(b19_ologit_margins_PCM)
Error: No tidy method for objects of class margins.oglmx

stargazer(b19_ologit_margins_PCM,type = 'text', se=list(NULL))

% Error: Unrecognized object type.

Example of data set:

structure(list(b19_PCM_nob.Candidate_name_10th = structure(c(60L, 
20L, 121L, 25L, 122L, 29L, 10L, 73L, 11L, 93L, 68L, 128L, 126L, 
51L, 2L, 35L, 46L, 34L, 30L, 21L), .Label = c("A AZIZUDDIN", 
"AARCHEE AMRIT NEWARE", "AARUSH SHUKLA", "AAYUSH SHUKLA", "AAYUSH TETE", 
"AAYUSHI AGRAWAL", "ABHIJAY RATHOR", "ABHISHEK BURMAN", "ABHISHEK CHANDRAKAR", 
"ABHISHRI JOSHI", "ADARSH SINGH", "ADHIKARLA SHRAVANI", "ADITI JHA", 
"ADITI SAO", "ADITI TIWARI", "ADITYA RANJAN SINGH", "AIYUSHI GHOSH", 
"AMAN KUMAR SAHU", "AMAN KUMAR SINGH", "AMAN SINGH", "AMRITA PADHI", 
"ANANAYE DWIVEDI", "ANIKET VERMA", "ANJALI JAGNE", "ANUBHAV PRATAP SINGH", 
"ARINDAM SIL", "AVINASH PATEL", "AYAAN AHMED", "AYUSH BISHT", 
"AYUSH KUMAR YADAV", "B KIRTI", "CHETNA TIKARIHA", "CHIRAG SHRIVASTAVA", 
"CHITRA SARKAR", "D VAMSIDHAR", "DEEPANSHU PAWAR", "DEVENDRA KUMAR SAHU", 
"DEVESH SHUKLA", "DHARMESH JAIN", "DIKSHA DEWANGAN", "HARLEEN KAUR", 
"HARSH CHAKRABORTY", "HARSH DIWAN", "HARSH SAHU", "HARSHVARDHAN SINGH RAJPUT", 
"HEMANG VERMA", "HIMANSHU KUMAR", "HRIDAM PALWE", "JAY PATEL", 
"JAYANT S ULLAS", "JITENDRA KUMAR SINGH", "K RITIKA", "KHUSHI SAHU", 
"KHUSHI SONI", "KHYATI YADAV", "KRISHNA AGRAWAL", "KUNAL RAJURKAR", 
"KUNAL SINGH", "LAKSHYA VASWANI", "MANAS GUPTA", "MAYANK GUPTA", 
"MD MAROOF HASAN KHAN", "MOHAMMED AYAAN", "MOKSHA CHANDRAKAR", 
"MORESHWAR SAHU", "MRUNAL WALIMBE", "MUSKAN NAG", "NAIVEDYA SONI", 
"NAMAN DAVE", "NEHA ANN JACOB", "NIDHI CHANDRA", "NIKITA SAHU", 
"NUPUR CHANDRAKAR", "P V S CHIRAG", "PALAK RATHI", "PANKAJ KUMAR SAHU", 
"PIYUSH SAHU", "POORNIMA MISHRA", "PRACHI LOKHANDE", "PRAGYA SRIVASTAVA", 
"PRIYAM MISHRA", "PRIYANKA TRIPATHY", "PRIYANSHU CHANDRAKAR", 
"PRIYANSHU SINGH", "PRIYANSHU THAKUR", "PULKIT JAIN", "PUSHPRAJ GENDRE", 
"R HARSH SAI KUMAR", "R VAISHNAVI SATVIKA", "RACHIT RATHI", "RAMAN DESHPANDE", 
"REMAN KUMAR LAHARE", "RICHA PANDEY", "RIYA DHOL", "ROHAN SARNA", 
"ROHIT JAISWAL", "RUPALI AGRAWAL", "SAURABH KUSHWAHA", "SHADAB ABDUL LATIF SHEIKH", 
"SHAMBHAVI DWIVEDI", "SHARMIN ANSARI", "SHASHANK PILLAI", "SHRADDHA SINGH", 
"SHRAJAN PANDEY", "SHREYA DEWANGAN", "SHRISTI SAHU", "SHRISTI TIWARI", 
"SHUBHAM KR PATEL", "SHUBHASHISH BANERJEE", "SHUBHIKA SOLANKI", 
"SNEHA GUPTA", "SRISHTI SHUKLA", "SUBHASHIS SAMAL", "SUMEET RANJAN MISHRA", 
"SUVIGYA PANDEY", "SUYASH PATEL", "SWANAND SANGWAI", "TRISHA GUPTA", 
"TUSHAR BHARTI", "TUSHAR JASWANI", "UJJWAL JAIN", "UJJWAL MITTAL", 
"UTKARSH NASHINE", "VAIBHAV TEMBHURKAR", "VIPUL JAIN", "VIRENDRA KUMAR SINGH", 
"VIVEK KUMAR PRAJAPATI", "VRUSHALI ASODKAR", "YACHANA CHOUDHARY", 
"YASHASHVI MISHRA", "YASMEEN MAHILANG", "YATHARTH SINGH"), class = "factor"), 
    b19_PCM_nob.Sex_Female = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
    0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L), b19_PCM_nob.Caste_SC_ST = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 1L, 0L, 0L), b19_PCM_nob.ten_decile = c(8L, 1L, 1L, 1L, 
    8L, 5L, 2L, 4L, 7L, 9L, 4L, 9L, 9L, 9L, 7L, 7L, 5L, 2L, 5L, 
    5L), b19_PCM_nob.ABoards_decile = c(9L, 1L, 6L, 2L, 10L, 
    9L, 4L, 3L, 10L, 10L, 3L, 8L, 8L, 6L, 1L, 8L, 3L, 6L, 3L, 
    4L)), row.names = c(NA, 20L), class = "data.frame")
1

There are 1 best solutions below

0
On

Unfortunately, the oglmx does not appear to be supported by any of the main table-making softwares, or by the two main “extractor” packages (broom and parameters). Also, please note that the stargazer package does not appear to be updated anymore, so it is unlikely to support these models in the near future.

Thankfully, it is quite easy to create tables for such models using the modelsummary package (disclaimer: I am the author). To achieve this, all you need to do is create a function called tidy.margins.oglmx() which accepts a model object and returns a data frame with a term column to represet each parameter, and a group column to represent levels of the outcome variable. See here for detailed instructions:

https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#adding-new-models-part-i

Note that changing the output argument of the modelsummary function will allow you to export to LaTeX, HTML, Word, and more.

Here's an example with a somewhat modified model since your example was not directly replicable (missing variable):

library(oglmx)
library(modelsummary)

dat <- structure(list(b19_PCM_nob.Candidate_name_10th = structure(c(60L, 
20L, 121L, 25L, 122L, 29L, 10L, 73L, 11L, 93L, 68L, 128L, 126L, 
51L, 2L, 35L, 46L, 34L, 30L, 21L), .Label = c("A AZIZUDDIN", 
"AARCHEE AMRIT NEWARE", "AARUSH SHUKLA", "AAYUSH SHUKLA", "AAYUSH TETE", 
"AAYUSHI AGRAWAL", "ABHIJAY RATHOR", "ABHISHEK BURMAN", "ABHISHEK CHANDRAKAR", 
"ABHISHRI JOSHI", "ADARSH SINGH", "ADHIKARLA SHRAVANI", "ADITI JHA", 
"ADITI SAO", "ADITI TIWARI", "ADITYA RANJAN SINGH", "AIYUSHI GHOSH", 
"AMAN KUMAR SAHU", "AMAN KUMAR SINGH", "AMAN SINGH", "AMRITA PADHI", 
"ANANAYE DWIVEDI", "ANIKET VERMA", "ANJALI JAGNE", "ANUBHAV PRATAP SINGH", 
"ARINDAM SIL", "AVINASH PATEL", "AYAAN AHMED", "AYUSH BISHT", 
"AYUSH KUMAR YADAV", "B KIRTI", "CHETNA TIKARIHA", "CHIRAG SHRIVASTAVA", 
"CHITRA SARKAR", "D VAMSIDHAR", "DEEPANSHU PAWAR", "DEVENDRA KUMAR SAHU", 
"DEVESH SHUKLA", "DHARMESH JAIN", "DIKSHA DEWANGAN", "HARLEEN KAUR", 
"HARSH CHAKRABORTY", "HARSH DIWAN", "HARSH SAHU", "HARSHVARDHAN SINGH RAJPUT", 
"HEMANG VERMA", "HIMANSHU KUMAR", "HRIDAM PALWE", "JAY PATEL", 
"JAYANT S ULLAS", "JITENDRA KUMAR SINGH", "K RITIKA", "KHUSHI SAHU", 
"KHUSHI SONI", "KHYATI YADAV", "KRISHNA AGRAWAL", "KUNAL RAJURKAR", 
"KUNAL SINGH", "LAKSHYA VASWANI", "MANAS GUPTA", "MAYANK GUPTA", 
"MD MAROOF HASAN KHAN", "MOHAMMED AYAAN", "MOKSHA CHANDRAKAR", 
"MORESHWAR SAHU", "MRUNAL WALIMBE", "MUSKAN NAG", "NAIVEDYA SONI", 
"NAMAN DAVE", "NEHA ANN JACOB", "NIDHI CHANDRA", "NIKITA SAHU", 
"NUPUR CHANDRAKAR", "P V S CHIRAG", "PALAK RATHI", "PANKAJ KUMAR SAHU", 
"PIYUSH SAHU", "POORNIMA MISHRA", "PRACHI LOKHANDE", "PRAGYA SRIVASTAVA", 
"PRIYAM MISHRA", "PRIYANKA TRIPATHY", "PRIYANSHU CHANDRAKAR", 
"PRIYANSHU SINGH", "PRIYANSHU THAKUR", "PULKIT JAIN", "PUSHPRAJ GENDRE", 
"R HARSH SAI KUMAR", "R VAISHNAVI SATVIKA", "RACHIT RATHI", "RAMAN DESHPANDE", 
"REMAN KUMAR LAHARE", "RICHA PANDEY", "RIYA DHOL", "ROHAN SARNA", 
"ROHIT JAISWAL", "RUPALI AGRAWAL", "SAURABH KUSHWAHA", "SHADAB ABDUL LATIF SHEIKH", 
"SHAMBHAVI DWIVEDI", "SHARMIN ANSARI", "SHASHANK PILLAI", "SHRADDHA SINGH", 
"SHRAJAN PANDEY", "SHREYA DEWANGAN", "SHRISTI SAHU", "SHRISTI TIWARI", 
"SHUBHAM KR PATEL", "SHUBHASHISH BANERJEE", "SHUBHIKA SOLANKI", 
"SNEHA GUPTA", "SRISHTI SHUKLA", "SUBHASHIS SAMAL", "SUMEET RANJAN MISHRA", 
"SUVIGYA PANDEY", "SUYASH PATEL", "SWANAND SANGWAI", "TRISHA GUPTA", 
"TUSHAR BHARTI", "TUSHAR JASWANI", "UJJWAL JAIN", "UJJWAL MITTAL", 
"UTKARSH NASHINE", "VAIBHAV TEMBHURKAR", "VIPUL JAIN", "VIRENDRA KUMAR SINGH", 
"VIVEK KUMAR PRAJAPATI", "VRUSHALI ASODKAR", "YACHANA CHOUDHARY", 
"YASHASHVI MISHRA", "YASMEEN MAHILANG", "YATHARTH SINGH"), class = "factor"), 
    b19_PCM_nob.Sex_Female = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
    0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L), b19_PCM_nob.Caste_SC_ST = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 1L, 0L, 0L), b19_PCM_nob.ten_decile = c(8L, 1L, 1L, 1L, 
    8L, 5L, 2L, 4L, 7L, 9L, 4L, 9L, 9L, 9L, 7L, 7L, 5L, 2L, 5L, 
    5L), b19_PCM_nob.ABoards_decile = c(9L, 1L, 6L, 2L, 10L, 
    9L, 4L, 3L, 10L, 10L, 3L, 8L, 8L, 6L, 1L, 8L, 3L, 6L, 3L, 
    4L)), row.names = c(NA, 20L), class = "data.frame")
colnames(dat) <- gsub(".*nob\\.", "", colnames(dat))
mod <- oglmx(ABoards_decile ~ Caste_SC_ST + ten_decile + Sex_Female,
    data = dat, link = "logit",
    constantMEAN = FALSE, constantSD = FALSE,
    delta = 0, threshparam = NULL)
mar <- margins.oglmx(mod)

tidy.margins.oglmx <- function(x, ...) {
    # add term names
    for (n in names(x)) {
        x[[n]] <- as.data.frame(x[[n]])
        x[[n]][["group"]] <- n
        x[[n]][["term"]] <- row.names(x[[n]])
        x[[n]] <- setNames(x[[n]], c("estimate", "std.error", "statistic", "p.value", "group", "term"))
    }
    x <- do.call("rbind", x)
    return(x)
}

modelsummary(mar, group = group + term ~ model)
Model 1
1 Caste_SC_ST -0.051
(0.041)
Sex_Female 0.057
(0.076)
ten_decile -0.025
(0.018)
2 Caste_SC_ST -0.035
(0.036)
Sex_Female 0.035
(0.048)
ten_decile -0.016
(0.016)
3 Caste_SC_ST -0.193
(0.102)
Sex_Female 0.128
(0.123)
ten_decile -0.070
(0.038)
4 Caste_SC_ST -0.124
(0.095)
Sex_Female 0.031
(0.041)
ten_decile -0.025
(0.025)
6 Caste_SC_ST -0.186
(0.154)
Sex_Female -0.040
(0.076)
ten_decile 0.010
(0.033)
8 Caste_SC_ST -0.027
(0.155)
Sex_Female -0.089
(0.092)
ten_decile 0.049
(0.034)
9 Caste_SC_ST 0.089
(0.126)
Sex_Female -0.053
(0.056)
ten_decile 0.032
(0.023)
10 Caste_SC_ST 0.525
(0.435)
Sex_Female -0.069
(0.066)
ten_decile 0.043
(0.024)