21 Confidence interval for a mean with paired data#

%%html
<iframe width="700" height="400" src="https://www.youtube.com/embed/gWaA8mVBZQk/" frameborder="0" allowfullscreen></iframe>
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats, special

khanacademy

Confidence interval for a mean with paired data fig 1Confidence interval for a mean with paired data fig 2

df = DataFrame({
                'Jeff': [44, 35],
               'David': [42, 37],
               'Kim': [40, 32], 
               'Charlotte': [37, 31],
               'Jake': [42, 36]})
df = df.append(df.iloc[0] - df.iloc[1], ignore_index=True)
df['Participant'] = ['Dominant', 'Non Dominance', 'Difference (dom - non)']
df = df.set_index('Participant')

df2 = DataFrame()
df2['Mean'] = df.mean(axis=1)
df2['Standard deviation'] = df.std(ddof=1, axis=1)
df2['Sample standard error'] = df.sem(axis=1)
df2['Sample size'] = df.shape[1]
/tmp/ipykernel_9880/1464699692.py:7: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  df = df.append(df.iloc[0] - df.iloc[1], ignore_index=True)
df2
Mean Standard deviation Sample standard error Sample size
Participant
Dominant 41.0 2.645751 1.183216 5
Non Dominance 34.2 2.588436 1.157584 5
Difference (dom - non) 6.8 1.643168 0.734847 5
stats.t.interval(0.95,
                 df=df2.iloc[2]['Sample size'] - 1,
                 loc=df2.iloc[2]['Mean'],
                 scale=df2.iloc[2]['Sample standard error'])
(4.759737858025229, 8.84026214197477)