# R语言基础入门之六：Logistic回归

• A+

``` library(DAAG) head(anesthetic) cdplot(factor(nomove)~conc,data=anesthetic,main='条件密度图',ylab='病人移动',xlab='麻醉剂量') ```

``` anes1=glm(nomove~conc,family=binomial(link='logit'),data=anesthetic) summary(anes1) ```

``` anestot=aggregate(anesthetic[,c('move','nomove')],by=list(conc=anesthetic\$conc),FUN=sum) anestot\$conc=as.numeric(as.character(anestot\$conc)) anestot\$total=apply(anestot[,c('move','nomove')],1,sum) anestot\$prop=anestot\$nomove/anestot\$total ```

conc move nomove total      prop
1  0.8    6      1     7 0.1428571
2  1.0    4      1     5 0.2000000
3  1.2    2      4     6 0.6666667
4  1.4    2      4     6 0.6666667
5  1.6    0      4     4 1.0000000
6  2.5    0      2     2 1.0000000

``` anes2=glm(cbind(nomove,move)~conc,family=binomial(link='logit'),data=anestot) anes3=glm(prop~conc,family=binomial(link='logit'),weights=total,data=anestot) ```

``` x=seq(from=0,to=3,length.out=30) y=predict(anes1,data.frame(conc=x),type='response') plot(prop~conc,pch=16,col='red',data=anestot,xlim=c(0.5,3),main='Logistic回归曲线图',ylab='病人静止概率',xlab='麻醉剂量') lines(y~x,lty=2,col='blue') ```