You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于所有因子组合的相对频率计算:高血压风险关联分析与数据重塑技术问询

Hey Julian, here's a clean, efficient way to compute the risk scores for every possible combination of your blood components, exactly as you requested:

Step-by-Step Solution

First, make sure you have the tidyverse package installed (it includes dplyr for data manipulation and tibble for structured data frames):

# Load required package
library(tidyverse)

Next, use your existing dataset (I've included it here for completeness) and calculate the risk scores with these simple dplyr operations:

# Your original dataset
set.seed(1234)
df <- tibble::tibble(
  high_bp = sample(c("low", "high"), 1000, TRUE),
  amylase = sample(c("low", "high"), 1000, TRUE),
  proteins = sample(c("low", "high"), 1000, TRUE),
  sodium = sample(c("low", "high"), 1000, TRUE)
)

# Calculate risk scores for all component combinations
risk_result <- df %>%
  # Group data by every unique combination of the three blood components
  group_by(amylase, proteins, sodium) %>%
  # Compute risk as the relative frequency of high blood pressure
  summarise(
    risk_high_bp = mean(high_bp == "high"),
    .groups = "drop"  # Remove grouping to return a standard tibble
  )

# View the full result
print(risk_result)

How This Works

  • group_by(amylase, proteins, sodium): This splits your dataset into subgroups for every possible combination of the three blood components (there are 2×2×2=8 total combinations).
  • mean(high_bp == "high"): In R, logical values (TRUE/FALSE) are treated as 1/0 when calculating a mean. This means the average directly gives you the proportion of observations with high_bp = "high"—exactly your risk score formula.
  • .groups = "drop": Ensures the output is a regular tibble (not a grouped one) for easy downstream use.

Handling Missing Combinations

If your real dataset might have some component combinations with no observations, you can use complete() to retain all 8 possible combinations and fill missing risk scores with NA (or 0 if you prefer):

risk_result_full <- df %>%
  group_by(amylase, proteins, sodium) %>%
  summarise(
    risk_high_bp = mean(high_bp == "high"),
    .groups = "drop"
  ) %>%
  # Force all combinations to appear in the result
  complete(amylase = c("low", "high"), 
           proteins = c("low", "high"), 
           sodium = c("low", "high"),
           fill = list(risk_high_bp = NA))  # Fill missing values with NA

When you run the first code block with your random dataset, you'll see risk scores close to 0.5 for all combinations (since everything was sampled randomly). For your real data, these scores will reflect the actual association between blood components and hypertension risk.

内容的提问来源于stack exchange,提问作者Julian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 17:57:44