R语言画森林图展示Logistic回归分析的结果

第一步是准备数据

森林图展示的数据通常是Logistic回归分析的系数和95%置信区间以及显著性检验的P值,那么如何获得这些结果呢?

logistic回归分析的代码

data(Affairs,package = "AER")
df<-Affairs
df$ynaffairs<-ifelse(df$affairs>0,1,0)
df$ynaffairs<-factor(df$ynaffairs,
                     levels = c(0,1),
                     labels = c("No","Yes"))
fit.full<-glm(ynaffairs~gender+age+yearsmarried+
                children+religiousness+education+occupation+rating,
              data=df,family = binomial())

fit.result<-summary(fit.full)
df1<-fit.result$coefficients
df2<-confint(fit.full)
df3<-cbind(df1,df2)
df4<-data.frame(df3[-1,c(1,4,5,6)])
df4$Var<-rownames(df4)
colnames(df4)<-c("OR","Pvalue","OR_1","OR_2","Var")
df5<-df4[,c(5,1,2,3,4)]
df5$OR_mean<-df5$OR
df5$OR<-paste0(round(df5$OR,2),
               "(",
    

你可能感兴趣的:(R语言大学作业,r语言)