R:如何在减少模型公式后更新模型框架 [英] R: How to update model frame after reducing model formula

查看:70
本文介绍了R:如何在减少模型公式后更新模型框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下是重现该错误所需的最少代码:

 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)) 

具有使已删除的主效果中的所有交互效果在模型框架中无效的副作用.当人们意识到绘制厨师的距离与杠杆的关系图(如果他/她仅删除交互作用)将起作用时,这一点就变得更加明显.

因此,要解决此问题,必须在调用crunch()创建线性模型之前将所有交互效果都包括在原始数据帧中.虽然这会使数据转换稍微复杂一些,但可以通过以下两个链接轻松添加这些交互:

在R数据帧中生成交互变量(第二个答案不正确)

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

I am working a phylogenetic multiple regression using the caper package on Windows 7, and am receiving a Model frame / formula mismatch error consistently when ever I try to graph a residual leverage plot after generating a reduced model.

Here is the minimal code needed to reproduce the error:

 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####


When I try to graph a residual leverage plot to look at the effects of model complexity, I get the following error:

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.

How can I update my model frame to be able to examine cook's distance again (either graphically or numerically)?

解决方案

Within the source code of crunch() there was the implementation:

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

which has the side effect of making all interaction effects from a deleted primary effect invalid in the model frame. This becomes more apparent when one realizes that plotting cook's distance vs leverage will work if he/she only deletes interaction effects.

Thus to solve this problem, all interaction effects must be included in the original data frame before calling crunch() to create a linear model. While this makes transforming the data slightly more complicated, it is easy to add these interactions following these two links:

Generating interaction variables in R dataframes (second answer down)

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

这篇关于R:如何在减少模型公式后更新模型框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆