简体   繁体   English

具有第一差异的系数图

[英]coefficient plot with first differences

I am running a logistic model in r. 我在r中运行逻辑模型。 I am trying to present the differences in probabilities for my independent variables with a coefficient plot. 我试图用系数图表示我自变量的概率差异。 Specifically, I would like to create the differences in probabilities by shifting the variables of interest from their minimum to their maximum value (while holding the other variables at their means or modes). 具体来说,我想通过将感兴趣的变量从其最小值移至最大值(同时使其他变量保持其均值或众数)来创建概率差异。

In the picture attached, I would like for my graph to look similar to the top half. 在所附图片中,我希望图形看起来与上半部分相似。 在此处输入图片说明

I have ran this code: 我已经运行了这段代码:

 mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
 mylogit <- glm(admit ~ gre + gpa + rank, data = mydata, family =
 "binomial")

Afterwards I calculated the predicted probability for each variable for the minimum value and the maximum value and subtracted the two. 然后,我计算出每个变量的最小值和最大值的预测概率,然后将二者相减。 I repeated this process for the upper and lower bounds of the interval. 我对间隔的上限和下限重复了此过程。 Attached is my code 附上我的代码

plotdat <- data.frame(gre=c(.220, 800), gpa=mean(mydata$gpa, na.rm=TRUE), rank=c(2) ) 
preddat <- predict(mylogit, newdata=plotdat, se.fit=TRUE)

Grebeta<-(exp(preddat$fit[2])/(1+exp(preddat$fit[2])))-(exp(preddat$fit[1])/(1+exp(preddat$fit[1])))
Gremin<-(exp(preddat$fit[2]+1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]+1.96*preddat$se.fit[2])))-exp(preddat$fit[1]+1.96*preddat$se.fit[1])/(1+exp(preddat$fit[1]+1.96*preddat$se.fit[1]))
Gremax<-exp(preddat$fit[2]-1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]-1.96*preddat$se.fit[2]))-exp(preddat$fit[1]-1.96*preddat$se.fit[1])/(1+exp(preddat$fit[1]-1.96*preddat$se.fit[1]))


plotdat <- data.frame(gpa=c(2.26, 4), gre=mean(mydata$gre, na.rm=TRUE), rank=c(2) )
preddat <- predict(mylogit, newdata=plotdat, se.fit=TRUE)

GPAbeta<-(exp(preddat$fit[2])/(1+exp(preddat$fit[2])))-(exp(preddat$fit[1])/(1+exp(preddat$fit[1])))
GPAmin<-(exp(preddat$fit[2]+1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]+1.96*preddat$se.fit[2])))-exp(preddat$fit[1]+1.96*preddat$se.fit[1])/(1+exp(preddat$fit[1]+1.96*preddat$se.fit[1]))
GPAmax<-exp(preddat$fit[2]-1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]-1.96*preddat$se.fit[2]))-exp(preddat$fit[1]-1.96*preddat$se.fit[1])/(1+exp(preddat$fit[1]-1.96*preddat$se.fit[1]))


plotdat <- data.frame(rank=c(4, 1), gre=mean(mydata$gre, na.rm=TRUE), gpa=mean(mydata$gpa, na.rm=TRUE ))
preddat <- predict(mylogit, newdata=plotdat, se.fit=TRUE)

Rankbeta<-(exp(preddat$fit[2])/(1+exp(preddat$fit[2])))-(exp(preddat$fit[1])/(1+exp(preddat$fit[1])))
Rankmin<-(exp(preddat$fit[2]+1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]+1.96*preddat$se.fit[2])))-exp(preddat$fit[1]+1.96*preddat$se.fit[1])/(1+exp(preddat$fit[1]+1.96*preddat$se.fit[1]))
Rankmax<-exp(preddat$fit[2]-1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]-1.96*preddat$se.fit[2]))-exp(preddat$fit[1]-1.96*preddat$se.fit[1])/(1+exp(preddat$fit[1]-1.96*preddat$se.fit[1]))

Afterwards, I created three vectors containing the difference in probabilities and bands. 之后,我创建了三个包含概率和谱带差异的向量。 Attached is my code: 附上我的代码:

se.max<- c(Gremax   , GPAmax  , Rankmax  )
coef.vec<- c( Grebeta  ,GPAbeta  , Rankbeta ) 
se.min<-c(Gremin , GPAmin, Rankmin)



var.names <- c("gre", "gpa", "rank")

Finally, I plotted my graph. 最后,我绘制了图表。

y.axis <- c(length(coef.vec):1)

par(mar=c(2, 13, 0, 0))

plot(coef.vec, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19, cex = 1.2,  xlim = c(-2,2.5), xaxs = "r", main = "")

segments(se.max, y.axis,se.min, y.axis, lwd =  1.5)

axis(1, at = seq(-1,1,by=.25), labels = NA, tick = T,cex.axis = 1.2, mgp = c(2,.7,0))
axis(1, at = seq(-1,1,by=.5), labels =  c(-1,  -.5,  0, .5,1), tick = T,cex.axis = 1.2, mgp = c(2,.7,0))

axis(2, at = y.axis, label = var.names, las = 1, tick = T, ,mgp = c(2,.6,0), cex.axis = 1.2)
segments(0,0,0,17,lty=2)

However, I can't get my confidence intervals to plot. 但是,我无法绘制出置信区间。 Attached below is my final output. 以下是我的最终输出。

在此处输入图片说明

It appears my confidence bands won't plot. 看来我的信任范围不会策划。 If anyone could provide assistance and point to errors in my calculations or code, I would greatly appreciate it. 如果有人可以提供帮助并指出我的计算或代码中的错误,我将不胜感激。

plotdat <- data.frame(gre=c(.220, 800), gpa=mean(mydata$gpa, na.rm=TRUE), rank=c(2) ) 
preddat <- predict(mylogit, newdata=plotdat, se.fit=TRUE)

#GRE High
GREbetahigh<-(exp(preddat$fit[2])/(1+exp(preddat$fit[2])))
GREminhigh<-(exp(preddat$fit[2]+1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]+1.96*preddat$se.fit[2])))
GREmaxhigh<-exp(preddat$fit[2]-1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]-1.96*preddat$se.fit[2]))


#GRE low
GREbetalow<-(exp(preddat$fit[1])/(1+exp(preddat$fit[1])))
GREminlow<-(exp(preddat$fit[1]+1.96*preddat$se.fit[2])/(1+exp(preddat$fit[1]+1.96*preddat$se.fit[1])))
GREmaxlow<-exp(preddat$fit[1]-1.96*preddat$se.fit[2])/(1+exp(preddat$fit[1]-1.96*preddat$se.fit[1]))

#GRE Diff
GREbeta.diff<-GREbetahigh-GREbetalow
GREmax.diff<-GREmaxhigh-GREmaxlow
GREmin.diff<-GREminhigh-GREminlow

#GPA
plotdat <- data.frame(gpa=c(2.26, 4), gre=mean(mydata$gre, na.rm=TRUE), rank=c(2) )
preddat <- predict(mylogit, newdata=plotdat, se.fit=TRUE)

#GPA high
GPAbetahigh<-(exp(preddat$fit[2])/(1+exp(preddat$fit[2])))
GPAminhigh<-(exp(preddat$fit[2]+1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]+1.96*preddat$se.fit[2])))
GPAmaxhigh<-exp(preddat$fit[2]-1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]-1.96*preddat$se.fit[2]))

#GPA low
GPAbetalow<-(exp(preddat$fit[1])/(1+exp(preddat$fit[1])))
GPAminlow<-(exp(preddat$fit[1]+1.96*preddat$se.fit[2])/(1+exp(preddat$fit[1]+1.96*preddat$se.fit[1])))
GPAmaxlow<-exp(preddat$fit[1]-1.96*preddat$se.fit[2])/(1+exp(preddat$fit[1]-1.96*preddat$se.fit[1]))

#GPA Diff
GPAbeta.diff<-GPAbetahigh-GPAbetalow
GPAmax.diff<-GPAmaxhigh-GPAmaxlow
GPAmin.diff<-GPAminhigh-GPAminlow

#Rank

plotdat <- data.frame(rank=c(4, 1), gre=mean(mydata$gre, na.rm=TRUE), gpa=mean(mydata$gpa, na.rm=TRUE ))
preddat <- predict(mylogit, newdata=plotdat, se.fit=TRUE)

#Rank high
Rankbetahigh<-(exp(preddat$fit[2])/(1+exp(preddat$fit[2])))
Rankminhigh<-(exp(preddat$fit[2]+1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]+1.96*preddat$se.fit[2])))
Rankmaxhigh<-exp(preddat$fit[2]-1.96*preddat$se.fit[2])/(1+exp(preddat$fit[2]-1.96*preddat$se.fit[2]))

#Rank Low
Rankbetalow<-(exp(preddat$fit[1])/(1+exp(preddat$fit[1])))
Rankminlow<-(exp(preddat$fit[1]+1.96*preddat$se.fit[1])/(1+exp(preddat$fit[1]+1.96*preddat$se.fit[1])))
Rankmaxlow<-exp(preddat$fit[1]-1.96*preddat$se.fit[1])/(1+exp(preddat$fit[1]-1.96*preddat$se.fit[1]))


#Rank Diff
Rankbeta.diff<-Rankbetahigh-Rankbetalow
Rankmax.diff<-Rankmaxhigh-Rankmaxlow
Rankmin.diff<-Rankminhigh-Rankminlow

#Graph
se.max<- c(GREmax.diff   , GPAmax.diff, Rankmax.diff)
coef.vec<- c( GREbeta.diff , GPAbeta.diff, Rankbeta.diff)
se.min<-c(GREmin.diff , GPAmin.diff, Rankmin.diff)

var.names <- c("gre", "gpa", "rank")

y.axis <- c(length(coef.vec):1)

par(mar=c(2, 13, 0, 0))


plot(y.axis, coef.vec, type = "p", axes = F, xlab = "", ylab = "", pch = 19, cex = 1.2,  ylim = c(-1,1), xlim=c(1,3.3), xaxs = "r", main = "")
segments(y.axis, se.max,y.axis, se.min, lwd =  1.5)

axis(2, at = seq(-1,1,by=.25), labels = NA, tick = T,cex.axis = 1.2, mgp = c(2,.7,0))
axis(2, at = seq(-1,1,by=.5), labels =  c(-1,  -.5,  0, .5, 1), tick = T,cex.axis = 1.2, mgp = c(2,.7,0))

axis(1, at = y.axis, label = var.names, las = 1, tick = T, ,mgp = c(2,.6,0), cex.axis = 1.2)
segments(1,0,3.3,0,lty=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM