R 如何使用ggplot2在对数奇数标度上绘制逻辑回归

R 如何使用ggplot2在对数奇数标度上绘制逻辑回归,r,ggplot2,logistic-regression,R,Ggplot2,Logistic Regression,我试图用对数奇数标度绘制逻辑回归的结果 load(url("https://github.com/bossaround/question/raw/master/logisticregressdata.RData")) ggplot(D, aes(Year, as.numeric(Vote), color = as.factor(Male))) + stat_smooth( method="glm", method.args=list(family="binomial"), formula

我试图用对数奇数标度绘制逻辑回归的结果

load(url("https://github.com/bossaround/question/raw/master/logisticregressdata.RData"))

ggplot(D, aes(Year, as.numeric(Vote), color = as.factor(Male))) +
  stat_smooth( method="glm", method.args=list(family="binomial"), formula = y~x + I(x^2), alpha=0.5, size = 1, aes(fill=as.factor(Male))) +
  xlab("Year") 
但这幅图的刻度是0~1。我猜这是概率表(如果我错了请纠正我)

我真正想要的是在对数奇数标度上绘制它,就像逻辑回归报告一样,然后将其转换为概率

理想情况下,我想在这样一个模型中,通过男性,在控制了外国投资后,绘制投票和年份之间的关系:

   Model <-  glm(Vote ~ Year + I(Year^2) + Male + Foreign, family="binomial", data=D)
ModelPredictions <- predict(Model , type="response")

Model要绘制具有多个变量的模型预测,应制作模型,根据新数据进行预测,以生成预测并绘制该预测

Model <-  glm(Vote ~ Year + I(Year^2) + Male + Foreign, family="binomial", data=D)
for_pred = expand.grid(Year = seq(from = 2, to = 10, by = 0.1), Male = c(0,1), Foreign = c(0,1)) #generate data to get a smooth line

for_pred = cbind(for_pred, predict(Model, for_pred, type = "link", se.fit= T)) 
#if the probability scale was needed: `type = "response`

library(ggplot2)
ggplot(for_pred, aes(Year, fit, color = as.factor(Male))) +
  geom_line() +
  xlab("Year")+
  facet_wrap(~Foreign)  + #important step - check also how it looks without it
  geom_ribbon(aes(ymax = fit + se.fit, ymin = fit - se.fit, fill = as.factor(Male)), alpha = 0.2) 

#omit the color by `color = NA` or by `inherit.aes = F` (if like this, one should provide the data and full `aes` mapping for  geom_ribbon). 
#If geom_ribbon should not have a mapping, specify `fill` outside of `aes` like: `fill = grey80`.

Model要绘制具有多个变量的模型预测,应制作模型,根据新数据进行预测,以生成预测并绘制该预测

Model <-  glm(Vote ~ Year + I(Year^2) + Male + Foreign, family="binomial", data=D)
for_pred = expand.grid(Year = seq(from = 2, to = 10, by = 0.1), Male = c(0,1), Foreign = c(0,1)) #generate data to get a smooth line

for_pred = cbind(for_pred, predict(Model, for_pred, type = "link", se.fit= T)) 
#if the probability scale was needed: `type = "response`

library(ggplot2)
ggplot(for_pred, aes(Year, fit, color = as.factor(Male))) +
  geom_line() +
  xlab("Year")+
  facet_wrap(~Foreign)  + #important step - check also how it looks without it
  geom_ribbon(aes(ymax = fit + se.fit, ymin = fit - se.fit, fill = as.factor(Male)), alpha = 0.2) 

#omit the color by `color = NA` or by `inherit.aes = F` (if like this, one should provide the data and full `aes` mapping for  geom_ribbon). 
#If geom_ribbon should not have a mapping, specify `fill` outside of `aes` like: `fill = grey80`.

Model从
broom()
使用
augmented()
进一步回答:

Model%重命名(对数赔率=`.fitted`,
性别=男性)
一瞥(df.1型)
意见:46398
变量:13
$行名“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“11”、“12”、“13”、“14”、“15”、“16”、“17”、“18”、“19”、“20”、“21”。。。
$VOUT 0,0,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0。。。
第二、三、四、五、二、三、二、三、四、五、六、二、二、三、四、二、三、三、四、四、五、五、六、七、八、九、二、三、四、五、二、三、三、四、五、二、三、四、五、五、五、五、六、七、八、九、二、三、四、五、五、二、二、三、三、四、五。。。
一美元一年。4,  9, 16, 25,  4,  9,  4,  9, 16, 25, 36,  4,  4,  4,  9, 16,  4,  9, 16, 25, 36, 49, 64, 81,  4,  9, 16, 2...
$Sex 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1。。。
$0、0、0、0、1、1、0、0、0、0、1、0、0、0、0、1、1、1、1、1、1、1、1、0、0、0、0、0、0、0、0、0、0、。。。
$log_赔率-0.01910985、-0.68184753、-1.14317053、-1.40307885、0.26930939、-0.39342829、-0.01910985、-0.68184753、-1.14317053。。。
$se.fit 0.01675017、0.01466136、0.01790972、0.02058514、0.01931826、0.01777691、0.01675017、0.01466136、0.01790972、0.0205。。。
美元残差-1.1693057,-0.9047053,-0.7439452,1.8016037,-1.2937083,1.3483961,-1.1693057,-0.9047053,-0.7439452,-0.66303。。。
$.hat 7.013561e-05、4.794678e-05、5.879536e-05、6.711744e-05、9.162739e-05、7.602458e-05、7.013561e-05、4.794678e-05、5。。。。
美元西格玛1.124879,1.124884,1.124886,1.124861,1.124876,1.124874,1.124879,1.124884,1.124886,1.124887,1.124860,1.12。。。
$.cooksd 1.376354e-05、4.849628e-06、3.749311e-06、5.461011e-05、2.399355e-05、2.253792e-05、1.376354e-05、4.849628e-06、3。。。。
美元标准差-1.1693467,-0.9047270,-0.7439671,1.8016642,-1.2937676,1.3484474,-1.1693467,-0.9047270,-0.7439671,-0.66305。。。
#想象
ggplot(df.1型,aes(年份,对数赔率,颜色=性别))+
几何线()
几何平滑(se=真)+
面_包裹(~外来)
其中:


使用
broom()
中的
augmented()
进一步回答:

Model%重命名(对数赔率=`.fitted`,
性别=男性)
一瞥(df.1型)
意见:46398
变量:13
$行名“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“11”、“12”、“13”、“14”、“15”、“16”、“17”、“18”、“19”、“20”、“21”。。。
$VOUT 0,0,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0。。。
第二、三、四、五、二、三、二、三、四、五、六、二、二、三、四、二、三、三、四、四、五、五、六、七、八、九、二、三、四、五、二、三、三、四、五、二、三、四、五、五、五、五、六、七、八、九、二、三、四、五、五、二、二、三、三、四、五。。。
一美元一年。4,  9, 16, 25,  4,  9,  4,  9, 16, 25, 36,  4,  4,  4,  9, 16,  4,  9, 16, 25, 36, 49, 64, 81,  4,  9, 16, 2...
$Sex 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1。。。
$0、0、0、0、1、1、0、0、0、0、1、0、0、0、0、1、1、1、1、1、1、1、1、0、0、0、0、0、0、0、0、0、0、。。。
$log_赔率-0.01910985、-0.68184753、-1.14317053、-1.40307885、0.26930939、-0.39342829、-0.01910985、-0.68184753、-1.14317053。。。
$se.fit 0.01675017、0.01466136、0.01790972、0.02058514、0.01931826、0.01777691、0.01675017、0.01466136、0.01790972、0.0205。。。
美元残差-1.1693057,-0.9047053,-0.7439452,1.8016037,-1.2937083,1.3483961,-1.1693057,-0.9047053,-0.7439452,-0.66303。。。
$.hat 7.013561e-05、4.794678e-05、5.879536e-05、6.711744e-05、9.162739e-05、7.602458e-05、7.013561e-05、4.794678e-05、5。。。。
美元西格玛1.124879,1.124884,1.124886,1.124861,1.124876,1.124874,1.124879,1.124884,1.124886,1.124887,1.124860,1.12。。。
$.cooksd 1.376354e-05、4.849628e-06、3.749311e-06、5.461011e-05、2.399355e-05、2.253792e-05、1.376354e-05、4.849628e-06、3。。。。
美元标准差-1.1693467,-0.9047270,-0.7439671,1.8016642,-1.2937676,1.3484474,-1.1693467,-0.9047270,-0.7439671,-0.66305。。。
#想象
ggplot(df.1型,aes(年份,对数赔率,颜色=性别))+
几何线()
几何平滑(se=真)+
面_包裹(~外来)
其中:


您的方法是正确的,但您需要使用您构建的模型预测值,如下所示:

   Model <-  glm(Vote ~ Year + I(Year^2) + Male + Foreign, family="binomial", data=D)
ModelPredictions <- predict(Model , type="response")

您的方法是正确的,但您需要使用您构建的模型预测值,如下所示:

   Model <-  glm(Vote ~ Year + I(Year^2) + Male + Foreign, family="binomial", data=D)
ModelPredictions <- predict(Model , type="response")

predict(Model,type=“link”,se.fit=TRUE)
将根据对数优势度进行预测
se.fit=TRUE
将包括预测的标准误差。预测将针对用于拟合模型的观测值。要获得自变量(IVs)其他值的预测,请使用predict的
newdata
参数,并包括一个新的数据框(您创建的)以及您希望预测结果的IV值(例如,
predict(Model,newdata=my_data_frame,type=“link”,se.fit=TRUE)
)1.如果你有预测,你