我正在寻找一些关于如何使用以下数据制作ggplot的帮助。 stackoverflow上有几个例子,但是它们要复杂得多,而且我很难搞清楚它。
library(car)
data(Chile)
Chile$yes <- with(Chile, ifelse(vote == "Y", 1, ifelse(vote=="N", 0, NA)))
Chile<-na.omit(Chile)
logit1 <- glm(yes ~ statusquo + age + income + sex, data = Chile, family=binomial(link="logit"))
inject1 <- data.frame(statusquo=mean(na.omit(Chile$statusquo)), income=mean(Chile$income), sex="F", age=mean(Chile$age))
predict1 <- predict(logit1, newdata=inject1, type="response", se.fit=TRUE)
inject2 <- data.frame(statusquo=mean(na.omit(Chile$statusquo)), income=mean(Chile$income), sex="M", age=mean(Chile$age))
predict2 <- predict(logit1, newdata=inject2, type="response", se.fit=TRUE)
结果变量是&#34;是&#34;,我认为x会在性别上有所不同 - 我想绘制预测。
提前感谢您的时间和帮助!
答案 0 :(得分:2)
对于典型的&#34; S&#34;形成逻辑回归的情节你需要更多的预测 而不是你目前正在使用的点预测
install.packages("caret")
library(car)
library(ggplot2)
data(Chile)
Chile$yes <- with(Chile, ifelse(vote == "Y", 1, ifelse(vote=="N", 0, NA)))
Chile<-na.omit(Chile)
为了获得更多预测,我们使用createDataPartition
包中的caret
函数将数据集拆分为训练和测试数据集
#split Chile dataset into train for fitting and test for prediction purpose
set.seed(42)
trainIndex = createDataPartition(Chile$yes, p=0.75,list=FALSE)
trainChile = Chile[trainIndex,]
testChile = Chile[-trainIndex,]
#fit logit model
fitLogit <- glm(yes ~ statusquo + age + income + sex, data = trainChile, family=binomial(link="logit"))
#predict on test data
predLogit <- predict(fitLogit, newdata=testChile, type="response", se.fit=TRUE)
绘图
我已将结果变量与一个预测变量联系起来&#34; statusquo&#34;。
您可以通过更改predictorVec
#if fit value is > 0.5, we consider voting outcome as "YES/1 else "NO"/0
predictorVec = c("statusquo","age","income","sex")
x = predictorVec[1]
plotObj= data.frame(predictor=testChile[,x],outcome=ifelse(predLogit$fit>0.5,1,0))
gg = ggplot(plotObj, aes(x=predictor, y= outcome)) + geom_point() +
stat_smooth(method="glm", method.args=list(family="binomial"), se=FALSE) +
ggtitle(paste0("Prediction for predictor:",x)) +
theme(plot.title = element_text(size=14, face="bold"))
print(gg)