การแสดงภาพสถิติทดสอบ
ในแบบฝึกหัดนี้ จะสำรวจสมมติฐานว่างโดยเปรียบเทียบการกระจายตัวของสถิติทดสอบจากสองวิธีที่แตกต่างกัน
ขั้นแรก จะพิจารณาประชากร 2 กลุ่มที่แบ่งตามช่วงเวลาต้นและช่วงเวลาหลัง แล้วคำนวณการกระจายตัวของสถิติทดสอบ จากนั้นสับเปลี่ยนข้อมูลของทั้งสองกลุ่มให้ปะปนกัน เพื่อให้ข้อมูลไม่เรียงตามลำดับเวลาอีกต่อไป แล้วคำนวณการกระจายตัวของสถิติทดสอบใหม่อีกครั้ง
เพื่อเริ่มต้น ได้โหลดกลุ่มช่วงเวลา 2 กลุ่มไว้ให้แล้ว ได้แก่ group_duration_short และ group_duration_long รวมถึงฟังก์ชัน 2 ตัว คือ shuffle_and_split() และ plot_test_statistic()
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Introduction to Linear Modeling in Python
คำแนะนำการฝึกหัด
- ใช้
np.random.choice()เพื่อสุ่มตัวอย่างใหม่จากgroup_duration_shortและgroup_duration_longแล้วหาผลต่างระหว่างตัวอย่างเพื่อคำนวณtest_statistic_unshuffled - ใช้
shuffle_and_split()กับgroup_duration_shortและgroup_duration_longตามลำดับ (ระบุตามลำดับนี้) เพื่อสร้างประชากรผสม 2 กลุ่มใหม่ - สุ่มตัวอย่างใหม่จากประชากรที่สับเปลี่ยนแล้ว แล้วลบ
resample_shortออกจากresample_longเพื่อคำนวณtest_statistic_shuffledใหม่ - ใช้
plot_test_statistic()เพื่อพล็อตการกระจายตัวของสถิติทดสอบทั้งสองและเปรียบเทียบด้วยภาพ
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# From the unshuffled groups, compute the test statistic distribution
resample_short = np.random.choice(____, size=500, replace=____)
resample_long = np.random.choice(____, size=500, replace=____)
test_statistic_unshuffled = ____ - ____
# Shuffle two populations, cut in half, and recompute the test statistic
shuffled_half1, shuffled_half2 = shuffle_and_split(____, ____)
resample_half1 = np.random.choice(____, size=500, replace=____)
resample_half2 = np.random.choice(____, size=500, replace=____)
test_statistic_shuffled = resample_half2 - resample_half1
# Plot both the unshuffled and shuffled results and compare
fig = plot_test_statistic(____, label='Unshuffled')
fig = plot_test_statistic(____, label='Shuffled')