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

R语言逐步回归报错:Error in is.data.frame(data) : object '.' not found

解决逐步回归中Error in is.data.frame(data) : object '.' not found的问题

错误原因

你用dplyr的%>%管道把过滤后的数据直接传给lm()时,生成的线性模型对象里,data属性会被设置成管道的临时占位符.。这个.只在管道执行的瞬间存在,当后续调用step()函数时,临时对象已被回收,step()找不到数据对象就会抛出该错误。

解决方案

把过滤后的数据单独存成一个变量,再传入lm(),避免依赖管道的临时对象:

# 先保存过滤后的数据集
filtered_1999 <- country_tidy %>%
  filter(Year == "1999", 
         !is.na(`Life expectancy at birth, total (years)`))

# 基于保存好的数据集构建初始模型
lm_life_expectancy_1 <- lm(
  formula = `Life expectancy at birth, total (years)` ~ 1,
  data = filtered_1999
)

# 执行逐步回归
step(lm_life_expectancy_1, scope = ~ `Age at first marriage, female` + 
       `Age at first marriage, male` +
       `Birth rate, crude (per 1,000 people)` +
       `Births attended by skilled health staff (% of total)` +
       `Death rate, crude (per 1,000 people)` +
       `Fertility rate, total (births per woman)` +
       `GNI per capita, Atlas method (current US$)` +
       `Health expenditure per capita (current US$)` +
       `Health expenditure, total (% of GDP)` +
       `Hospital beds (per 1,000 people)` +
       `Incidence of HIV (% of uninfected population ages 15-49)` +
       `Incidence of tuberculosis (per 100,000 people)` +
       `Literacy rate, adult total (% of people ages 15 and above)` +
       `Mortality rate, adult, female (per 1,000 female adults)` +
       `Mortality rate, adult, male (per 1,000 male adults)` +
       `Mortality rate, infant (per 1,000 live births)` +
       `Physicians (per 1,000 people)` +
       `Population growth (annual %)` +
       `Population, female` +
       `Population, male` +
       `Population, total` +
       `Rural population` +
       `Rural population growth (annual %)` +
       `Urban population` +
       `Urban population growth (annual %)`)

关于缺失值的说明

数据中的缺失值不会直接导致这个错误。lm()默认会自动删除包含缺失值的行(行删除法),step()会基于lm()处理后的数据集继续运算。不过如果后续变量缺失值过多,可能会导致可用样本量骤降,但这和当前遇到的object '.' not found错误无关。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 23:52:43