从线性模型中自动删除不重要的术语
Automating nonsignificant term removal from linear models
我正在处理一个包含分类和连续自变量的数据集,并且想找出什么是最小适当模型。
这是起始模型:
mod1 <- lm(Richness ~ Distances*Flower*Veg*Canopy*factor(Vines), data = Data)
anova(mod1)
然后我通过手动过程删除不重要的交互项和变量,如下所示:
mod2 <- update(mod1, ~.-Canopy:factor(Vines))
anova(mod2)
有没有办法使这个过程自动化?
感谢您的帮助。
根据@nongkrong 关于 step
函数的建议,我认为我找到了解决方案:
model.null = lm(Abundance ~ 1,
data=data) #define the null model#
model.full = lm(Abundance ~ Distances*Flower*Veg*Canopy*factor(Vines),
data = data) #define the max model#
step(model.full, #start at the full model#
scope = list(lower = model.null), # the lower limit of model it can produce#
direction = "backward", #work by deleting terms until you get to the minimum model #
data = data
)
到目前为止似乎可以正常工作,如果有人发现此代码有问题,请发表评论。
我正在处理一个包含分类和连续自变量的数据集,并且想找出什么是最小适当模型。
这是起始模型:
mod1 <- lm(Richness ~ Distances*Flower*Veg*Canopy*factor(Vines), data = Data)
anova(mod1)
然后我通过手动过程删除不重要的交互项和变量,如下所示:
mod2 <- update(mod1, ~.-Canopy:factor(Vines))
anova(mod2)
有没有办法使这个过程自动化?
感谢您的帮助。
根据@nongkrong 关于 step
函数的建议,我认为我找到了解决方案:
model.null = lm(Abundance ~ 1,
data=data) #define the null model#
model.full = lm(Abundance ~ Distances*Flower*Veg*Canopy*factor(Vines),
data = data) #define the max model#
step(model.full, #start at the full model#
scope = list(lower = model.null), # the lower limit of model it can produce#
direction = "backward", #work by deleting terms until you get to the minimum model #
data = data
)
到目前为止似乎可以正常工作,如果有人发现此代码有问题,请发表评论。