R中嵌套模型序列的构造

时间:2017-04-16 20:43:46

标签: r glm

对于广义线性模型的主题:

如何通过在时间添加一个变量来构建(至少100个)模型的嵌套序列?

有一个基础model_0,E(Y)= b0 + b1x1 + b2x2,这是下一个复杂模型的一部分。

模式是:

  

model_1 = model_0+b3x1*x2
model_2 = model_1+b4x1^2
model_3 = model_2+b5x2^2
model_4 = model_3+b6x1^2*x2
model_5 = model_4+b7x1*x2^2
model_6 = model_5+b8x1^2*x2^2
model_7 = model_6+b10x1^3
model_8 = model_7+b11x2^3
model_9 = model_8+b12x1^3x2 
etc.

任务提示使用poly()和update()函数,主要任务是针对其他生成模型的生成AIC测试AIC(model_o)并应用测试统计信息。

对于编码以上模式的任何帮助都会感到高兴。

1 个答案:

答案 0 :(得分:1)

我发现最简单的方法是依靠var turn = 'X'; $('.xo-btns').click(function(){ if (against != 'PC'){ show(this, turn, board); // someoneWon(board); switchTurns (turn); // if (turn == 'X'){ // turn = 'O'; // } else { // turn = 'X'; // } } }); 来推导公式的所有组合。

例如: 使用leaps::regsubsets数据集,swiss作为响应变量,我可以依赖regsubsets来提供预测变量的所有差异组合的逻辑矩阵。

Fertility

使用矩阵构建模型以进行简单线性回归:

library(leaps)

mod <- regsubsets(Fertility ~ ., data=swiss, nvmax = 100, nbest = 10, really.big= T, method = "exhaustive")
head(summary(mod)$which[,-1])

  Agriculture Examination Education Catholic Infant.Mortality
1       FALSE       FALSE      TRUE    FALSE            FALSE
1       FALSE        TRUE     FALSE    FALSE            FALSE
1       FALSE       FALSE     FALSE     TRUE            FALSE
1       FALSE       FALSE     FALSE    FALSE             TRUE
1        TRUE       FALSE     FALSE    FALSE            FALSE
2       FALSE       FALSE      TRUE     TRUE            FALSE

使用矩阵为简单的GAM模型构建模型:

am <- summary(mod)$which[,-1]
pred <- lapply(1:nrow(am), function(x) colnames(am)[which(am[x,])])
lm.mod.form <- lapply(pred, function(x) paste0("lm(Fertility ~ ", paste(x, collapse="+"), ", data=swiss)"))

head(lm.mod.form)

[[1]]
[1] "lm(Fertility ~ Education, data=swiss)"

[[2]]
[1] "lm(Fertility ~ Examination, data=swiss)"

[[3]]
[1] "lm(Fertility ~ Catholic, data=swiss)"

[[4]]
[1] "lm(Fertility ~ Infant.Mortality, data=swiss)"

[[5]]
[1] "lm(Fertility ~ Agriculture, data=swiss)"

[[6]]
[1] "lm(Fertility ~ Education+Catholic, data=swiss)"

all.mods <- lapply(lm.mod.form, function(x) eval(parse(text=x)))

编辑:

am <- summary(mod)$which[,-1]
pred <- lapply(1:nrow(am), function(x) colnames(am)[which(am[x,])])

gam.mod.form <- lapply(pred, function(x) paste0("gam(Fertility ~ s(", paste(x, collapse=") + s("), "), data=swiss)"))
head(gam.mod.form)

[[1]]
[1] "gam(Fertility ~ s(Education), data=swiss)"

[[2]]
[1] "gam(Fertility ~ s(Examination), data=swiss)"

[[3]]
[1] "gam(Fertility ~ s(Catholic), data=swiss)"

[[4]]
[1] "gam(Fertility ~ s(Infant.Mortality), data=swiss)"

[[5]]
[1] "gam(Fertility ~ s(Agriculture), data=swiss)"

[[6]]
[1] "gam(Fertility ~ s(Education) + s(Catholic), data=swiss)"