Can MplusAutomation extract multinomial logistic regression coefficients following LCA (R3STEP)?

302 views
Skip to first unread message

Ewan

unread,
Mar 16, 2018, 2:44:14 PM3/16/18
to MplusAutomation
Does anyone know if MplusAutomation can extract coefficients from the multinomial logistic regressions carried out as part of a LCA model? (using the R3STEP options)

E.g. the output looks like this:

TESTS OF CATEGORICAL LATENT VARIABLE MULTINOMIAL LOGISTIC REGRESSIONS USING
THE
3-STEP PROCEDURE

                                                   
Two-Tailed
                   
Estimate       S.E.  Est./S.E.    P-Value

 
#1       ON
    AGE              
-0.099      0.039     -2.551      0.011
    FEMALE            
-0.364      0.178     -2.048      0.041

 
#2       ON
    AGE              
-0.135      0.047     -2.867      0.004
    FEMALE            
-0.346      0.198     -1.751      0.080

 
Intercepts
   
#1                 1.259      0.419      3.008      0.003
   
#2                 2.074      0.502      4.128      0.000

Parameterization using Reference Class 1

[...]

I couldn't find these numbers anywhere the output of readModels. If not, I can go ahead and extract them manually, but thought I'd check first.

Thanks as always, for creating such a useful package!

Ewan

Ray Sin

unread,
Jul 17, 2018, 3:30:22 PM7/17/18
to MplusAutomation
bump... anyone?

David Allsop

unread,
Apr 20, 2021, 4:59:24 PM4/20/21
to MplusAutomation
I would love if this was a feature!

Simon Brauer

unread,
Apr 21, 2021, 3:15:35 PM4/21/21
to MplusAutomation
I was just working on this issue last week. I couldn't find a way to do this within the package, so I wrote my own code to scrape it from the `.out` file. I've included my code below. It uses tidyverse (stringr, dplyr, readr, tidyr, purrr). The main function is `extract_r3step()` which takes the path to your output file. e.g.

    `extract_r3step("output/model1.out")`

I can't claim how generalizable my code is. It certainly isn't pretty. But hope it helps. You may have to edit the line that says "row2 = (1:n())[str_detect(x, 'CONFIDENCE INTERVALS OF MODEL RESULTS')" to whatever text follows the R3STEP section in your output.

```
parse_predictors <- function(x){
  x <- x %>%
    str_replace('^[\\s]+', '') %>%
    str_replace('[\\s]+$', '') %>%
    str_split('[\\s]+') %>%
    unlist()
  
  if(length(x) < 5){
    x <- c(x, rep('', times = 5 - length(x)))
  }
  
  tibble(colnames = c('predictor', 'estimate', 'se', 'tval', 'pval'),
         value = x) %>%
    spread(colnames, value) %>%
    select(predictor, estimate, se, tval, pval)
}

extract_r3step <- function(model_path){
  tibble(x = read_lines(model_path)) %>%
    mutate(row1 = (1:n())[str_detect(x, 'TESTS OF CATEGORICAL LATENT VARIABLE')],
           row2 = (1:n())[str_detect(x, 'CONFIDENCE INTERVALS OF MODEL RESULTS')]) %>%
    slice((.$row1[1] + 2):(.$row2[1] - 1)) %>%
    mutate(type_comparison = str_detect(x, 'Parameterization using Reference'),
           type_colname = str_detect(x, 'Estimate'),
           type_classnum = str_detect(x, 'C#[0-9]+'),
           type_predictor = str_detect(x, '[\\.\\-0-9]+[\\s]+[\\.\\-0-9]+')) %>%
    mutate(class = ifelse(type_classnum, parse_number(x), NA),
           predictors = ifelse(type_predictor, map(x, parse_predictors), list()),
           comparison_group = cumsum(type_comparison)) %>%
    group_by(comparison_group) %>%
    mutate(class_group = cumsum(type_classnum)) %>%
    ungroup() %>%
    mutate(comparison_group = ifelse(comparison_group == 0, max(class, na.rm = TRUE), comparison_group)) %>%
    group_by(comparison_group, class_group) %>%
    filter(class_group != 0) %>%
    mutate(class_group2 = class[!is.na(class)]) %>%
    ungroup() %>%
    filter(type_predictor) %>%
    select(comparison_group, class_group2, predictors) %>%
    unnest() %>%
    rename(comparison_class = comparison_group,
           class = class_group2) %>%
    mutate(estimate = as.numeric(estimate),
           se = as.numeric(se),
           tval = as.numeric(tval),
           pval = as.numeric(pval)) 
}
```

David Allsop

unread,
Apr 21, 2021, 4:34:54 PM4/21/21
to MplusAutomation
Awesome - thanks for sharing!
Reply all
Reply to author
Forward
0 new messages