Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib

Basic drawing

For example, suppose you have a data frame like this.

    x         y
0   2  0.954025
1   3  0.146810
2   1  0.409961
3   1  0.164558
4   3  0.782152
5   2  0.905869
6   3  0.210528
7   1  0.437970
8   1  0.801206
9   3  0.089576
10  2  0.960357
11  2  0.670732

When I try to draw a diagram with standard error from this data frame, it looks like this.

import numpy as np
import matplotlib.pyplot as plt
 
m = df.pivot_table(index='x', values='y', aggfunc='mean')
e = df.pivot_table(index='x', values='y', aggfunc='sem')
m.plot(xlim=[0.8, 3.2], yerr=e)

20161010_1.png

In this way, the error bar can be created by specifying the magnitude of the error in yerr.

Drawing using confidence intervals

Therefore, we define and use cilen to find the length of the confidence interval.

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt


def cilen(arr, alpha=0.95):
    if len(arr) <= 1:
        return 0
    m, e, df = np.mean(arr), stats.sem(arr), len(arr) - 1
    interval = stats.t.interval(alpha, df, loc=m, scale=e)
    cilen = np.max(interval) - np.mean(interval)
    return cilen

m = df.pivot_table(index='x', values='y', aggfunc='mean')
e = df.pivot_table(index='x', values='y', aggfunc=cilen)
m.plot(xlim=[0.8, 3.2], yerr=e)

20161010_2.png

I was able to create a diagram with confidence intervals.

bonus

The method of calculating the confidence interval is confusing due to the "n or n-1" problem.
For those who searched for "confidence interval Python" according to "Avoid reinventing the wheel",

  1. How to calculate the confidence interval of the mean of the normal distribution in Python
  2. keiskS @technote – Confidence interval with python

I think you'll be confused by the subtle differences, try both, and be frustrated by the different results.
1 is the same answer as R. The difference is in 2. I'll give it a try.

> x <- c(1, 1, 3, 3)
> t.test(x)

    One Sample t-test

data:  x
t = 3.4641, df = 3, p-value = 0.04052
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 0.1626138 3.8373862
sample estimates:
mean of x
        2

1 way

import numpy as np
from scipy import stats

alpha     = 0.95
data = [1, 1, 3, 3]

mean_val = np.mean(data)
sem_val  = stats.sem(data)  # standared error of the mean
ci       = stats.t.interval(alpha, len(data)-1, loc=mean_val, scale=sem_val)

print(ci)

>> (0.16261376896260105, 3.837386231037399)

2 methods

import math
import numpy as np
from scipy import stats

alpha     = 0.95
data = [1, 1, 3, 3]

mean_val = np.mean(data)
std_val = np.std(data)
ci = stats.t.interval(alpha,len(data)-1,loc=mean_val,scale=std_val/math.sqrt(len(data)))
print(ci)

>> (0.40877684735786857, 3.5912231526421312)

This time, I decided to follow R in the world, so I chose 1.
What is different about 2 is the end of the part where ci is calculated.

math.sqrt(len(data))

This. Divide by n. However, if you want to do inference statistics, it is better to divide by n-1. This is because we assume a t distribution. In fact,

math.sqrt(len(data) - 1)

Then, the answer of Method 2 also completely matches R.

Recommended Posts

Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib
Introduction to Python with Atom (on the way)
Add information to the bottom of the figure with Matplotlib
What I did to welcome the Python2 EOL with confidence
Save images on the web to Drive with Python (Colab)
[Python] Set the graph range with matplotlib
Download files on the web with Python
Add Gaussian noise to images with python2.7
Match the colorbar to the figure with matplotlib
The road to compiling to Python 3 with Thrift
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Use python on Raspberry Pi 3 to light the LED with switch control!
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
Strategy on how to monetize with Python Java
[Python] matplotlib: Format the diagram for your dissertation
Just add the python array to the json data
Drawing tips with matplotlib on the server side
Add diagonal lines (hatch) to heatmap (python, matplotlib)
The easiest way to synthesize speech with python
Try to solve the man-machine chart with Python
[Python] How to draw multiple graphs with Matplotlib
Specify the Python executable to use with virtualenv
Say hello to the world with Python with IntelliJ
[Python] How to save images on the Web at once with Beautiful Soup
I wanted to visualize 3D particle simulation with the Python visualization library Matplotlib.
The easiest way to use OpenCV with python
Get coordinate values and keyboard input values by clicking on the python / matplotlib diagram
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
Connect to MySQL with Python on Raspberry Pi
Visualize grib2 on a map with python (matplotlib)
An easy way to pad the number with zeros depending on the number of digits [Python]
Make it easy to install the ROS2 development environment with pip install on Python venv
Starting with Python 3.10, the form returned by inspect.signature () seems to be based on typing.get_type_hints ().
Think about how to program Python on the iPad
Try to solve the programming challenge book with python3
How to add help to HDA (with Python script bonus)
[Python] How to draw a line graph with Matplotlib
Just add the driver to the shape key with blender
[Introduction to Python] How to iterate with the range function?
I tried to implement Minesweeper on terminal with python
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
Steps to install the latest Python on your Mac
I tried to solve the soma cube with python
Yum command to access MySQL with Python 3 on Linux
[Python] How to specify the download location with youtube-dl
Make a breakpoint on the c layer with python
Convert the image in .zip to PDF with Python
I want to inherit to the back with python dataclass
Information for controlling the motor with Python on RaspberryPi
Install the latest stable Python with pyenv (both 2 and 3)
I want to AWS Lambda with Python on Mac!
Specify MinGW as the compiler to use with Python
I tried to solve the problem with Python Vol.1
[Python] How to rewrite the table style with python-pptx [python-pptx]
[Python] How to create a 2D histogram with Matplotlib
How to enjoy Python on Android !! Programming on the go !!
[Introduction to Python] Basic usage of the library matplotlib
[Python] How to draw a scatter plot with Matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib