pythonpandasstatisticsdata-scienceusage-statistics

Construct a 95% confidence interval


A pharmacist wants to test whether a new kind of sleeping pill would be effective to increase the hours of sleep for people who take it. A random sample of 10 persons (Group A) was given the new pills and another random sample of 13 persons (Group B) was given the old pills.

Their sleep in hours were recorded as follows:

Mean of group A = 8.9 and Standard Deviation of group A = 0.8

Mean of Group B = 8.5 and standard Deviation of group B = 0.5

Construct a 95% confidence interval for the difference of the average hours of sleep that would be obtained for the people taking the new pills over the people taking the old pills. State the assumptions made.

How do I code this in python???


Solution

  • To compute confidence intervals of differene of means and if as You say assumptions are met then:

    from scipy import stats
    from math import sqrt
    from scipy.stats import t
    
    N1 = 10 #numbers of observations
    N2 = 13
    df = (N1 + N2 - 2) #deegrees of freedom
    std1 = 0.8 #standard deviations
    std2 = 0.5
    std_N1N2 = sqrt( ((N1 - 1)*(std1)**2 + (N2 - 1)*(std2)**2) / df) #average standard deviations between groups.
    diff_mean = 8.9 - 8.5 #difference between means
    MoE = t.ppf(0.975, df) * std_N1N2 * sqrt(1/N1 + 1/N2) # margin of error 
    
    print(f"Difference between means is: {diff_mean:.2f} ( {diff_mean-MoE:.2f}, {diff_mean + MoE:.2f} )")
    
    

    result:

    Difference between means is: 0.40 ( -0.16, 0.96 )
    

    t.ppf(0.975, df) is t-value associated with 95% cut-off

    So possible interpretation would be that You reject null hypothesis that difference between means is equal zero at a significance level of 0.05.