绘制适用于R的GLM的Profile Deviance

时间:2012-03-20 13:50:43

标签: r plot glm

我希望能够绘制使用R中的函数glm()拟合的参数估计的轮廓偏差。轮廓偏差是在估计所有参数估计的不同值之后的偏差函数。其他参数。我需要在拟合参数周围绘制几个值的偏差,以检查二次偏差函数的假设。

我的模型预测会对罪犯进行重新定罪。该公式的形式如下: reconv ~ [other variables] + sex,其中reconv是二进制是/否因子,sex是二元男/女因子。我想绘制性别=女性估计参数的轮廓偏差(性别=男性是参考水平)。

glm()函数估计参数为-0.22,标准误差为0.12。

[我问的是这个问题,因为我找不到答案,但是我把它解决了,并想发布一个对别人有用的解决方案。当然,欢迎提供额外的帮助。 : - )]

3 个答案:

答案 0 :(得分:6)

见Ioannis Kosmidis的profileModel package。他在R Journal(R News中会出现)中有一篇论文说明了这个方案:

Ioannis Kosmidis。 profilemodel R包:具有线性预测变量的模型的分析目标。 R News,8(2):12-18,2008年10月。

PDF是here(整篇简报)。

答案 1 :(得分:5)

请参阅?profile.glm包中的example("profile.glm")(以及MASS) - 我认为它会执行您想要的所有操作(默认情况下不会加载,但会在{{ 1}},这可能是你看的第一个......)(请注意,配置文件通常绘制在有符号的方形根刻度上,因此真正的二次曲线将显示为一条直线。)

答案 2 :(得分:0)

我发现这样做的方法涉及使用offset()函数(详见Pawitan,Y。(2001)'In All Likelihood'p172)。 @BenBolker和@GavinSimpson给出的答案比这更好,因为他们引用的包将完成这一切以及更多的工作。 我发布这个因为它的另一种方式,并且,“手动”绘制事物有时候很适合学习。它教会了我很多。

sexi <- as.numeric(data.frame$sex)-1      #recode a factor as 0/1 numeric

beta <- numeric(60)              #Set up vector to Store the betas
deviance <- numeric(60)          #Set up vector to Store the deviances

for (i in 1:60){

  beta[i] <- 0.5 - (0.01*i)  
  #A vector of values either side of the fitted MLE (in this case -0.22)

  mod <- update(model,
                   .~. - sex             #Get rid of the fitted variable
                   + offset(   I(sexi*beta[i])   )   #Replace with offset term.
                )
  deviance[i] <- mod$deviance                        #Store i'th deviance
}

best <- which.min(deviance)                   
#Find the index of best deviance. Should be the fitted value from the model

deviance0 <- deviance - deviance[best]         
#Scale deviance to zero by subtracting best deviance

betahat <- beta[best]    #Store best beta. Should be the fitted value.
stderror <- 0.12187      #Store the std error of sex, found in summary(model)

quadratic <- ((beta-betahat)^2)*(1/(stderror^2))  
#Quadratic reference function to check quadratic assumption against

x11()                                    
plot(beta,deviance0,type="l",xlab="Beta(sex)",ylim=c(0,4))    
lines(beta,quadratic,lty=2,col=3)           #Add quadratic reference line
abline(3.84,0,lty=3)                #Add line at Deviance = 3.84
相关问题