評估 BMI 的結果
BMI 最高的前 10% 與最低的後 10% 病患,其預測疾病進展(反應變數 y)有什麼差異?你將使用多變量常態分配取樣的模擬結果來回答這個問題!
模擬已經為你執行好;你的任務是評估 df_results 中的模擬結果。
以下函式庫已為你匯入:pandas 作為 pd、numpy 作為 np,以及 scipy.stats 作為 st。
本練習屬於課程
Python 的 Monte Carlo 模擬
練習說明
- 使用
np.quantile()計算模擬結果中bmi的第 10 分位數與第 90 分位數,並分別存為bmi_q10與bmi_q90。 - 使用
bmi_q10與bmi_q90過濾df_summary,取得對應的預測 y 值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
simulation_results = st.multivariate_normal.rvs(mean=mean_dia, size=20000, cov=cov_dia)
df_results = pd.DataFrame(simulation_results,columns=["age", "bmi", "bp", "tc", "ldl", "hdl", "tch", "ltg", "glu"])
predicted_y = regr_model.predict(df_results)
df_y = pd.DataFrame(predicted_y, columns=["predicted_y"])
df_summary = pd.concat([df_results,df_y], axis=1)
# Calculate the 10th and 90th quantile of bmi in the simulated results
bmi_q10 = np.quantile(df_summary["bmi"], ____)
bmi_q90 = np.quantile(df_summary["bmi"], ____)
# Use bmi_q10 and bmi_q90 to filter df_summary and obtain predicted y values
mean_bmi_q90_outcome = np.mean(df_summary[____]["predicted_y"])
mean_bmi_q10_outcome = np.mean(df_summary[____]["predicted_y"])
y_diff = mean_bmi_q90_outcome - mean_bmi_q10_outcome
print(y_diff)