这是我第一次询问关于R的问题,如果我遗漏了必要的细节,请原谅我。我会尝试尽可能彻底但简洁。
我的数据有4列:得分(值为-1,0或1);物种(至少10种不同的物种);栖息地(三个不同的);和网站(至少8个不同的)。因此,每一行都是得分,物种,栖息地和场地的独特组合。请参阅下面的顶行:
score species habitat site
0 NOCA NAG NAG1
0 BRTH NAG NAG1
0 BARS NAG NAG1
1 COYE NAG NAG1
0 HOWR NAG NAG1
0 SAVS NAG NAG1
0 CEDW NAG NAG1
1 CHSP NAG NAG1
0 EAKI NAG NAG1
1 MODO NAG NAG1
0 NOCA NAG NAG16
0 BRTH NAG NAG16
我跑了一个lme来评估物种和栖息地对得分的影响,场地是随机效应。代码如下:
anova(model<-lme(score~species*habitat, random=~1|site))
然后,我想进行成对比较,以找出显着差异的位置。 TukeyHSD不能用于lme,所以我使用了multcomp包,如下所示:
summary(glht(model, linfct=mcp(habitat="Tukey")))
据我所知,根据栖息地类型对比分进行成对比较。我也尝试用物种代替栖息地进行物种比较。在这两种情况下,我都收到以下错误:
contrMat中的错误(表(mf [[nm]]),type = types [pm]):小于 两组
我看不出我怎么能在任何地方少于两个组,因为我对任何列的最小组数是3。也许我误解了R所指的“群体”?关于可能出现的问题以及如何解决问题的任何建议?
编辑根据建议(谢谢),以下是有关我的数据的更多信息,可能会使其重现。首先,我完整使用的代码。
library(nlme)
library(multcomp)
null<-read.csv("baci_null_red.csv", head=TRUE)
attach(null)
anova(model<-lme(score~species*habitat, random=~1|site))
numDF denDF F-value p-value
(Intercept) 1 1392 1.929021 0.1651
species 29 1392 2.691207 <.0001
habitat 2 48 3.412485 0.0411
species:habitat 58 1392 1.239267 0.1099
摘要(glht(model,linfct = mcp(habitat =“Tukey”)))
Error in contrMat(table(mf[[nm]]), type = types[pm]) :
less than two groups
以下是有关可能有助于复制它的数据的一些摘要...它有点大,我不知道如何使其最小化但仍然得到错误。
str(null)
'data.frame': 1530 obs. of 4 variables:
$ score : int 0 0 -1 0 0 0 0 0 0 0 ...
$ species: Factor w/ 30 levels "AMGO","AMRO",..: 27 5 7 2 18 12 22 28 6 13 ...
$ habitat: Factor w/ 3 levels "CUM","IAG","NAG": 3 3 3 3 3 3 3 3 3 3 ...
$ site : Factor w/ 51 levels " CUM6","CUM1",..: 37 37 37 37 37 37 37 37 37 37 ..
summary(null)
score species habitat site
Min. :-1.00000 AMGO : 51 CUM:210 CUM6 : 30
1st Qu.: 0.00000 AMRO : 51 IAG:660 CUM1 : 30
Median : 0.00000 BARS : 51 NAG:660 CUM10 : 30
Mean :-0.01569 BCCH : 51 CUM12 : 30
3rd Qu.: 0.00000 BHCO : 51 CUM2 : 30
Max. : 1.00000 BLJA : 51 CUM3 : 30
(Other):1224 (Other):1350
dput(null[somerows,c("species","habitat","site")])
structure(list(species = structure(c(27L, 5L, 7L, 2L, 18L, 12L,
22L, 28L, 6L, 13L, 15L, 23L, 4L, 10L, 14L, 11L, 20L, 30L, 9L,
19L, 26L, 8L, 16L, 25L, 3L, 17L, 21L, 29L, 24L, 1L, 27L, 5L,
7L, 2L, 18L, 12L, 22L, 28L, 6L, 13L, 15L, 23L, 4L, 10L, 14L,
11L, 20L, 30L, 9L, 19L, 26L, 8L, 16L, 25L, 3L, 17L, 21L, 29L,
24L, 1L, 27L, 5L, 7L, 2L, 18L, 12L, 22L, 28L, 6L, 13L, 15L, 23L,
4L, 10L, 14L, 11L, 20L, 30L, 9L, 19L, 26L, 8L, 16L, 25L, 3L,
17L, 21L, 29L, 24L, 1L, 27L, 5L, 7L, 2L, 18L, 12L, 22L, 28L,
6L, 13L), .Label = c("AMGO", "AMRO", "BARS", "BCCH", "BHCO",
"BLJA", "BOBO", "BRTH", "CEDW", "CSWA", "EABL", "EAKI", "EAWP",
"FISP", "GCFL", "HOLA", "HOWR", "INBU", "KILL", "LEFL", "MODO",
"NOCA", "RBGR", "RWBL", "SAVS", "SOSP", "VESP", "WIFL", "YSFL",
"YWAR"), class = "factor"), habitat = structure(c(3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("CUM", "IAG", "NAG"), class = "factor"), site = structure(c(37L,
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L,
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L,
37L, 37L, 37L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L,
46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L,
46L, 46L, 46L, 46L, 46L, 46L, 46L, 47L, 47L, 47L, 47L, 47L, 47L,
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L,
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 48L, 48L,
48L, 48L, 48L, 48L, 48L, 48L, 48L, 48L), .Label = c(" CUM6",
"CUM1", "CUM10", "CUM12", "CUM2", "CUM3", "CUM8", "IAG1", "IAG10",
"IAG13", "IAG14", "IAG15", "IAG16", "IAG18", "IAG19", "IAG21",
"IAG22", "IAG23", "IAG24", "IAG25", "IAG26", "IAG27", "IAG28",
"IAG3", "IAG4", "IAG5", "IAG6", "IAG8", "IAG9", "NAG10", "NAG11",
"NAG13", "NAG14", "NAG15", "NAG18", "NAG19", "NAG2", "NAG21",
"NAG22", "NAG23", "NAG24", "NAG25", "NAG26", "NAG27", "NAG28",
"NAG3", "NAG4", "NAG5", "NAG6", "NAG7", "NAG8"), class = "factor")), .Names =
c("species", "habitat", "site"), row.names = c(NA, 100L), class = "data.frame")
你有它:感谢任何指导。
答案 0 :(得分:0)
我刚刚通过避免附加数据框来解决此问题。我在网上读到,当试图将拟合模型传递给函数时,附加数据框可能会产生问题。
我建议使用这样的代码,而不是使用CURRENT_TIMESTAMP
attach()