在垂直列中组织mlogit生成的结果

时间:2014-02-06 14:36:48

标签: r mlogit

我正在尝试组织使用mlogit获得的结果,以便导出到xtable的LaTeX。但是,我发现很难在相邻的专栏中准备结果,这在学术出版物中经常出现。

特别是,我在最后一步遇到问题,其中方程式需要彼此相邻移动。

我现在举一个小数据框的例子,到目前为止我已经走了多远。如果有更简单的方法,如果你让我知道,我会很高兴。

#--------------------------- 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

我需要帮助创建一个解决方案,该解决方案可以适应不同数量的方程(取决于结果变量有多少级别)和每个方程的不同长度(取决于预测变量的数量)。

1 个答案:

答案 0 :(得分:3)

这是一种方法:

# 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))

结果:

            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