我正在开展一个引导项目,需要对 M=N-1 个观测值进行放回抽样,其中 N 是特定组(由 group_id 定义)中唯一观测值的数量。我需要弄清楚如何在极坐标系中执行此操作。有什么解决方案吗?
这是一个展示我想要完成的事情的例子:
# Have:
water_data = {
'group_id': [1,1,1,1,2,2,2,3,3,3,4,4,4,4,5,5,5],
'obs_id_within_group': [1,2,3,4,1,2,3,1,2,3,1,2,3,4,1,2,3],
'N': [4,4,4,4,3,3,3,3,3,3,4,4,4,4,3,3,3],
'M': [3,3,3,3,2,2,2,2,2,2,3,3,3,3,2,2,2],
'water_gallons': [12,23,21,11,10,10,10,23,24,25,27,30,17,12,11,14,20],
'water_source': ['lake','lake','pond','river','lake','glacier','glacier','lake','pond','river','lake','lake','pond','river','river','lake','glacier'],
'water_acidity': [3,4,5,1,2,4,3,2,3,3,4,6,7,8,8,3,1]
}
df=pl.DataFrame(water_data)
print(df)
# Want to randomly sample with replacement to:
sampled_water_data = {
'group_id': [1,1,1,2,2,3,3,4,4,4,5,5],
'obs_id_within_group': [1,2,2,3,3,3,2,4,1,1,2,1],
'N': [4,4,4,3,3,3,3,4,4,4,3,3],
'M': [3,3,3,2,2,2,2,3,3,3,2,2],
'water_gallons': [12,23,23,10,10,25,24,12,27,27,14,11],
'water_source': ['lake','lake','lake','glacier','glacier','river','pond','river','lake','lake','lake','river'],
'water_acidity': [3,4,4,3,3,3,3,8,4,4,5,8]
}
df_sampled=pl.DataFrame(sampled_water_data)
print(df_sampled)
不确定如何从每个组中抽取一个特定的数字。