Stephen Choularton
2005-Jan-05 19:34 UTC
[R] plotting percent of incidents within different 'bins'
Hi Say I have some data, two columns in a table being a binary outcome plus a predictor and I want to plot a graph that shows the percentage positives of the binary outcome within bands of the predictor, e.g. Outcome predictor 0 1 1 2 1 2 0 3 0 3 0 2 1 3 1 4 1 4 0 4 0 4 0 4 etc In this case there are 4 cases in the band 1 - 2 of the predictor, 2 of them are true so the percent is 50% and there are 7 cases in the band 3 - 4, 3 of which are true making the percentage 43% . Is there some function in R that will sum these outcomes by bands of predictor and produce a one by two data set with the percentages in one column and the ordered bands in the other, or alternately is there some sort of special plot.???? that does it all for you? Thanks Stephen [[alternative HTML version deleted]]
BXC (Bendix Carstensen)
2005-Jan-05 20:47 UTC
[R] plotting percent of incidents within different 'bins'
You want: tapply( Outcome, predictor, mean ) Bendix Carstensen ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc at steno.dk www.biostat.ku.dk/~bxc ----------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Stephen Choularton > Sent: Wednesday, January 05, 2005 8:35 PM > To: R Help > Subject: [R] plotting percent of incidents within different 'bins' > > > Hi > > Say I have some data, two columns in a table being a binary > outcome plus a predictor and I want to plot a graph that > shows the percentage positives of the binary outcome within > bands of the predictor, e.g. > > > Outcome predictor > > 0 1 > 1 2 > 1 2 > 0 3 > 0 3 > 0 2 > 1 3 > 1 4 > 1 4 > 0 4 > 0 4 > 0 4 > etc > > In this case there are 4 cases in the band 1 - 2 of the > predictor, 2 of them are true so the percent is 50% and there > are 7 cases in the band 3 > - 4, 3 of which are true making the percentage 43% . > > Is there some function in R that will sum these outcomes by > bands of predictor and produce a one by two data set with > the percentages in one column and the ordered bands in the > other, or alternately is there some sort of special plot.???? > that does it all for you? > > Thanks > > Stephen > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read > the posting guide! http://www.R-project.org/posting-guide.html >
james.holtman@convergys.com
2005-Jan-05 21:04 UTC
[R] plotting percent of incidents within different 'bins'
You can use 'cut' to create the breaks.. Actually there are 8 in the 3-4 range: Outcome predictor 1 0 1 2 1 2 3 1 2 4 0 3 5 0 3 6 0 2 7 1 3 8 1 4 9 1 4 10 0 4 11 0 4 12 0 4> cut(x.1$p, breaks=c(0,2,4))[1] (0,2] (0,2] (0,2] (2,4] (2,4] (0,2] (2,4] (2,4] (2,4] (2,4] (2,4] (2,4] Levels: (0,2] (2,4]> x.c <- cut(x.1$p, breaks=c(0,2,4)) > tapply(x.1$O, x.c, function(x)sum(x==1)/length(x))(0,2] (2,4] 0.500 0.375>__________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Office of Technology, Convergys james.holtman at convergys.com +1 (513) 723-2929 "Stephen Choularton" <mail at bymouth.com> To: "R Help" <r-help at stat.math.ethz.ch> Sent by: cc: r-help-bounces at stat.m Subject: [R] plotting percent of incidents within different 'bins' ath.ethz.ch 01/05/2005 14:34 Hi Say I have some data, two columns in a table being a binary outcome plus a predictor and I want to plot a graph that shows the percentage positives of the binary outcome within bands of the predictor, e.g. Outcome predictor 0 1 1 2 1 2 0 3 0 3 0 2 1 3 1 4 1 4 0 4 0 4 0 4 etc In this case there are 4 cases in the band 1 - 2 of the predictor, 2 of them are true so the percent is 50% and there are 7 cases in the band 3 - 4, 3 of which are true making the percentage 43% . Is there some function in R that will sum these outcomes by bands of predictor and produce a one by two data set with the percentages in one column and the ordered bands in the other, or alternately is there some sort of special plot.???? that does it all for you? Thanks Stephen [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html