bartlett.test失败,公式为col1~col2 + col3

时间:2014-07-03 11:34:46

标签: r variance

我希望执行具有多个独立分组变量的bartlett.test。但是,会生成错误。

以下是一些数据:

d=read.table(text='
1 w e
2 w e
3 w r
3 e r
4 e r
5 e e
4 w r
6 e e')

当我使用公式V1~V2测试一个分组变量的方差同质性时,一切都很完美:

bartlett.test(V1 ~ V2, data = d)

    Bartlett test of homogeneity of variances

data:  V1 by V2
Bartlett's K-squared = 0, df = 1, p-value = 1

但是当尝试多个自变量时,会导致错误:

bartlett.test(V1 ~ V2 + V3, data = d)
Error in bartlett.test.formula(V1 ~ V2 + V3, data = d) : 
  'formula' should be of the form response ~ group

然而我想用多列来测试方差的同质性,即测试V1~V2 + V3,所以任何帮助都是值得赞赏的。

1 个答案:

答案 0 :(得分:0)

This tutorial表示"对于多个自变量,必须使用interaction函数将IV合并为具有所有因子组合的单个变量。如果没有使用,那么将是错误的自由度,并且p值将是错误的。"。因此,

bartlett.test(V1 ~ interaction(V2, V3), data = d)
# Bartlett test of homogeneity of variances
# 
# data:  V1 by interaction(V2, V3)
# Bartlett's K-squared = 0, df = 3, p-value = 1