基于R中模型平均系数的部分残差图

基于R中模型平均系数的部分残差图,r,plot,lm,r-car,mumin,R,Plot,Lm,R Car,Mumin,我使用R包MuMIn进行多模态推理,并使用函数model.avg对一组模型估计的系数进行平均。为了直观地将数据与基于平均系数的估计关系进行比较,我想使用部分残差图,类似于car包的crPlots函数创建的残差图。我试过三种方法,我不确定是否有合适的方法。这里有一个演示 library(MuMIn) # Loading the data data(Cement) # Creating a full model with all the covariates we are interested in

我使用R包
MuMIn
进行多模态推理,并使用函数
model.avg
对一组模型估计的系数进行平均。为了直观地将数据与基于平均系数的估计关系进行比较,我想使用部分残差图,类似于
car
包的
crPlots
函数创建的残差图。我试过三种方法,我不确定是否有合适的方法。这里有一个演示

library(MuMIn)
# Loading the data
data(Cement)
# Creating a full model with all the covariates we are interested in
fullModel <- lm(y ~ ., data = Cement, na.action=na.fail)
# Getting all possible models based on the covariates of the full model
muModel <- dredge(fullModel)
# Averaging across all models
avgModel <- model.avg(muModel)
# Getting the averaged coefficients
coefMod <- coef(avgModel)
coefMod
# (Intercept)          X1          X2          X4          X3 
# 65.71487660  1.45607957  0.61085531 -0.49776089 -0.07148454 
请注意,具有平均系数的完整版本和黑客版本的CRPLOT是不同的。

这里的问题是:这是否合适?结果取决于我在这篇文章中发现的一个黑客。除了残差和系数之外,我是否需要更改模型的其他部分

备选方案2:自制地块

# Partial residuals: residuals(hacked model) + beta*x
# X1
# Get partial residuals
prX1 <- resid(hackModel) + coefMod["X1"]*Cement$X1
# Plot the partial residuals
plot(prX1 ~ Cement$X1)
# Add modeled relationship
abline(a=0,b=coefMod["X1"])
# X2 - X4
plot(resid(hackModel) + coefMod["X2"]*X2 ~ X2, data=Cement); abline(a=0,b=coefMod["X2"])
plot(resid(hackModel) + coefMod["X3"]*X3 ~ X3, data=Cement); abline(a=0,b=coefMod["X3"])
plot(resid(hackModel) + coefMod["X4"]*X4 ~ X4, data=Cement); abline(a=0,b=coefMod["X4"])

现在我们的值与上面的
crPlots
相似,但关系仍然不同。差异可能与拦截有关。但是我不确定应该使用什么来代替0

对哪种方法更合适有什么建议?是否有一种更直接的方法来获得基于模型平均系数的部分残差图


非常感谢

通过查看
crPlot.lm
源代码,看起来只有函数
残差(model,type=“partial”)
预测(model,type=“terms”,term=var)
,以及与查找变量名称相关的函数在模型对象上使用。正如@BenBolker所说,这种关系似乎也在倒退。
crPlot.lm
中使用的代码是:
abline(lm(partial.res[,var]~.x),lty=2,lwd=lwd,col=col.lines[1])
。因此,我认为改变模型的系数和残差就足以在其上使用
crPlots
。我现在也可以用自制的方式复制结果

library(MuMIn)
# Loading the data
data(Cement)
# Creating a full model with all the covariates we are interested in
fullModel <- lm(y ~ ., data = Cement, na.action=na.fail)
# Getting all possible models based on the covariates of the full model
muModel <- dredge(fullModel)
# Averaging across all models
avgModel <- model.avg(muModel)
# Getting the averaged coefficients
coefMod <- coef(avgModel)

# Option 1 - crPlots
library(car) # For crPlots
# Creating a duplicate of the fullMode
hackModel <- fullModel
# Changing the coefficents to the averaged coefficient
hackModel$coefficients <- coefMod[names(coef(fullModel))]
# Changing the residuals
hackModel$residuals <- Cement$y - predict(hackModel)

# Plot the crPlots and the regressed homemade version 
layout(matrix(1:8, nrow=2, byrow=TRUE))
par(mar=c(3.5,3.5,0.5,0.5), mgp=c(2,1,0))
crPlots(hackModel, layout=NA, ylab="Partial Res", smooth=FALSE)

# Option 4 - Homemade centered and regressed
# Get the centered partial residuals
pRes <- resid(hackModel, type='partial')
# X1 - X4 plot partial residuals and used lm for the relationship
plot(pRes[,"X1"] ~ Cement$X1); abline(lm(pRes[,"X1"]~Cement$X1))
plot(pRes[,"X2"] ~ Cement$X2); abline(lm(pRes[,"X2"]~Cement$X2))
plot(pRes[,"X3"] ~ Cement$X3); abline(lm(pRes[,"X3"]~Cement$X3))
plot(pRes[,"X4"] ~ Cement$X4); abline(lm(pRes[,"X4"]~Cement$X4))
库(MuMIn)
#加载数据
数据(水泥)
#创建一个包含我们感兴趣的所有协变量的完整模型

fullModel这对我来说可能是个问题。。。我不知道为什么您既不在选项2中添加截距,也不将预测值居中?你有没有看过车里面::crPlot
看它在干什么?(看起来它实际上是对相关变量的部分残差进行回归——不仅仅是使用简单的多元斜率估计……我还需要再复习一下这个理论。这在Fox的《应用回归的同伴》中有详细说明吗?@BenBolker谢谢!我没有访问福克斯的书,但我已经从
car
下载了源代码,是的,
crPlot
回归部分残差:
abline(lm(partial.res[,var]~.x),lty=2,lwd=lwd,col=col.lines[1])
。我不知道这为什么有意义,但你是对的,理解为什么这个问题会成为一个交叉验证的问题。我希望能够从
MuMIn
软件包中为平均模型对象绘制组件+残差图,或者确认只需更改残差和模型中的系数就可以了应阅读:hackModel$Courters在glmmadmb类对象上执行crPlots的任何经验???@Marie Auger Methe-在没有模型对象的情况下创建这些绘图的任何经验?我基本上将许多模型对象的系数估计值存储在excel文件中,不再有模型对象。有什么建议吗?
# Get the centered partial residuals
pRes <- resid(hackModel, type='partial')
# X1
# Plot the partial residuals
plot(pRes[,"X1"] ~ Cement$X1)
# Plot the component - modeled relationship
lines(coefMod["X1"]*(X1-mean(X1))~X1, data=Cement)
# X2 - X4
plot(pRes[,"X2"] ~ Cement$X2); lines(coefMod["X2"]*(X2-mean(X2))~X2, data=Cement) 
plot(pRes[,"X3"] ~ Cement$X3); lines(coefMod["X3"]*(X3-mean(X3))~X3, data=Cement)
plot(pRes[,"X4"] ~ Cement$X4); lines(coefMod["X4"]*(X4-mean(X4))~X4, data=Cement)
library(MuMIn)
# Loading the data
data(Cement)
# Creating a full model with all the covariates we are interested in
fullModel <- lm(y ~ ., data = Cement, na.action=na.fail)
# Getting all possible models based on the covariates of the full model
muModel <- dredge(fullModel)
# Averaging across all models
avgModel <- model.avg(muModel)
# Getting the averaged coefficients
coefMod <- coef(avgModel)

# Option 1 - crPlots
library(car) # For crPlots
# Creating a duplicate of the fullMode
hackModel <- fullModel
# Changing the coefficents to the averaged coefficient
hackModel$coefficients <- coefMod[names(coef(fullModel))]
# Changing the residuals
hackModel$residuals <- Cement$y - predict(hackModel)

# Plot the crPlots and the regressed homemade version 
layout(matrix(1:8, nrow=2, byrow=TRUE))
par(mar=c(3.5,3.5,0.5,0.5), mgp=c(2,1,0))
crPlots(hackModel, layout=NA, ylab="Partial Res", smooth=FALSE)

# Option 4 - Homemade centered and regressed
# Get the centered partial residuals
pRes <- resid(hackModel, type='partial')
# X1 - X4 plot partial residuals and used lm for the relationship
plot(pRes[,"X1"] ~ Cement$X1); abline(lm(pRes[,"X1"]~Cement$X1))
plot(pRes[,"X2"] ~ Cement$X2); abline(lm(pRes[,"X2"]~Cement$X2))
plot(pRes[,"X3"] ~ Cement$X3); abline(lm(pRes[,"X3"]~Cement$X3))
plot(pRes[,"X4"] ~ Cement$X4); abline(lm(pRes[,"X4"]~Cement$X4))