在 R 中使用 lapply 和方差分析时的行为不一致
Inconsistent behaviour when using lapply and anova in R
函数 anova.lm
的文档提供了一个示例,其中通过函数 anova
.
设置并比较了五个不同的线性模型
data(LifeCycleSavings)
fit0 <- lm(sr ~ 1, data = LifeCycleSavings)
fit1 <- update(fit0, . ~ . + pop15)
fit2 <- update(fit1, . ~ . + pop75)
fit3 <- update(fit2, . ~ . + dpi)
fit4 <- update(fit3, . ~ . + ddpi)
anova(fit0, fit1, fit2, fit3, fit4, test = "F")
您也可以使用 lapply
对模型连续执行函数 anova
。
fit_L = list(fit0, fit1, fit2, fit3, fit4)
lapply(fit_L, anova)
类似地,包 diversitree 的函数 find.mle
的文档提供了一个示例,其中通过函数 anova
.
设置并比较了两个模型
library(diversitree)
#
pars <- c(0.1, 0.2, 0.03, 0.03, 0.01, 0.01)
phy <- tree.bisse(pars, max.t=60, x0=0)
lik <- make.bisse(phy, phy$tip.state)
fit <- find.mle(lik, pars)
lik.l <- constrain(lik, lambda0 ~ lambda1)
fit.l <- find.mle(lik.l, pars[-2])
anova(fit, equal.lambda=fit.l)
然而,在这里,我不能使用 lapply 在两个模型上执行函数 anova
。
fit_L = list(fit, fit.l)
lapply(fit_L, anova)
# Error in anova.fit.mle(X[[i]], ...) : Need to specify more than one model
任何人都可以想出一种方法来使用 lapply
(或类似的功能)到包 diversitree
中的示例吗?
编辑 1:
澄清我的问题:我的 post 的基本思想是使 lapply
独立于要测试的模型的精确数量。对于一些分析,我不知道有多少模型将被测试 先验 ,所以最好 lapply
方差分析尽管许多模型恰好在列表中fit_L
.
lapply
遍历列表的元素并对它们应用函数。这不是你想要的。您希望将所有列表元素作为参数传递给函数,这就是 do.call
所做的:
do.call(anova, c(fit_L, test = "F"))
如果您查看使用 anova.lm
的示例,您会发现使用 lapply
时输出不同。来自文档:
Specifying a single object gives a sequential analysis of variance
table for that fit. [...] If more than one object is specified, the
table has a row for the residual degrees of freedom and sum of squares
for each model. For all but the first model, the change in degrees of
freedom and sum of squares is also given. ...
lapply
将单个对象传递给 anova.lm
。这不适用于您的 mle
拟合,因为相应的 anova
方法仅进行模型比较。
函数 anova.lm
的文档提供了一个示例,其中通过函数 anova
.
data(LifeCycleSavings)
fit0 <- lm(sr ~ 1, data = LifeCycleSavings)
fit1 <- update(fit0, . ~ . + pop15)
fit2 <- update(fit1, . ~ . + pop75)
fit3 <- update(fit2, . ~ . + dpi)
fit4 <- update(fit3, . ~ . + ddpi)
anova(fit0, fit1, fit2, fit3, fit4, test = "F")
您也可以使用 lapply
对模型连续执行函数 anova
。
fit_L = list(fit0, fit1, fit2, fit3, fit4)
lapply(fit_L, anova)
类似地,包 diversitree 的函数 find.mle
的文档提供了一个示例,其中通过函数 anova
.
library(diversitree)
#
pars <- c(0.1, 0.2, 0.03, 0.03, 0.01, 0.01)
phy <- tree.bisse(pars, max.t=60, x0=0)
lik <- make.bisse(phy, phy$tip.state)
fit <- find.mle(lik, pars)
lik.l <- constrain(lik, lambda0 ~ lambda1)
fit.l <- find.mle(lik.l, pars[-2])
anova(fit, equal.lambda=fit.l)
然而,在这里,我不能使用 lapply 在两个模型上执行函数 anova
。
fit_L = list(fit, fit.l)
lapply(fit_L, anova)
# Error in anova.fit.mle(X[[i]], ...) : Need to specify more than one model
任何人都可以想出一种方法来使用 lapply
(或类似的功能)到包 diversitree
中的示例吗?
编辑 1:
澄清我的问题:我的 post 的基本思想是使 lapply
独立于要测试的模型的精确数量。对于一些分析,我不知道有多少模型将被测试 先验 ,所以最好 lapply
方差分析尽管许多模型恰好在列表中fit_L
.
lapply
遍历列表的元素并对它们应用函数。这不是你想要的。您希望将所有列表元素作为参数传递给函数,这就是 do.call
所做的:
do.call(anova, c(fit_L, test = "F"))
如果您查看使用 anova.lm
的示例,您会发现使用 lapply
时输出不同。来自文档:
Specifying a single object gives a sequential analysis of variance table for that fit. [...] If more than one object is specified, the table has a row for the residual degrees of freedom and sum of squares for each model. For all but the first model, the change in degrees of freedom and sum of squares is also given. ...
lapply
将单个对象传递给 anova.lm
。这不适用于您的 mle
拟合,因为相应的 anova
方法仅进行模型比较。