在临床研究中的随机化分组方法这篇文章中提到了将入组患者随机分配到治疗组和对照组的随机分配规则,如抽取混合有100个A组和100个B组的编码球的方法。
学习python的时候想到可以用代码实现洗牌的效果,将入组患者编码,然后将编码“洗牌”,前一半进入组1,后一半进入组2:
import numpy as np
def divide_patients(num_patients):
"""Divides patients into two equal groups.
Args:
num_patients: Total number of patients.
Returns:
A tuple of two lists containing the patient indices for each group.
"""
if num_patients % 2 != 0:
raise ValueError("Number of patients must be even for equal division.")
patient_indices = np.arange(num_patients)
np.random.shuffle(patient_indices)
group1 = patient_indices[:num_patients // 2]
group2 = patient_indices[num_patients // 2:]
return group1, group2
# Example usage:
num_patients = 20
group_a, group_b = divide_patients(num_patients)
print(group_a)
print(group_b)
或者
import random
def divide_patients_random(num_patients):
"""Divides patients into two equal groups using random.sample.
Args:
num_patients: Total number of patients.
Returns:
A tuple of two lists containing the patient indices for each group.
"""
if num_patients % 2 != 0:
raise ValueError("Number of patients must be even for equal division.")
all_patients = list(range(num_patients))
random.shuffle(all_patients)
group1 = all_patients[:num_patients // 2]
group2 = all_patients[num_patients // 2:]
return group1, group2
同样的:
- 样本大小的平衡只在试验结束时达成,而非贯穿整个试验过程。
- 在接近试验结束时,分配序列容易被破译。
转载请注明来源:使用python代码实现随机分配规则
本文链接地址:https://omssurgeon.com/2509/