R:如何在减少模型公式后更新模型框架

R: How to update model frame after reducing model formula

我正在 Windows 7 上使用 caper 包进行系统发育多元回归,并且在生成简化模型后尝试绘制残差杠杆图时,我始终收到模型框架/公式不匹配错误.

这里是重现错误所需的最少代码:

 g <- Response ~ (Name1 + Name2 + Name3 + Name4 + Name5 + Name6 + Name7)^2 + Name1Sqd
 + Name2Sqd + Name3Sqd + Name4Sqd + Name5Sqd + Name6Sqd + Name7Sqd

 crunchMod <- crunch(g, data = contrasts)
 plot(crunchMod, which=c(5)) ####Works just fine####

 varName <- row.names(summary(crunchMod)$coefficients)[1]
 #it doesn't matter which predictor I remove.

 Reduce(paste, deparse(g))
 g <- as.formula(paste(Reduce(paste, deparse(g)), as.name(varName), sep=" - "))
 #Edits the model formula to remove varName

 crunchMod <- crunch(g, data = contrasts)
 plot(crunchMod, which=c(5)) ####Error Happens Here####

当我尝试绘制残差杠杆图以查看模型复杂性的影响时,出现以下错误:

Error in model.matrix.default(object, data = list(Response = c(-0.0458443124730482,
: model frame and formula mismatch in model.matrix()

The code that starts this error is: plot(crunchMod, which=c(5)) where crunchMod
holds my regression model via crunchMod <- crunch(g, data = contrasts) from the
caper Package on Windows 7 OS.

如何更新我的模型框架以便能够再次检查厨师的距离(图形或数字)?

在 crunch() 的源代码中有实现:

    data <- subset(data, select = all.vars(formula)) 

它的副作用是使来自已删除主效应的所有交互效应在模型框架中无效。当人们意识到如果 he/she 仅删除交互效应,绘制厨师的距离与杠杆作用将起作用时,这一点就变得更加明显。

因此,为了解决这个问题,在调用crunch()创建线性模型之前,必须将所有交互效果包含在原始数据框中。虽然这会使数据转换稍微复杂一些,但很容易通过以下两个链接添加这些交互:

Generating interaction variables in R dataframes(第二个答案向下)

http://www.r-bloggers.com/type-conversion-and-you-or-and-r/