程序代写 wk6_lec_support(1)

wk6_lec_support(1)

Week 6 T-test¶
© Professor Yuefeng Li

Copyright By PowCoder代写 加微信 powcoder

import pandas as pd
import scipy

# example 1
data = {‘Category’: [‘cat2′,’cat1′,’cat1′,’cat1′,’cat2′,’cat1′,’cat2′,’cat1′,’cat2′,’cat1′,’cat1′,’cat1′,’cat2′,’cat2′,’cat2’],
‘values’: [1,19,20,2,3,1,2,3,1,2,3,5,1,0,1]}
my_data = pd.DataFrame(data)
my_data.groupby(‘Category’).mean()

#example 2
V1 = {‘Variable’:[1,2,3,4,5,6], ‘values’:[12,15,9,7,9,6]}
V2 = {‘Variable’:[1,2,3,4,5,6], ‘values’:[10,3,5,6,3,5]}
my_V1 = pd.DataFrame(V1)
my_V2 = pd.DataFrame(V2)

from scipy.stats import ttest_ind

cat1 = my_data[my_data[‘Category’]==’cat1′]
cat2 = my_data[my_data[‘Category’]==’cat2′]
test1=ttest_ind(cat1[‘values’], cat2[‘values’], alternative=’greater’)
test2=ttest_ind(cat1[‘values’], cat2[‘values’], alternative=’two-sided’)
print(test1)
print(test2)

print(my_V1)
print(my_V2)
test3=ttest_ind(my_V1[‘values’], my_V2[‘values’]) # two-tails in python
test4=ttest_ind(my_V1[‘values’], my_V2[‘values’], equal_var=False)
# test4 results is closed to Excel t-Test: Two-Sample Assuming Unequal Variances
print(test3)
print(test4)

# scipy.stats.ttest_ind(a, b, axis=0, equal_var=True, nan_policy=’propagate’, permutations=None, random_state=None, alternative=’two-sided’, trim=0)
# alternative{‘two-sided’, ‘less’, ‘greater’}, optional
# Defines the alternative hypothesis. The following options are available (default is ‘two-sided’):
# ‘two-sided’: the means of the distributions underlying the samples are unequal.
# ‘less’: the mean of the distribution underlying the first sample is less than the mean of the distribution underlying the second sample.
# ‘greater’: the mean of the distribution underlying the first sample is greater than the mean of the distribution underlying the second sample.

Ttest_indResult(statistic=1.8556679485212833, pvalue=0.0431546769600154)
Ttest_indResult(statistic=1.8556679485212833, pvalue=0.0863093539200308)
Variable values
0 1 12
1 2 15
2 3 9
3 4 7
4 5 9
5 6 6
Variable values
0 1 10
1 2 3
2 3 5
3 4 6
4 5 3
5 6 5
Ttest_indResult(statistic=2.520591886570814, pvalue=0.03035652574914744)
Ttest_indResult(statistic=2.520591886570814, pvalue=0.031669547795686336)

# Example 3
from scipy import stats
import numpy as np
wt = np.array([71.93636,71.34689,72.2162])
mut = np.array([71.58995,70.82698,70.89562])
t, p = stats.ttest_ind(wt, mut, equal_var=False)
print(t,p)

2.061639430016146 0.10842572187616989

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com