从car软件包的Anova或Manova函数输出中提取多元检验

从car软件包的Anova或Manova函数输出中提取多元检验,r,statistics,anova,multivariate-testing,manova,R,Statistics,Anova,Multivariate Testing,Manova,我想知道如何从以下MWE中fm1的输出中提取多变量测试:Site部分 library(car) fm1 <- summary(Anova(lm(cbind(Al, Fe, Mg, Ca, Na) ~ Site, data=Pottery))) fm1 Type II MANOVA Tests: Sum of squares and products for error: Al Fe Mg Ca

我想知道如何从以下MWE中fm1的输出中提取
多变量测试:Site
部分

library(car)
fm1 <- summary(Anova(lm(cbind(Al, Fe, Mg, Ca, Na) ~ Site, data=Pottery)))
fm1

Type II MANOVA Tests:

Sum of squares and products for error:
           Al          Fe          Mg          Ca         Na
Al 48.2881429  7.08007143  0.60801429  0.10647143 0.58895714
Fe  7.0800714 10.95084571  0.52705714 -0.15519429 0.06675857
Mg  0.6080143  0.52705714 15.42961143  0.43537714 0.02761571
Ca  0.1064714 -0.15519429  0.43537714  0.05148571 0.01007857
Na  0.5889571  0.06675857  0.02761571  0.01007857 0.19929286

------------------------------------------

Term: Site 

Sum of squares and products for the hypothesis:
            Al          Fe          Mg         Ca         Na
Al  175.610319 -149.295533 -130.809707 -5.8891637 -5.3722648
Fe -149.295533  134.221616  117.745035  4.8217866  5.3259491
Mg -130.809707  117.745035  103.350527  4.2091613  4.7105458
Ca   -5.889164    4.821787    4.209161  0.2047027  0.1547830
Na   -5.372265    5.325949    4.710546  0.1547830  0.2582456

Multivariate Tests: Site
                 Df test stat  approx F num Df   den Df     Pr(>F)    
Pillai            3   1.55394   4.29839     15 60.00000 2.4129e-05 ***
Wilks             3   0.01230  13.08854     15 50.09147 1.8404e-12 ***
Hotelling-Lawley  3  35.43875  39.37639     15 50.00000 < 2.22e-16 ***
Roy               3  34.16111 136.64446      5 20.00000 9.4435e-15 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
库(车)
fm1(F)
Pillai 3 1.55394.29839 15 60.000002.4129e-05***
威尔克斯30.01230 13.08854 15 50.09147 1.8404e-12***
霍特林·劳利335.43875 39.37639 1550.00000<2.22e-16***
罗伊3 34.16111 136.64446 5 20.000009.4435e-15***
---
签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1

fm1$multivariable.tests
让您进入
fm1
输出的
站点
部分

然后,您可以将
cat
capture.output
组合使用,以实现良好的打印效果,或者仅将
capture.output
用于字符向量

> cat(capture.output(fm1$multivariate.tests)[18:26], sep = "\n")
#
# Multivariate Tests: Site
#                  Df test stat  approx F num Df   den Df     Pr(>F)    
# Pillai            3   1.55394   4.29839     15 60.00000 2.4129e-05 ***
# Wilks             3   0.01230  13.08854     15 50.09147 1.8404e-12 ***
# Hotelling-Lawley  3  35.43875  39.37639     15 50.00000 < 2.22e-16 ***
# Roy               3  34.16111 136.64446      5 20.00000 9.4435e-15 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
结果看起来不像是很容易作为数值访问的。因此,正如您所要求的,下面是将结果处理成矩阵所需的步骤。在完成这项工作并看到user20650的答案后,我建议您遵循该建议,并通过方差分析表获得值

co <- capture.output(fm1$multivariate.tests)[20:24]
s <- strsplit(gsub("([*]+$)|[<]", "", co[-1]), "\\s+")
dc <- do.call(rbind, lapply(s, function(x) as.numeric(x[-1])))
row.names(dc) <- sapply(s, "[", 1)
s2 <- strsplit(co[1], " ")[[1]]
s2 <- s2[nzchar(s2)]
s3 <- s2[-c(1, length(s2))]
colnames(dc) <- c(s2[1], paste(s3[c(TRUE, FALSE)], s3[c(FALSE, TRUE)]), s2[10])
dc
#                  Df test stat  approx F num Df   den Df     Pr(>F)
# Pillai            3   1.55394   4.29839     15 60.00000 2.4129e-05
# Wilks             3   0.01230  13.08854     15 50.09147 1.8404e-12
# Hotelling-Lawley  3  35.43875  39.37639     15 50.00000 2.2200e-16
# Roy               3  34.16111 136.64446      5 20.00000 9.4435e-15

co我也找不到如何提取测试表,但作为一种解决方法,您可以通过对所有测试类型运行
Anova
命令来计算结果

但是,print方法,
print.Anova.mlm
不会返回结果,因此需要稍微调整一下

library(car)

# create new print function
outtests <- car:::print.Anova.mlm

# allow the function to return the results and disable print
body(outtests)[[16]] <- quote(invisible(tests))
body(outtests)[[15]] <- NULL

# Now run the regression
mod <- lm(cbind(Al, Fe, Mg, Ca, Na) ~ Site, data=Pottery)

# Run the Anova over all tests  
tab <- lapply(c("Pillai", "Wilks", "Hotelling-Lawley", "Roy"), 
                  function(i)  outtests(Anova(mod, test.statistic=i)))

tab <- do.call(rbind, tab)
row.names(tab) <- c("Pillai", "Wilks", "Hotelling-Lawley", "Roy")
tab  

   # Type II MANOVA Tests: Pillai test statistic
   #               Df test stat approx F num Df den Df    Pr(>F)    
 #Pillai            3     1.554    4.298     15 60.000 2.413e-05 ***
 #Wilks             3     0.012   13.089     15 50.091 1.840e-12 ***
 #Hotelling-Lawley  3    35.439   39.376     15 50.000 < 2.2e-16 ***
 #Roy               3    34.161  136.644      5 20.000 9.444e-15 ***
 #---
 #Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(+1):很好的解决方法。是否可以将其转换为data.frame以用于xtable?谢谢,嗯,我可以试着用一些字符串操作把它变成一个矩阵,这对我来说很好。我从来没有想过这个方法。我做了太多的数据处理,以至于我忘记了统计数据。
library(car)

# create new print function
outtests <- car:::print.Anova.mlm

# allow the function to return the results and disable print
body(outtests)[[16]] <- quote(invisible(tests))
body(outtests)[[15]] <- NULL

# Now run the regression
mod <- lm(cbind(Al, Fe, Mg, Ca, Na) ~ Site, data=Pottery)

# Run the Anova over all tests  
tab <- lapply(c("Pillai", "Wilks", "Hotelling-Lawley", "Roy"), 
                  function(i)  outtests(Anova(mod, test.statistic=i)))

tab <- do.call(rbind, tab)
row.names(tab) <- c("Pillai", "Wilks", "Hotelling-Lawley", "Roy")
tab  

   # Type II MANOVA Tests: Pillai test statistic
   #               Df test stat approx F num Df den Df    Pr(>F)    
 #Pillai            3     1.554    4.298     15 60.000 2.413e-05 ***
 #Wilks             3     0.012   13.089     15 50.091 1.840e-12 ***
 #Hotelling-Lawley  3    35.439   39.376     15 50.000 < 2.2e-16 ***
 #Roy               3    34.161  136.644      5 20.000 9.444e-15 ***
 #---
 #Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
xtable:::xtable(tab)