Вопрос

I am trying to organize my results obtained with mlogit for exporting to LaTeX with xtable. However, I am finding it difficult to prepare the results in adjacent columns as often found in academic publications.

In particular, I am having problems in the last step, where equations need to be moved next to each other.

I am presenting an example with a small dataframe and how far I have gotten so far below. If there is an easier way to do this, I would be happy if you let me know.

#--------------------------- Create test data and run model --------------------#

id <- 1:12
color <- factor(rep(c("blue","red","yellow"), each=4))
value1 <- round(rnorm(12)*5,1)
value2 <- round(runif(12),1)
factor1 <- factor(rep(c("A", "B"), 6))
data_sample <- data.frame(id, color, value1, value2, factor1)

# Reshape data 
data_sample2 <- mlogit.data(data_sample, choice="color", shape="wide" )

# Run model 
mlogit.ds <- mlogit(color ~ 1 | value2 + value1 + factor1, data=data_sample2)
#summary(mlogit.ds)

# Save model summary 
mlogit.ds <- summary(mlogit.ds)

#-------------------------- Prepare table -------------------------------#

mlogit_table <- data.frame(mlogit.ds$CoefTable)
mlogit_table <- mlogit_table[c(1,4)] # to keep only estimates and p-values 
mlogit_table <- mlogit_table[order(rownames(mlogit_table)),] # to group all equations      together 
mlogit_table

                      Estimate  Pr...t..
  red:(intercept)     2.33034676 0.4653448
  red:factor1B        0.13591855 0.9506175
  red:value1          0.26639321 0.2072482
  red:value2         -5.64821495 0.1956896
  yellow:(intercept)  5.32776498 0.1372126
  yellow:factor1B    -3.30689681 0.2688475
  yellow:value1      -0.09929715 0.6394161
  yellow:value2      -7.28057244 0.1335184

#------------------------  Desired result ------------------------------#

                red         p      yellow         p
intercept -0.5522404 0.7597343  0.50745137 0.7349326
factor1B  -0.6573629 0.7289306 -0.08885928 0.9528689
value1    -0.4058873 0.1495544  0.05956548 0.7833022
value2     0.6370185 0.8398007 -1.30156671 0.6051921

I need help with creating a solution which could adapt to different numbers of equations (depending on how many levels the outcome variable has) and different lengths of each equation (depending on the numbers of predictors).

Это было полезно?

Решение

Here's one approach:

# extract data
tab <- summary(mlogit.ds)$CoefTable[, c(1, 4)]
# find values of outcome variable
ind <- sub("^(\\w+):.*", "\\1", rownames(tab))
# create table
mlogit_table <- do.call(cbind, split(as.data.frame(tab), ind))
# change row names
rownames(mlogit_table) <- sub("^(\\w+:)", "", rownames(mlogit_table))

The result:

            red.Estimate red.Pr(>|t|) yellow.Estimate yellow.Pr(>|t|)
(intercept)   -1.9697934    0.3301242      -4.4497945      0.19866621
value2         5.7087164    0.1550275       8.7793979      0.09833026
value1        -0.0838299    0.8377691      -0.4767750      0.29019742
factor1B      -0.3583036    0.8447884       0.1671317      0.94356618
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top