问题描述
我有一个关于 R 的问题.
I have a question about R.
我正在使用名为 levene.test 的测试来测试方差的同质性.
I am using a test called levene.test to test a homogeneity of variance.
我知道您需要一个至少具有两个级别的因子变量才能使其发挥作用.从我所见,我使用的因子变量至少有两个级别.但不知何故,我不断收到以下错误:
I know that you need a factor variable with at least two levels in order for this to work. And from what I see, I do have at least two levels for the factor variable that I am using. But somehow I keep getting the error of:
> nocorlevene <- levene.test(geno1rs11809462$SIF1, geno1rs11809462$k, correction.method = "correction.factor")
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
我什至尝试从二项分布中生成一个变量:
I even try generate a variable from a binomial distribution:
k<-rbinom(1304, 1, 0.5)
然后将其用作一个因素,但仍然无效.
and then use that as a factor, but is still not working.
最后,我创建了一个具有 3 个级别的变量:
Lastly I create a variable with 3 levels:
k<-sample(c(1,0,2), 1304, replace=T)
但有些仍然无法正常工作并出现以下错误:
but some how still not working and getting the same error of:
nocorlevene <- levene.test(geno1rs11809462$SIF1, geno1rs11809462$k, Correction.method="zero.removal")
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
这是数据中变量类型的输出:
This is the output of the type of the variable in the data:
> str(geno1rs11809462)
'data.frame': 1304 obs. of 16 variables:
$ id : chr "WG0012669-DNA_A03_K05743" "WG0012669-DNA_A04_K05752" "WG0012669-DNA_A05_K05761" "WG0012669-DNA_A06_K05785" ...
$ rs11809462 : Factor w/ 2 levels "2/1","2/2": 2 2 2 2 2 2 2 2 2 2 ...
..- attr(*, "names")= chr "WG0012669-DNA_A03_K05743" "WG0012669-DNA_A04_K05752" "WG0012669-DNA_A05_K05761" "WG0012669-DNA_A06_K05785" ...
$ FID : chr "9370" "9024" "14291" "4126" ...
$ AGE_CALC : num 61 47 NA 62.5 55.6 59.7 46.6 41.2 NA 46.6 ...
$ MREFSUM : num 185 325 NA 211 212 ...
$ NORSOUTH : Factor w/ 3 levels "0","1","NA": 1 1 3 1 1 1 1 1 3 1 ...
$ smoke1 : Factor w/ 3 levels "0","1","NA": 2 2 3 1 1 1 2 1 3 1 ...
$ smoke2 : Factor w/ 3 levels "0","1","NA": 1 1 3 2 2 2 1 2 3 2 ...
$ ANYCG60 : num 0 0 NA 1 0 0 0 0 NA 1 ...
$ DCCT_HBA_MEAN: num 7.39 6.93 NA 7.37 7.56 7.86 6.22 8.88 NA 8.94 ...
$ EDIC_HBA : num 7.17 7.63 NA 8.66 9.68 7.74 6.59 9.34 NA 7.86 ...
$ HBAEL : num 7.3 8.82 NA 9.1 9.3 ...
$ ELDTED_HBA : num 7.23 7.76 NA 8.36 9.21 7.92 6.64 9.64 NA 9.09 ...
$ SIF1 : num 19.6 17 NA 23.8 24.1 ...
$ sex : Factor w/ 2 levels "0","1": 1 1 2 2 2 2 1 1 1 1 ...
$ k : Factor w/ 3 levels "0","1","2": 1 1 2 3 1 3 3 3 1 2 ...
如您所见,变量 k、sex 分别有 3 和 2 个级别,但不知何故我仍然收到该错误消息.
As you can see the variable k, sex have 3 and 2 levels respectively but somehow I still get that error message.
> head(geno1rs11809462)
id rs11809462 FID AGE_CALC MREFSUM NORSOUTH smoke1 smoke2 ANYCG60
1 WG0012669-DNA_A03_K05743 2/2 9370 61.0 184.5925 0 1 0 0
2 WG0012669-DNA_A04_K05752 2/2 9024 47.0 325.0047 0 1 0 0
3 WG0012669-DNA_A05_K05761 2/2 14291 NA NA NA NA NA NA
4 WG0012669-DNA_A06_K05785 2/2 4126 62.5 211.2557 0 0 1 1
5 WG0012669-DNA_A08_K05802 2/2 11280 55.6 212.2922 0 0 1 0
6 WG0012669-DNA_A09_K05811 2/2 11009 59.7 261.0116 0 0 1 0
DCCT_HBA_MEAN EDIC_HBA HBAEL ELDTED_HBA SIF1 sex k
1 7.39 7.17 7.30 7.23 19.6136 0 0
2 6.93 7.63 8.82 7.76 17.0375 0 0
3 NA NA NA NA NA 1 1
4 7.37 8.66 9.10 8.36 23.8333 1 2
5 7.56 9.68 9.30 9.21 24.1338 1 0
6 7.86 7.74 8.53 7.92 25.7272 1 2
如果有人能给我一些关于为什么会发生这种情况的提示,那就太好了.我只是不知道为什么在我运行测试时变量 k 或性别或具有不同的级别会给我错误.
If anyone can give me some hints as to why this is happening, it would be great. I just don't know why the variable k or sex or having different levels are giving me error when I run the test.
谢谢
推荐答案
我想我可能已经解决了这个问题.我相信这是由于数据中的 NA 值.因为在我使用 say
I think I may have solved the problem. I believe it is due to NA value in the data. Because after I removed the na using say
x<-na.omit(original_data)
然后对 x 应用 levene 测试,警告消息消失.
then apply the levene test on x, the warning message disappears.
希望这是问题的原因.
这篇关于对比只能应用于因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!