我需要为我的modell(amod.null)找到最佳的空间相关结构。为此,我用五种不同的方法更新了一个modell:
methods <-c("corExp", "corGaus", "corLin", "corRatio", "corSpher")
library(nlme)
amod.null <- lme(fixed=Abundance ~ Date, data = D, random = ~1| Date, method="ML")
amod.exp <- update(amod.null, correlation = corExp(1, form = ~ x + y), method="ML")
amod.gau <- update(amod.null, correlation = corGaus(1, form = ~ x + y), method="ML")
amod.lin <- update(amod.null, correlation = corLin(1, form = ~ x + y), method="ML")
amod.rat <- update(amod.null, correlation = corRatio(1, form = ~ x + y), method="ML")
amod.sph <- update(amod.null, correlation = corSpher(1, form = ~ x + y), method="ML")
这部分我想要loop
,例如
res.list <- lapply(amod.null, function(x) update(x, method="ML", correlation=paste("One of the five methods")(1, form = ~ x + y))
或者更好for (method in methods)
?
不知道如何创建合适的loop
。
names(res.list) <- c("null",methods)
所以我稍后可以提取AIC
s:
list.AIC <- lapply(res.list, function(x) AIC(x))
并在list.AIC
中找到最小值:
best.mod <- names(which.min(list.AIC))
注意:数据"D"
是更大的"OTUs"
(以及x
和y
)集的子集,对于每个,我试图找到最好的模型。这已经发生在for-loop
。
这是玩具数据:
D <- structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L), .Label = c("April", "May", "June", "Aug.",
"Oct.", "Nov."), class = c("ordered", "factor")), x = c(0.835,
0.835, 2.505, 2.505, 3.68, 3.68, 5.845, 5.845, 7.515, 7.515,
1.33, 1.33, 3, 3, 4.175, 4.175, 6.34, 6.34, 7.02, 7.02, 0.835,
0.835, 3, 3, 4.67, 4.67, 6.34, 6.34, 7.515, 7.515, 1.33, 1.33,
2.505, 2.505, 3.68, 3.68, 5.35, 5.35, 8.01, 8.01, 0.34, 0.34,
2.01, 2.01, 4.175, 4.175, 5.35, 5.35, 7.02, 7.02, 0.34, 0.34,
2.01, 2.01, 4.67, 4.67, 5.845, 5.845, 8.01, 8.01), y = c(8.75,
8.25, 8.75, 8.25, 9.75, 9.25, 9.75, 9.25, 9.75, 9.25, 8.75, 8.25,
8.75, 8.25, 8.75, 8.25, 8.75, 8.25, 8.75, 8.25, 9.75, 9.25, 9.75,
9.25, 8.75, 8.25, 9.75, 9.25, 8.75, 8.25, 9.75, 9.25, 9.75, 9.25,
8.75, 8.25, 8.75, 8.25, 9.75, 9.25, 9.75, 9.25, 9.75, 9.25, 9.75,
9.25, 9.75, 9.25, 9.75, 9.25, 8.75, 8.25, 8.75, 8.25, 9.75, 9.25,
8.75, 8.25, 8.75, 8.25), OTU = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "OTU_Aprot_14838", class = "factor"),
Abundance = c(1.48742098901607, 1.47602962127657, 1.99781826705493,
2.16799070316816, 1.42730141362433, 2.09405099150142, 1.42149664725555,
1.71617918712907, 2.11969750781785, 2.00363569653608, 2.06911262798635,
3.06502106177575, 2.62679182129084, 2.43131628439033, 2.65580291946686,
2.545622671272, 3.34947567204109, 2.44114901281655, 3.14537211688011,
3.03783080180767, 3.60165104432695, 3.57000720670331, 2.99622878692646,
2.66145144132223, 2.8018956792242, 2.52402622893616, 2.80692169132398,
3.17718621641112, 2.84984188360835, 3.26996222461549, 2.41208590689059,
2.40563161687268, 2.33524720673279, 2.61013078030161, 2.99901671583088,
2.49504464207406, 2.59731033033264, 1.85027677857431, 2.21967857846652,
2.82016562659445, 2.5840233804189, 1.90332908755883, 2.92665553153861,
3.15514411586858, 2.34252722773885, 2.02662959200043, 2.56737326954397,
1.82033908889375, 3.23909854647286, 2.73334042669443, 2.89323508548431,
2.84211486172823, 3.51909510289364, 3.89335076578259, 3.82968829072805,
3.59667320927228, 3.76602962682929, 3.62572486092721, 4.15772493029095,
4.30691542046211), Taxon = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Alphaproteobacteria", class = "factor"),
dummy = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1)), .Names = c("Date", "x", "y", "OTU", "Abundance",
"Taxon", "dummy"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L,
120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 179L,
180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 239L, 240L,
241L, 242L, 243L, 244L, 245L, 246L, 247L, 248L, 299L, 300L, 301L,
302L, 303L, 304L, 305L, 306L, 307L, 308L), class = "data.frame")
答案 0 :(得分:2)
我会使用getFunction
:
mods <- lapply(methods, function(m)
update(amod.null, correlation = getFunction(m)(1, form = ~ x + y), method="ML"))
sapply(mods, AIC)
#[1] 69.01810 69.20616 69.28956 69.59196 69.18809