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

变量长度不一致导致线性回归建模失败问题求助

解决线性回归中因缺失值导致的变量长度不一致问题

问题背景

有一个包含缺失值的.txt数据集,其中AdvExp6(过去6个月广告支出)前6个月因无法获取历史数据而缺失。拟合以NewPI为因变量、AdvExp6为自变量的线性回归模型时,触发以下错误:

Error in (function (formula, data = NULL, subset = NULL, na.action = na.fail, :
variable lengths differ (found for 'AdvExp6')

用户使用的代码如下:

LegalAdv <- read.table("LegalAdv.txt", header=TRUE)
str(LegalAdv)
head(LegalAdv, 20)
with(LegalAdv, plot(AdvExp6, NewPI, type="p"))
with(LegalAdv, plot(AdvExp6, NewWC, type="p"))

M1 <- lm(NewPI ~ AdvExp6, data=LegalAdv)
with(LegalAdv, plot(NewPI ~ AdvExp6, type="p"))
with(LegalAdv, lines(fitted(M1) ~ AdvExp6))

附完整数据集:

LegalAdv <- data.frame(
  Month = 1:48,
  TotAdvExp = c(
    27154.5, 28966.8, 16993.8, 11960.5, 27452.2, 27120.2, 27806.2, 14553.4,
    14436.8, 5148.6, 16892.2, 45167.2, 24011.9, 26199.4, 7616.9, 30537.9, 5630.7,
    13743.4, 34980.6, 22960.4, 29992.7, 39880.5, 9169.9, -517.5, 10689.9, 44988.5,
    11548.9, 27664.1, 27543.4, 16218.6, 30464.4, 50717, 48558, 42463.8, 22310.4,
    25774.9, 13852.9, 47073.8, 44442.2, 18969.4, 35094.6, 19375.8, 3087.6,
    32296.2, 31087, 39952.8, -2072, 26889.4
  ),
  NewPI = c(
    4L, 12L, 3L, 10L, 7L, 8L, 11L, 27L, 15L, 27L, 35L, 22L, 13L,
    32L, 36L, 30L, 17L, 27L, 19L, 17L, 42L, 39L, 33L, 32L, 20L, 30L,
    32L, 29L, 31L, 22L, 45L, 40L, 46L, 42L, 34L, 40L, 42L, 31L, 31L,
    47L, 30L, 25L, 34L, 28L, 45L, 25L, 22L, 27L
  ),
  NewWC = c(
    23L, 33L, 23L, 25L, 21L, 22L, 42L, 32L, 28L, 31L, 36L, 47L,
    31L, 43L, 41L, 35L, 43L, 29L, 24L, 19L, 40L, 19L, 24L, 44L, 38L,
    36L, 24L, 50L, 24L, 24L, 38L, 36L, 32L, 29L, 27L, 32L, 26L, 37L,
    41L, 24L, 41L, 41L, 35L, 23L, 38L, 35L, 27L, 31L
  ),
  AdvExp6 = c(
    NA, NA, NA, NA, NA, NA, 167.4542, 154.8531, 140.3231, 128.4779, 133.4096,
    151.1246, 148.0163, 146.4095, 139.473, 155.5741, 156.0562, 152.9074, 142.7208,
    141.6693, 145.4626, 177.7262, 156.3582, 150.21, 147.1565, 157.1644, 145.7529,
    143.4243, 131.0872, 138.1359, 169.1178, 209.1449, 212.7144, 243.6293,
    238.2756, 236.5071, 234.1414, 250.7508, 244.476, 214.8874, 207.5182, 204.5836,
    181.8963, 200.3396, 184.3528, 179.8634, 158.822, 150.6168
  )
)

问题原因

lm()函数默认的na.action参数为na.fail,即遇到缺失值直接报错。这里AdvExp6前6个值为NA,触发了该规则,导致系统误判为变量长度不一致。

解决方案

修改lm()的na.action参数,使用na.omit或na.exclude自动处理缺失值:

方案1:使用na.omit删除含缺失值的行

# 读取数据
LegalAdv <- read.table("LegalAdv.txt", header=TRUE)
# 查看数据结构和前20行
str(LegalAdv)
head(LegalAdv, 20)
# 绘制散点图
with(LegalAdv, plot(AdvExp6, NewPI, type="p"))
with(LegalAdv, plot(AdvExp6, NewWC, type="p"))

# 拟合模型,自动删除含缺失值的行
M1 <- lm(NewPI ~ AdvExp6, data=LegalAdv, na.action=na.omit)
# 查看模型系数、R²等统计结果
summary(M1)
# 绘制散点图和拟合回归线
with(LegalAdv, plot(NewPI ~ AdvExp6, type="p"))
lines(fitted(M1) ~ LegalAdv$AdvExp6[!is.na(LegalAdv$AdvExp6)])

方案2:使用na.exclude(保留缺失值位置的预测结果)

M1 <- lm(NewPI ~ AdvExp6, data=LegalAdv, na.action=na.exclude)
summary(M1)
with(LegalAdv, plot(NewPI ~ AdvExp6, type="p"))
lines(fitted(M1) ~ LegalAdv$AdvExp6)

结果说明

两种方案都会自动排除前6行带NA的数据,用剩余42行拟合回归模型。执行summary(M1)可查看系数、显著性、R²等关键统计量,拟合的回归线也能正常显示在散点图上。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 17:15:39