Have you ever heard of confidence intervals? Probably. Have you ever made them? Probably not. If you're like me, you studied confidence intervals in Stats 101, you're convinced of their importance, but when it comes to plotting data they have always seemed like too much trouble.

Getting into R, the popular language for computing statistics, I thought there would be some built-in function to make it easy -- maybe plot.with.confidence. No such luck. But I discovered it is easy in R *if* you learn how to use the language to its best advantage.

Below is my solution for binomial data. It is comes from an analysis of clicks and impressions over time. The code could easily be adapted for other distributions, like a normal distribution.

First, here is a plotting function to draw the statistic of interest plus lines above and below for confidence.

plot.conf = function(x, y, upper, lower, ...) {
    plot(x, y, type='l', lwd=2, ylim=c(min(lower), mean(y) + 3*sd(y)), ...)
    lines(x, upper, type='l', lty=1, lwd=.5)
    lines(x, lower, type='l', lty=1, lwd=.5)
}

Notice the "..." for variable keyword arguments (similar to Python's **kwargs). Also notice the ylimits are based on the y values. Confidence intervals can become vary wide and scale a graph beyond comprehension.

Now the part that calculates the upper and lower confidence lines. The way to get a binomial confidence interval in R is to perform a binomial test. This returns an object which has a confidence interval attached, a common pattern in R.

We can loop implicitly over all our data points because R is a functional language.

CI = mapply(
    function(s, t) {
        b = binom.test(s, t, conf.level=0.95)
        b$conf.int[1:2]
    },
    successes, trials
)

Mapply will apply a function of x parameters to x vectors. The return value(s) are put in a matrix. Remember that R will return the last expression in a function as its value.

Finally we call our plotting function with the appropriate arguments.

plot.conf(xvalues, successes / trials, CI[2,], CI[1,], main='Plot with confidence')

 


Comments

01/21/2009 20:38

Doesn't plotCI do a lot of this?

Reply
Greg Dingle
01/22/2009 10:39

@Jesse: I didn't know about plotCI or gplots in general -- thanks! But it looks like it plots the CI as error bars -- not as nice for a time series as in my example.

Reply
R
12/17/2009 14:16

Cool site

Reply
08/20/2010 17:43

stray birds of summer come to my window to sing and fly away.
and yellow leaves of autumn, which have no songs, flutter and fall
there with a sign.

Reply
09/01/2010 19:21

To love someone in secret is like a seed in bottle waiting for growing up, though not sure whether the future will be more beautiful, still waiting it earnestly and eagerly.

Reply
09/17/2010 17:20

You know it's easier to get killed by a terrorist than get married over the age of 40.《Sleepless in Seattle》

Reply
09/18/2010 01:47

No morality can be founded on authority, even if the authority were divine. (A. J. Ayer. USA writer)

Reply
10/18/2010 00:02

If identical affection cannot be, allow the a terrific offer more loving be me.

Reply
10/21/2010 03:07

Good friend is a person is. Pals can indomitable spirit would be the friend’s companion. Rain, Had an associate can stir. Prosperity isn't going to essentially is a pal, but an associate is prosperity. So I concur with you, we have got to pleasant to our close friends. I'm a Nike shoes lover. If you desire to purchase, you possibly can arrive to right here Nike Shox NZ selected what you like.

Reply
11/02/2010 01:25

That is a wonderful piece of writing, I observed your blog page internet site searching aol for any equivalent topic and came to this. I couldnt get to a lot other info and information on this weblog submit, so it was good to discover this 1. I'll finish up becoming returning to look at another articles or web site posts that you've yet another time.

Reply
11/03/2010 02:50

Firstly,thanks for your share of your experience and happy.so I hope you could do better and keep show it in your blog.I like this blog,and may often attention it.

Reply
11/07/2010 23:29

This is a great post and thank you for sharing this nice experience,and hope you can give another posts as soon as possible.

Reply
11/07/2010 23:35

Well , the view of the passage is totally correct ,your details is really reasonable and you give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum!

Reply
11/07/2010 23:42

This is a very good idea! Just want to say thank you for the information, you have to share. Just continue to write such a position. I will be your faithful reader. Thank you again.

Reply
11/16/2010 00:01

Good post. This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. I am sure this post has helped me save many hours of browsing other similar posts just to find what I was looking for. I just want to say: Thank you!

Reply

The greatest happiness is to work for education.

Reply
11/25/2010 00:05

I have a great admiramation for you,you know since your ariticle is fluent and of enough content expression.

Reply
11/26/2010 17:30

This is a responsibility of love and protection!

Reply
11/26/2010 21:54

That which is striking and beautiful is not always good, but that which is good is always beautiful.*

Reply

Happiness depends on our attitudes to life

Reply
12/05/2010 22:11

I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing ability has inspired me. Really the article is spreading its wings rapidly.

Reply
12/09/2010 23:35

This is a great post and thank you for sharing this nice experience,and hope you can give another posts as soon as possible.

Reply
12/10/2010 18:00

Christmas, children, is not a date. It is a state of mind.

Reply
12/10/2010 22:05

But this is an experience of mind, there is no imbalance!

Reply
12/17/2010 23:49

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading.Nice blog,I will keep visiting this blog very often.

Reply
12/23/2010 19:14

You have great blog and this post is good! *

Reply
12/30/2010 23:00

I can't believe how much of this I just wasn't aware of. Thank you for bringing more information to this topic for me. I'm truly grateful and really impressed.

Reply
01/12/2011 23:23

This is nice post which I was awaiting for such an artice and I have gained some useful information from this site.

Reply
01/18/2011 20:40

A certain amount of care or pain or trouble is necessary for every man at all times.
A ship without a ballast is unstable and will not go straight. Do you think so?

Reply
02/16/2011 17:38

This article is very useful, I have been looking for, thank you.Very good!

Reply
02/20/2011 19:39

*The city has had a long tradition of holding one of the grandest lantern festivals in China and its workers are in hot demand to stage lantern festivals throughout China and the rest of Asia.

Reply
02/21/2011 17:21

Expericence is a hard teacher because she gives the test first, the lesson afterwards.

Reply
02/23/2011 18:06

Pretty good post.I found your website perfect for my needs. thanks for sharing the great ideas.

Reply
02/23/2011 23:30

Enjoy your blog very much. I have read your chest on frame article noticed you make your own forged nails from common nails. I wonder if you might tell how you do this in a future blog.

Reply
02/24/2011 17:08

you can find what you want to in here,make your life full of color ,it's a perfect website.

Reply
02/25/2011 16:44

you can find what you want to in here,make your life full of color ,it's a perfect website.

Reply
02/25/2011 18:36

Retro jordan shoes stand out the jordan shoes

Reply
02/27/2011 19:16

You distributed several thoughts and also intention weblog, I yet seriously observed your internal ideas, you would like to provide to the current society, surely is actually my opinion, I've admitted, I additionally respect you.

Reply
02/28/2011 18:02

Well, I think that at present, the most important quality is the ability to deal with other people. I am poor in this aspect.

Reply
02/28/2011 18:11

Well worth to read this article, thanks for sharing this information. With this article you offered me got a chance to know about this, anyway i say Great Article! and waiting for you next article about this interesting subject.

Reply
03/01/2011 18:48

Well, How time flies! Spring is around the corner.

Reply
03/03/2011 17:53

I would like to thank you for the efforts you've got made in writing this submit. I am hoping the exact same greatest function from you inside the future too. Actually your creative writing abilities has inspired me to begin my own BlogEngine weblog now. Seriously the blogging is spreading its wings rapidly. Your write up is often a fine example of it.

Reply
03/06/2011 17:19

The shoe has great breathability, and they look great.

Reply
03/07/2011 01:23

you can find what you want to in here,make your life full of color ,it's a perfect website.

Reply
03/07/2011 18:11

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.

Reply
03/07/2011 18:12

This is a great post and thank you for sharing this nice experience,and hope you can give another posts as soon as possible.

Reply
03/07/2011 22:52

Thank you for this website. Thats all I can say. You most definitely have made this blog into something thats eye opening and important. You clearly know so much about the subject, youve covered so many bases. Great stuff from this part of the internet. Again, thank you for this website.

Reply
03/07/2011 23:30

you can find what you want to in here,make your life full of color ,it's a perfect website.

Reply
03/08/2011 00:01

Your blog is so great . I'd like to read the information about it, so how does it work? Do you want to know the MBT shoes ? MBT shoes on hot sale !

Reply
03/09/2011 23:35

Thank you for this website. Thats all I can say. You most definitely have made this blog into something thats eye opening and important. You clearly know so much about the subject, youve covered so many bases. Great stuff from this part of the internet. Again, thank you for this website.

Reply
03/14/2011 19:54

It's a nice post!Thanks for your good work,it helps me a lot!I will come back again!

Reply
03/21/2011 21:03

Very interesting information, as in previous posts. excellent article and well written! Thank you for sharing

Reply
03/23/2011 18:05

Just need to say your article is striking. The clearness inside your article is merely spectacular and i can take for granted you might be an expert on this field. Effectively with your permission enable me to grab your rss feed to retain up to date with forthcoming article. Thank you a million and please preserve up the efficient function.

Reply
03/26/2011 09:03

It's so hard for my stupid brain. Try, really try to understand it, but unsucessfull

Reply
04/01/2011 02:44

Well I know that a confidence interval with a particular confidence level is intended to give the assurance that, if the statistical model is correct, then taken over all the data that might have been obtained

Reply
04/11/2011 13:06

can u help me with getting the tweet and facebook blogs

Reply
04/11/2011 13:08

ass

Reply
04/12/2011 13:31

please

Reply
04/18/2011 04:48


Business & Laptop Bags
<a href="http://www.amandamall.com/business-laptop-bags_c315">Business & Laptop Bags </a>
Digital Gear & Camera Bags
<a href="http://www.amandamall.com/digital-gear-camera-bags_c317">Digital Gear & Camera Bags </a>
Backpacks
<a href="http://www.amandamall.com/backpacks_c319">Backpacks </a>
Luggage
<a href="http://www.amandamall.com/luggage_c321">Luggage</a>
Wallets & Holders
<a href="http://www.amandamall.com/wallets-holders_c322">Wallets & Holders </a>
Bag Parts & Accessories
<a href="http://www.amandamall.com/bag-parts-accessories_c324">Bag Parts & Accessories </a>
Special Purpose Bags & Cases
<a href="http://www.amandamall.com/special-purpose-bags-cases_c326">Special Purpose Bags & Cases </a>
Thank you for your Order very much. You are a good partner. We wish our next cooperation.

Reply
04/19/2011 18:38

I really like this website , and hope you will write more ,thanks a lot for your information.

Reply
04/26/2011 19:20

appear. They also supply the extra assistance that gives a heavier and bigger busted girl that additional bit of self self-confidence.A great deal of females

Reply
04/28/2011 23:32

Hello you its just i do extremely considerably like your original weblog, It really is so fortunate to study your posting, from this I can get some information that I didn't know prior to. Thank you for the shaving! I give you limitless brand-new exceptional wishes. Please accept them as my greetings.

Reply
06/08/2011 00:59

When it comes to blatant lies, there are none more egregious than budget figures.Time spent agonizing over them is time wasted. Even if (miracle of miracles!) yours are honest and accurate, no one else will have been so foolish.

Reply
10/05/2011 20:53

Belstaff jackets is a leading make of motorcycle jackets and also the motorcycle jackets within the Belstaff range are finished with best quality.And the belstaff coat are just about the most well-known belstaff leather inside thegood reputation for motorcycling and also the version belstaff jacket produce today is without a doubt a lot better than the main whilst still maintains the classic look. The north face jackets can be component of the north face collection and is also another good quality north face jacket with classic style. Here, in our store ,we show the picture, display the north face jackets in gentle black. http://www.northface-uk.net

Reply
11/21/2011 01:47

Thank you for this article.but i don't agree you idea

Reply
02/12/2012 23:32

This is a great inspiring piece of writing. I am beautiful to a large extent content by means of your high-quality work. You put in fact awfully obliging information. be it up. be blogging Looking to appraisal your subsequently post.

Reply
05/22/2012 01:35

A girl can generally really feel alluring irrespective with the simple fact exactly where she is. She can appreciate on the cruise, a beach, relax inside of a Jacuzzi or relax at a swimming pool party with Wholesale Sexy Swimwear; she will make heads turn when she wears a bikini. Make girls become sexier and more charming with Wholesale Sexy Bikini.

Reply
07/10/2012 17:52

In fact, I have been concerned about your blog, I very much appreciate your literary talent, some of your thoughts and ideas a little bit the same with me, so I have been paying attention to you, looking forward to your published more articles saysay that your views, I support you.

Reply
07/10/2012 17:54

Inadvertently into your blog, the first impression to my surprise, I have been looking for a bar blog as my reference, you want to learn some good experience, your blog is very suitable for me.Thank you very much.

Reply
07/10/2012 17:56

Your literary talent fascinated me, and writing is really great, in short, is really good, useful information, I think that should allow more people to pay attention to your blog, with the support you.

Reply



Leave a Reply