r - Combine logistic regression with bar graph for maturity results -


i trying present results of logistic regression analysis maturity schedule of fish species. below reproducible code.

#coded r version r version 3.0.2 (2013-09-25) #frisbee sailing  rm(list=ls()) library(ggplot2) library(fsa) #generate sample data 1 mature, 0 non mature m<-rep(c(0,1),each=25) tl<-seq(31,80, 1) dat<-data.frame(m,tl) # add non mature individuals @ random in middle of df #prevent glm.fit: fitted probabilities numerically 0 or 1 occurred error tl<-sample(50:65, 15) m<-rep(c(0),each=15) dat2<-data.frame(tl,m) #final dataset data3<-rbind(dat,dat2) 

ggplot can produce logistic regression graph showing each of data points employed, following code:

#plot logistic model ggplot(data3, aes(x=tl, y=m)) +     stat_smooth(method="glm", family="binomial", se=false)+    geom_point()  

i want combine probability of being mature @ given size, obtained, , plotted following code:

#plot proportion of mature  #clump data in 5 cm size classes l50<-lencat(~tl,data=data3,startcat=30,w=5) #table of frequency of mature individuals size mat<-with(l50, table(lcat, m)) #proportion of mature mat_prop<-as.data.frame.matrix(prop.table(mat, margin=1)) colnames(mat_prop)<-c("nm", "m") mat_prop$tl<-as.factor(seq(30,80, 5)) # bar plot probability mature  ggplot(mat_prop, aes(x=tl,y=m)) +  geom_bar(stat="bin") 

what i've been trying do, no success, make graph combines both, since axis same should straightforward, cant seem make t work. have tried:

ggplot(mat_prop, aes(x=tl,y=m)) +  geom_bar(stat="bin")+  stat_smooth(method="glm", family="binomial", se=false) 

but not work. appreciated. new not able add resulting graphs post.

i see 3 problems code:

  • using stat="bin" in geom_bar() inconsisten giving values y-axis (y=m). if bin, count number of x-values in interval , use count y-value, there no need map data y-axis.

  • the data glm-plot in data3, combined plot uses mat_prop.

  • the x-axis of 2 plots acutally not quite same. in bar plot, use factor variable on x-axis, making axis discrete, while in glm-plot, use numeric variable, leads continuous x-axis.

the following code gave graph combining 2 plots:

mat_prop$tl<-seq(30,80, 5) ggplot(mat_prop, aes(x=tl,y=m)) +    geom_bar(stat="identity") +    geom_point(data=data3) +    geom_smooth(data=data3,aes(x=tl,y=m),method="glm", family="binomial", se=false) 

i run after first sourcing script define variables. 3 problems mentioned above adressed follows:

  • i used geom_bar(stat="identity") in order not use binning in bar plot.

  • i use data-argument in geom_point , geom_smooth in order use correct data (data3) these parts of plot.

  • i redifine mat_prop$tl make numeric. consistent column tl in data3, numeric well.

(i added points. if don't want them, remove geom_point(data=data3).)

the plot looks follows:

enter image description here

does lead plot wanted? let me know if need change something.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -