在`mtable`输出中使用'mlm`对象

时间:2014-11-11 20:03:47

标签: r lm mlm

有没有办法使用mlm包中的mtable memisc个对象?

不使用多重响应矩阵,我想要的是:

library(car)
library(memisc)
lm1 = lm(Sepal.Length ~ Petal.Length + Petal.Width + Species, data=iris)
lm2 = lm(Sepal.Width ~ Petal.Length + Petal.Width + Species, data=iris)
mtable(lm1, lm2)

产生

Calls:
  lm1: lm(formula = Sepal.Length ~ Petal.Length + Petal.Width + Species, 
          data = iris)
lm2: lm(formula = Sepal.Width ~ Petal.Length + Petal.Width + Species, 
        data = iris)

===============================================
  lm1       lm2   
-----------------------------------------------
  (Intercept)                  3.683***  3.048***
  (0.107)   (0.094)  
Petal.Length                 0.906***  0.155*  
  (0.074)   (0.065)  
Petal.Width                 -0.006     0.623***
  (0.156)   (0.136)  
Species: versicolor/setosa  -1.598*** -1.764***
  (0.206)   (0.180)  
Species: virginica/setosa   -2.113*** -2.196***
  (0.304)   (0.265)  
-----------------------------------------------
  R-squared                      0.837     0.551 
adj. R-squared                 0.832     0.539 
sigma                          0.339     0.296 
F                            185.769    44.496 
p                              0.000     0.000 
Log-likelihood               -48.116   -27.711 
Deviance                      16.681    12.708 
AIC                          108.231    67.423 
BIC                          126.295    85.486 
N                            150       150     
===============================================

可是:

mlmIris = lm(cbind(Sepal.Length, Sepal.Width) ~ Petal.Length + Petal.Width + Species, data=iris)
mtable(mlmIris)

产生

Error in qt(p = alpha/2, df = dendf) : 
  Non-numeric argument to mathematical function

我不会重现我尝试提取lm对象的方法,我可以在mtable中使用它。我只想说,它们都没有用。

1 个答案:

答案 0 :(得分:1)

根据评论中的建议,您需要为getSummary个对象编写mlm方法,因为目前还没有。通过继承获得的lm方法不起作用。

我快速尝试了这一点并在下面提供getSummary.mlm以及合适的setSummaryTemplate。现在可以正确提取系数和标准误差。需要做更多工作的是提取总结统计数据(R平方,残差平方和,F统计等),这些都是我没有做过的。它应该会给你一个良好的开端。如果您进一步改进该方法,请考虑将其提供给Martin Elff(memisc维护者),以便它可以直接在memisc中使用。

在获得下面提供的功能后,这可行:

R> mtable(mlmIris)

Calls:
mlmIris: lm(formula = cbind(Sepal.Length, Sepal.Width) ~ Petal.Length + 
    Petal.Width + Species, data = iris)

=====================================================
                            Sepal.Length Sepal.Width 
-----------------------------------------------------
(Intercept)                   3.683***     3.048***  
                             (0.107)      (0.094)    
Petal.Length                  0.906***     0.155*    
                             (0.074)      (0.065)    
Petal.Width                  -0.006        0.623***  
                             (0.156)      (0.136)    
Species: versicolor/setosa   -1.598***    -1.764***  
                             (0.206)      (0.180)    
Species: virginica/setosa    -2.113***    -2.196***  
                             (0.304)      (0.265)    
-----------------------------------------------------
N                               150                  
=====================================================

源代码是:

getSummary.mlm <- function(obj, alpha = 0.05, ...)
{
## extract coefficient summary
cf <- lapply(summary(mlmIris), "[[", "coefficients")
## augment with confidence intervals
cval <- qnorm(1 - alpha/2)
for(i in seq_along(cf)) cf[[i]] <- cbind(cf[[i]],
  cf[[i]][, 1] - cval * cf[[i]][, 2],
  cf[[i]][, 1] + cval * cf[[i]][, 2])
## collect in array
nam <- unique(unlist(lapply(cf, rownames)))
acf <- array(dim = c(length(nam), 6, length(cf)),
  dimnames = list(nam, c("est", "se", "stat", "p", "lwr", "upr"), colnames(coef(obj))))
for(i in seq_along(cf)) acf[rownames(cf[[i]]), , i] <- cf[[i]]

## return everything
return(list(
  coef = acf,
  sumstat = c(
    "N" = nobs(obj)
  ),
  contrasts = obj$contrasts,
  xlevels = obj$xlevels,
  call = obj$call
))
}

setSummaryTemplate("mlm" = c(
  "N" = "($N:d)"
))