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

R语言调用yeojohnson函数传入lm对象报错参数x缺失如何解决?

R代码yeojohnson函数报错解决方案

报错原因

Error in stopifnot(is.numeric(x)) : argument "x" is missing, with no default

  • 该报错由bestNormalize包的yeojohnson()函数参数规则触发:该函数第一个必填参数x要求传入待转换的数值向量,不支持直接传入lm线性模型对象。你当前的传参写法是car包同名函数yeoJohnson()的用法,两个包的同名函数参数设计不一致,因此触发参数缺失报错。

修复方案

方案1:继续使用bestNormalize包实现转换

直接将待转换的因变量数值向量传入x参数即可,示例代码:

library(bestNormalize)

# 加载数据
data("cars", package = "datasets")

# 对因变量dist执行Yeo-Johnson转换
yj_transform <- yeojohnson(x = cars$dist, plotit = FALSE)
# 查看转换参数
print(yj_transform)

# 如需拟合转换后的线性模型,可提取转换后的值
cars$dist_transformed <- predict(yj_transform)
lm_cars <- lm(dist_transformed ~ speed, data = cars)

方案2:切换为car包实现传入lm对象的转换

如果你需要直接对lm模型的因变量做转换,可加载car包调用同名函数,该函数支持直接传入lm模型对象作为参数:

library(car)

# 加载数据、拟合线性模型
data("cars", package = "datasets")
lm_cars <- lm(dist ~ speed, data = cars)

# 直接传入lm对象执行转换
yj_result <- yeoJohnson(object = lm_cars, plotit = FALSE)
print(yj_result)

内容的提问来源于stack exchange,提问作者Renée

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 08:45:06