You need to enable JavaScript to run this app. 基于age、diagnosis1变量开展患者诊断数量线性回归建模的lm报错排查
优惠活动
大模型
产品
解决方案
定价
更多

基于age、diagnosis1变量开展患者诊断数量线性回归建模的lm报错排查

建模需求

计划采用线性回归方法,将患者诊断总数number_diagnoses建模为age、diagnosis1两个变量的函数。

运行报错情况

执行代码 lm(age~diagnosis1, data = df) 时,程序抛出如下错误:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : NA/NaN/Inf in 'y'
前置数据处理说明

已对数据集内的缺失值采用众数插补方法完成处理,理论上不存在标准NA类缺失值,用于定位问题的数据集结构(通过dput导出)如下:

structure(list(patient_nbr = c(3069L, 4302L, 5220L, 5220L, 5220L, 
5220L), encounter_id = c(36469686L, 85907514L, 7981038L, 33503946L, 
55397514L, 60892254L), admission_source_id = structure(c(1L, 
1L, 15L, 1L, 1L, 15L), .Label = c("1", "10", "11", "13", "14", 
"17", "2", "20", "22", "25", "3", "4", "5", "6", "7", "8", "9"
), class = "factor"), admission_type_id = structure(c(2L, 3L, 
1L, 2L, 2L, 1L), .Label = c("1", "2", "3", "4", "5", "6", "7", 
"8"), class = "factor"), discharge_disposition_id = structure(c(1L, 
1L, 1L, 1L, 23L, 1L), .Label = c("1", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "2", "20", "22", "23", "24", 
"25", "27", "28", "3", "4", "5", "6", "7", "8", "9"), class = "factor"), 
    medical_specialty = c("Cardiology", "Orthopedics-Reconstructive", 
    "InternalMedicine", "Cardiology", "InternalMedicine", "InternalMedicine"
    ), payer_code = c("?", "?", "?", "?", "?", "?"), readmitted = c("NO", 
    "NO", ">30", ">30", ">30", "NO"), time_in_hospital = c(8L, 
    1L, 2L, 11L, 8L, 1L), number_diagnoses = c(9L, 7L, 9L, 9L, 
    9L, 9L), num_medications = c(31L, 9L, 14L, 19L, 15L, 12L), num_procedures = c(6L, 
    1L, 0L, 4L, 3L, 0L), number_emergency = c(0L, 0L, 0L, 0L, 
    0L, 0L), number_inpatient = c(0L, 0L, 0L, 2L, 1L, 2L), number_outpatient = c(0L, 
    0L, 0L, 0L, 0L, 0L), age = c("[60-70)", "[70-80)", "[60-70)", 
    "[70-80)", "[70-80)", "[70-80)"), gender = c("Male", "Female", 
    "Male", "Male", "Male", "Male"), race = c("Caucasian", "Caucasian", 
    "Caucasian", "Caucasian", "Caucasian", "Caucasian"), weight = c("?", 
    "?", "?", "?", "?", "?"), diagnosis1 = c("circulatory", "musculoskeletal", 
    "EMI", "circulatory", "skin", "EMI"), diagnosis2 = c("genitourinary", 
    "EMI", "circulatory", "circulatory", "EMI", "skin"), diagnosis3 = c("blood", 
    "circulatory", "digestive", "EMI", "circulatory", "circulatory"
    )), row.names = 18:23, class = "data.frame")
报错根因定位
  • 建模公式方向错位:需求是将number_diagnoses作为因变量,但实际代码将age放在了公式左侧的因变量位置,与建模目标完全相反。
  • 因变量类型不满足线性回归要求:数据集内的age为字符串格式的区间文本(如[60-70)、[70-80)),不属于可参与数值计算的数值型变量。R在将该类文本向量作为因变量传入线性回归时,会强制将其转换为数值类型,所有非数字字符串转换后都会变为NA,直接触发NA/NaN/Inf in 'y'的报错。
  • 缺失值识别遗漏:数据集中的缺失值并非以标准NA格式存储,而是以?字符串形式标记(如payer_code、weight列的取值),此前的众数插补流程若未识别这类字符串标记的缺失值,后续计算仍可能触发异常。
修正操作步骤
  1. 对齐建模目标调整公式,将number_diagnoses设为因变量,age、diagnosis1设为自变量:
    model <- lm(number_diagnoses ~ age + diagnosis1, data = df)
    
  2. 转换age列的存储类型,避免文本被错误转换为NA:
    • 若需将年龄作为连续变量使用,可提取年龄区间的中点转换为数值:
      df$age <- sapply(strsplit(df$age, "[-\\(\\)]"), function(x) {
        x_val <- as.numeric(x[!x %in% c("", "[", ")")])
        mean(x_val)
      })
      
    • 若需将年龄作为分类效应使用,可将其转换为有序因子:
      age_levels <- c("[0-10)","[10-20)","[20-30)","[30-40)","[40-50)","[50-60)","[60-70)","[70-80)","[80-90)","[90-100)")
      df$age <- factor(df$age, levels = age_levels, ordered = TRUE)
      
  3. 统一识别以?标记的缺失值,按标准流程完成插补:
    df[df == "?"] <- NA
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.24 15:15:03