lm.fit(x,y,offset = offset, singular.ok,...) 错误 0 个使用 boxcox 公式的非 NA 案例

Error in lm.fit(x,y,offset = offset, singular.ok,...) 0 non-NA cases with boxcox formula

我正在尝试 运行 使用以下代码进行 boxcox 转换:

urban1 <- subset(ski,urban <= 4,na.rm=TRUE)
ski$gender <- as.numeric((as.character(ski$gender)),na.rm=TRUE)
urban1 <- as.numeric((as.character(urban1)))
x <- (ski$gender*urban1)
y <- ski$EPSI.
bc <- boxcox(y ~ x) 
(trans <- bc$x[which.max(bc$y)]) 
model3 <- lm(y ~ x) 
model3new <- lm(y^trans ~ x)
ski$EPSI. <- ski$EPSI. + 1

但是我一直收到这个错误:

lm.fit(x,y,offset = offset, singular.ok = singular.ok, ...) 中的错误:0(非 NA)案例调用:... eval -> eval -> boxcar -> boxcar.formula -> lm -> lm.fit 执行暂停

提前致谢!

错误信息

lm.fit(x,y,offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases

当变量 xy(或两者)只有 NA 时,

lm(y ~ x) 命令生成。
这是一个例子:

n <- 10
x <- rnorm(n,1)
y <- rep(NA,n)
lm(y ~ x)

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

在你的代码中,我建议测试(就在你的 lm 命令之前)你的变量之一是否具有所有 NA 使用:

all(is.na(x))
all(is.na(y))
all(is.na(y^trans))

在我的例子中:

all(is.na(y))
[1] TRUE

错误可能由 数据中的 NA 错误转换

触发
#From the mtcars dataset
mpg.reg3 <- lm(mpg ~ cylinders + displacement + horsepower + weight + acceleration + year + origin, data=Auto, na.action=na.exclude)

注意 na.action= 参数。将此设置为 na.exclude 将允许 lm 函数忽略数据中的 NA。另一个选项是 na.omit,它的行为方式略有不同。

另一个问题可能是您的数据转换不当 - 请仔细检查您的交互项和操作。