`nnet::multinom` with wide format data
vincentarelbundock opened this issue · 0 comments
vincentarelbundock commented
The nnet::multinom
function supports two different data formats: wide and long. The two approaches yield exactly identical estimates. However, the parameters
package does not support the wide format:
library(nnet)
library(faraway)
data("cns", package = "faraway")
Long format works as expected
cns2 <- reshape(cns, direction = "long", timevar = "Type",
times = names(cns)[3:5], varying = 3:5, v.names = "Freq")[, 3:6]
cns2$Type <- factor(cns2$Type, levels = unique(cns2$Type))
m2 <- multinom(Type ~ Water + Work, data = cns2, weights = Freq, trace = FALSE)
parameters::parameters(m2)
# # Response level: sp
#
# Parameter | Log-Odds | SE | 95% CI | t(42) | p
# -----------------------------------------------------------------------
# (Intercept) | 0.38 | 0.19 | [-0.01, 0.76] | 1.97 | 0.055
# Water | -1.30e-03 | 2.03e-03 | [-0.01, 0.00] | -0.64 | 0.527
# Work [NonManual] | 0.12 | 0.21 | [-0.31, 0.54] | 0.55 | 0.582
#
# # Response level: other
#
# Parameter | Log-Odds | SE | 95% CI | t(42) | p
# ------------------------------------------------------------------------
# (Intercept) | -1.12 | 0.28 | [-1.69, -0.56] | -4.02 | < .001
# Water | 2.18e-03 | 2.90e-03 | [ 0.00, 0.01] | 0.75 | 0.455
# Work [NonManual] | -0.27 | 0.32 | [-0.93, 0.39] | -0.83 | 0.410
Wide format does not display properly (merge issue)
m1 <- multinom(cbind(An, Sp, Other) ~ Water + Work, data = cns, trace = FALSE)
parameters::parameters(m1)
# Warning in merge.data.frame(parameters, std_err, by = merge_by, sort = FALSE):
# column names 'Response.x', 'Response.y' are duplicated in the result
# Warning in merge.data.frame(parameters, statistic, by = merge_by, sort =
# FALSE): column names 'Response.x', 'Response.y' are duplicated in the result
#
# Parameter | Log-Odds | SE | 95% CI | Response | t(10) | p | Response.1
# -----------------------------------------------------------------------------------------------
# (Intercept) | 0.38 | 0.19 | [-0.05, 0.80] | Sp | 1.97 | 0.077 | Sp
# Water | -1.30e-03 | 2.03e-03 | [-0.01, 0.00] | Sp | -0.64 | 0.538 | Sp
# Work [NonManual] | 0.12 | 0.21 | [-0.35, 0.58] | Sp | 0.55 | 0.591 | Sp
# (Intercept) | -1.12 | 0.19 | [-0.05, 0.80] | Sp | 1.97 | 0.077 | Sp
# Water | 2.18e-03 | 2.03e-03 | [-0.01, 0.00] | Sp | -0.64 | 0.538 | Sp
# WorkNonManual | -0.27 | 0.21 | [-0.35, 0.58] | Sp | 0.55 | 0.591 | Sp
Note that the two models are exactly equivalent:
coef(m1)
# (Intercept) Water WorkNonManual
# Sp 0.3752018 -0.001297048 0.1157557
# Other -1.1225496 0.002182689 -0.2702791
coef(m2)
# (Intercept) Water WorkNonManual
# Sp 0.3752018 -0.001297048 0.1157557
# Other -1.1225496 0.002182689 -0.2702791