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

构建gtsummary关联logistic回归时glm报错:'weights'必须为数值向量

问题描述

尝试构建logistic回归模型时出现报错,对比gtsummary示例仍未定位问题。

报错信息

Error in glm(disease_present ~ disease_cat, sex, age, data = df, :
'weights' must be a numeric vector

原代码

mod <- glm(disease_present ~ disease_cat, sex, age, 
           data = df, family = binomial)

数据

df <-
  structure(
    list(
      disease_cat = structure(
        c(
          7L,
          2L,
          2L,
          2L,
          2L,
          5L,
          2L,
          2L,
          2L,
          2L,
          2L,
          3L,
          2L,
          2L,
          5L,
          2L,
          2L,
          2L,
          2L,
          2L,
          2L,
          2L,
          5L,
          5L,
          5L,
          2L,
          8L,
          2L,
          8L,
          2L,
          7L,
          2L,
          7L,
          2L,
          7L,
          8L,
          8L,
          2L,
          2L,
          7L,
          2L,
          7L,
          7L,
          2L,
          2L,
          2L,
          2L,
          2L,
          5L,
          5L
        ),
        .Label = c(
          "breast",
          "prostate",
          "colorectal",
          "lung",
          "melanoma",
          "bladder",
          "pancreatic",
          "endometrial"
        ),
        class = "factor"
      ),
      disease_present = c(
        1,
        0,
        0,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        1,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        1,
        0,
        1,
        0,
        1,
        0,
        1,
        0,
        1,
        0,
        1,
        1,
        1,
        0,
        0,
        1,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        0,
        1,
        1
      ),
      sex = structure(
        c(
          2L,
          2L,
          1L,
          2L,
          1L,
          2L,
          1L,
          2L,
          1L,
          1L,
          2L,
          1L,
          1L,
          1L,
          2L,
          2L,
          1L,
          2L,
          2L,
          1L,
          2L,
          2L,
          1L,
          1L,
          2L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          1L,
          1L,
          2L,
          1L,
          2L,
          1L,
          1L,
          2L,
          1L,
          1L,
          2L,
          2L,
          1L,
          1L
        ),
        .Label = c("female", "male"),
        class = "factor"
      ),
      age = c(
        18,
        41,
        40,
        45,
        48,
        25,
        50,
        56,
        45,
        46,
        45,
        28,
        40,
        50,
        25,
        50,
        40,
        40,
        40,
        40,
        50,
        50,
        29,
        26,
        8,
        55,
        60,
        40,
        35,
        40,
        22,
        45,
        10,
        45,
        14,
        21,
        45,
        53,
        55,
        18,
        45,
        18,
        35,
        53,
        45,
        45,
        54,
        48,
        30,
        44
      )
    ),
    row.names = c(NA,-50L),
    class = c("tbl_df",
              "tbl", "data.frame")
  )
问题解决

报错原因是glm公式写法错误:在R的回归公式中,多个自变量之间必须用+连接,而非逗号。你原代码用逗号分隔disease_cat, sex, age,导致R把sex误识别为glm函数的weights参数(glm参数顺序为formula, family, data, weights, ...),但sex是因子类型,不符合weights必须为数值向量的要求,因此触发报错。

修正后的代码:

mod <- glm(disease_present ~ disease_cat + sex + age, 
           data = df, family = binomial)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 12:15:42